<CharlieDigital/> Programming, Politics, and uhh…pineapples



Posted by Charles Chen

My house backs a small forest.  So from time to time, we get some interesting visitors.

I saved these guys while I was mowing the lawn...I hope I didn't kill any of their siblings :-S

I briefly considered feeding them to my bearded dragon, Quincy, but decided that it was better to just put them back.

This deer came by about two weeks ago:

It was pretty cool because he walked right out into the main yard before he got spooked and ran back into the woods.

We've also had a red fox visit our back yard one time.  There's also this little garden snake living under our front stairs which I keep seeing every few weeks.  I've been trying to catch him, but who knew snakes could crawl backwards?


IE7, AJAX, And 400 “Bad Request”

Posted by Charles Chen

I spent bout two hours today tring to figure out why I kept getting a weird error while using prototype to call a WCF service.

There are various other resources on the web regarding how WCF has a few quirks with error handling using the default endpoints, however, none of these scenarios were applicable to me since the JavaScript call worked fine in FireFox.  To complicate things even further, it actually all worked fine in IE7 as well with one caveat: it only worked on the first load of the IE7 process.

After that, any refreshing of the page would return 400/"Bad Request" errors, mysteriously.  Just to make sure that it wasn't isolated to prototype, I also tried jQuery as well.  Still nothing.

Well, as it turns out, that setting the AJAX request content type to application/json wasn't enough; I had to set it to application/json; charset=utf-8.

So if you're encountering weird issues with any AJAX library, IE7, and WCF and/or ASP.NET, be sure to check your content type setting.

Update 6/20/2008: bah!  I woke up this morning and it's not working again!  Oddly, it works fine if I have Fiddler running and as soon as I shut off Fiddler, it stops working.  To complicate matters even more, it's only happening on methods where I have post content.

Update 6/22/2008: after further investigation, I'm concluding that it's related to SharePoint.  To isolate the different components, I created a custom WCF service and a simple runtime which models our current custom WCF runtime.  When I used a plain HTML page or even an ASPX page with a script manager on it, prototype AJAX requests worked fine.  Okay, so it wasn't how we were hosting the service and it wasn't an issue with a collision between ASP.NET AJAX and prototype.  I then hooked a SharePoint layout page up to the same service and boom, it all breaks :-S

I've broken it out into the following scenarios:

  1. Using FireFox, the requests always work.  No matter how many times I refresh the page, the AJAX request always works fine.
  2. Using IE7, it will always work on the first load of the IE process.
    1. If I hit CTRL+F5, it will continue to work.
    2. If at any point, I hit F5, it will fail and even CTRL+F5 will not fix it.
    3. If I have Fiddler running, it will always work, even if it entered a failure state after hitting F5! (So Fiddler must be doing something to the HTTP message??). As far as I can tell, the only difference between the Fiddler request and the native request, from viewing the WireShark trace, is the "Authorization" header and the fact that the failed request has a "Content-length" header value of 0 (manually setting the header doesn't work either).  I verified that prototype was not somehow mangling the POST body content by writing a trace of the XHR request body right before and right after the request is sent (both came out okay).

One of the really weird things about this error is that it cannot be observed when I have Fiddler running; somehow, Fiddler "fixes" the issue.  I had to hook up WireShark on the server and watch the raw TCP messages to finally see that on the unsuccessful attempts, the POST content to my WCF service was empty.  The method required one parameter, so the WCF serializer threw a formatter excpetion when it received an empty message:

The server encountered an error processing the request. The exception message is 'Error in deserializing body of request message for operation 'GetRoutes'. The OperationFormatter could not deserialize any information from the Message because the Message is empty (IsEmpty = true).'. See server logs for more details. The exception stack trace is:at System.ServiceModel.Dispatcher.PrimitiveOperationFormatter.DeserializeRequest(Message message, Object[] parameters) at System.ServiceModel.Dispatcher.DemultiplexingDispatchMessageFormatter.DeserializeRequest(Message message, Object[] parameters) at System.ServiceModel.Dispatcher.UriTemplateDispatchFormatter.DeserializeRequest(Message message, Object[] parameters) at System.ServiceModel.Dispatcher.CompositeDispatchFormatter.DeserializeRequest(Message message, Object[] parameters) at System.ServiceModel.Dispatcher.DispatchOperationRuntime.DeserializeInputs(MessageRpc& rpc) at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc& rpc) at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)

I've tried playing around with various cache avoidance strategies to no avail (setting the "Cache-Control" header, setting the "If-Modified-Since" header, setting the "Pragma" header, addin randomness to the POST URL) so for now, I'm just going to have to give up on this.  I've confirme the error using three different OS environments (XP SP2 (desktop), XP64 SP2 (dev), and 2003 R2 SP2 (VM)), all running IE 7.0.5730.11.

I know there are various articles out there regarding IE issues with XHR and how the ordering of the open(), onreadystatechange, and send() operations must be called; however, I verified in the prototype source (lines 1213-1223) that the correct order of operations are being performed.

Thinking that perhaps some Microsoft mojo was behind all of this, I gave the Sys.Net.WebRequest a shot.  This also did not yield any postive results.  Oddly, ASP.NET AJAX to web service calls all work fine (we had some existing AJAX components which were calling to a web service, but the goal was to transition away from writing these "empty" proxy services and call directly to the WCF service).

If any SharePoint development team members or program managers are reading this, please look into it!  I'm not sure if it affects IIS hosted WCF services at this point, but a sample project can be downloaded from here: WcfAjaxWonkiness.zip.  Create a layout page which calls the service and add it to SharePoint and observe it blowing up.

Filed under: Dev, SharePoint, WCF 3 Comments

Synchronizing SharePoint Files And Development Files

Posted by Charles Chen

I've previously posted on how to move compiled binaries into a remote GAC automatically from your development environment to your SharePoint environment using SysInternal's PsExec utility.

But what about moving content files from your SharePoint environment to your development environment?  When I'm developing content for layout pages (ASPX, js, css), for the most part, I don't want to develop in Visual Studio; I will usually use EditPlus (my weapon of choice) and edit the files directly on the remote server.

However, this poses a problem: synchronizing files between the SharePoint environment and the development environment.  It's quite time consuming and painful to do it manually since you may have to dig through various directories and keep tabs on which files you've edited on the server.

Enter 2BrightSparks' SyncBack utility.  Using this, I can create a synchronization automation between my SharePoint environment and my development environment.  And because I've set up my files to have the same structure on both sides, it's easy to set up the synchronization settings.

The image above shows the layout of my project. You can see in the image below at the directory layout mirrors the project layout, making automated synchronization a snap.

Working smarter > working harder.


Is Venter About To Bring Another Revolution In Science?

Posted by Charles Chen

If there's one man who could do this, it would have to be Venter.  Newsweek has a story on how Venter is working to revolutionize the way we view energy:

In a Maryland lab, he's manipulating chromosomes in the hopes of creating an energy bug—a bacterium that will ingest CO2, sunlight and water, and spew out liquid fuel that can be pumped into American SUVs

Talk about having your cake and eating it, too.

People want to bury that CO2 in the ground or pump it into oil wells or coal beds. We want to use that CO2 and the carbon in it to make new fuels.

We think the first fuels are maybe one to two years away. We're definitely thinking in terms of years, not decades.

Such a breakthrough would create a sudden shift in the geo-political climate to one where the Middle East becomes nearly irrelevant and perhaps the US dollar rebounds to become king (once again).

Venter also brings up a very intriguing point:

Right now oil is being isolated around the globe, and there is a major effort in shipping, trucking and otherwise transporting that oil around to a very finite number of refineries. Biology allows us to make these same fuels in a much more distributed fashion. I envision maybe a million micro-refineries. Companies, cities and potentially even individuals could have a small refinery to make their own fuel. This would eliminate a lot of the distribution problems and associated pollution.

Sounds like the days of $0.99/gallon fuel just might be in our future.

Filed under: News No Comments

WCF, Prototype, And Awesome-sauce

Posted by Charles Chen

One of the most awesome features of WCF in .NET 3.5 is support for REST-style AJAX requests to the services.

Rick Strahl has an excellent post on how to get it up and running with jQuery.

Just some notes on working with it myself which I hope can help clarify some some issues:

  • When using POST and the postBody option with Ajax.Request, the format must be {"argument-name":"value"}.  I struggled with this for a while using single quotes and no quotes.  For example: {arg:'hello'}, {'arg':'hello'}, {arg:"hello"} will all result in 400 "Bad Request" errors.
  • The contentType parameter must be set to "application/json".
  • Be sure to decorate the service contract using the WebInvokeAttribute with ResponseFormat and RequestFormat set to WebMessageFormat.Json.

You can download a sample project from here: WcfAjaxSample.zip (3.83 KB)

Filed under: .Net, WCF No Comments

Celebrating Science

Posted by Charles Chen

I like this Kamen interview:

Outfitting wounded veterans with dramatically better prosthetic limbs has been an emotional and rewarding journey for famed inventor Dean Kamen. But the project that has the biggest hold on his heart is a nonprofit called FIRST. The organization features a series of intellectual and scientific competitions. Students celebrate robotics the same way they celebrate football – complete with arenas, crowds, and cheerleaders.

Kamen says declines in graduating students and qualified engineers and scientists aren't an educational problem -- but a cultural one. When you celebrate sports and entertainment culturally, that's what kids naturally want to become. Solution? He's bringing sexy back to science. In this clip, he shares FIRST's results, and what corporate America is getting out of it too.

It needs a bit more love.  He makes a good point about the "unlimited class" of competition.  Whereas a cheetah can run faster than any human and an elephant can lift more than the strongest human, no animal can out-think humans.  Thus competitions of intellect are truly competitions of "unlimited class".


China Bans “Free” Plastic Bags

Posted by Charles Chen

I like this story:

China is banning free plastic bags common at shops and supermarkets and ordering customers to be charged for any they use, the government said Wednesday.

Shops have been instructed to mark the price of the plastic bags clearly and not fold them into the cost of other items.

Environmental organizations, including Greenpeace, praised China's move, and Christopher Flavin, president of Worldwatch Institute, an independent research organization in Washington, said "China is ahead of the U.S. with this policy," AP reported.

The Chinese use up to 3 billion plastic shopping bags a day.

Often, the flimsy bags are used once and discarded, adding to waste in a country grappling with air and water pollution as a result of rapid economic transformation, officials said.

"Our country consumes a large amount of plastic bags. While convenient for consumers, the bags also lead to a severe waste of resources and environmental pollution because of their excessive use and low rate of recycling," the statement at the Web site Gov.cn said. "The ultra-thin bags are the main source of 'white' pollution as they can easily get broken and end up as litter."

I think it's time for the US to do the same.  Not only are they not environmentally friendly, plastic bags are made from petroleum, contributing to the rising energy costs (fractionally).

Filed under: News No Comments