Wednesday, September 2, 2009

enableWebScript, UriTemplate, and HTTP methods

I got very nice post on enableWebScript from Justin Smith blog so I have posted it on my blog:

A little while ago I ran into an interesting set of errors that may be of interest to you. Consider the following service contract snippet:

[OperationContract]
[WebGet(UriTemplate="foobar/{value}")]
String GetData(String value);

If you add the enableWebScript behavior to an endpoint that is using the WebHttpBinding, you will see this exception when the ServiceHost starts:

System.InvalidOperationException: Endpoints using 'UriTemplate' cannot be used with 'System.ServiceModel.Description.WebScriptEnablingBehavior'.

The reason for this error is rooted in the origin of the enableWebScript behavior. One of it's design objectives was to simplify working with the ASP.NET AJAX stack (Javascript proxy, JSON messages, etc). The AJAX stack doesn't have the equivalent of the UriTempalte type. It simply puts parameters in query strings (gets) and constructs entity bodies (posts). This is the default behavior of the WCF stack when the WebGet / WebInvoke annotations do not have a value for UriTemplate. Since any value of UriTemplate would be incompatible with the ASP.NET AJAX stack, we throw when it's present.

If you want JSON messages from a contract and you want to use the UriTemplate niceness, you can change your contract to:

[OperationContract][
WebGet(UriTemplate="foobar/{value}", ResponseFormat=WebMessageFormat.Json)]
String GetData(String value);

Then, instead of using the enableWebScript behavior, use the WebHttpBehavior. You'll lose compat with the ASP.NET AJAX client stack (and the JS proxy), but you have the URI you are looking for.

The same is true if you are using the WebInvoke attribute and any HTTP method other than POST. The AJAX client stack only knows GET and POST...

HTH...

Happy coding... :)


1 comment:

Onion Insights said...

Hi Dasharath

We at Onion Insights (onioninsights.blogspot.com) are always happy to come across bloggers like you. We are also glad to inform you that your writing skills can now be put to good use and you can make some extra money too.

Get paid to shop, dine and report and make some extra money using your writing and observation skills. The concept is pretty simple. Top retail chains, five star properties and fast food chains in India hire our services to get their stores evaluated through the eyes of a normal customer. We are looking for people in Baroda who frequently visit these outlets and write well.

Every week there are a certain number of reviews in your city. Based on your availability and interest, you can choose to perform a review for us. You visit the outlet and notice what’s going on. You then go to our website and fill in a report which has a questionnaire and comment boxes to help you document your observations.

Within 15-20 days you will be paid a pre determined shopper fee which will include an reimbursement for any amount spent in the store plus an additional shopper fee. That’s how the system works.

Follow the below mentioned steps to sign up as an OI reviewer

Go to this link http://www.sassieshop.com/2dcs
Click on new shopper sign up
Click "India" under new shopper and click on "go"
Enter your basic information in the form and click on "sign me up"

Please call us at +919769781001 or shoot us an email at support@onioninsights.com if you have any other queries or any difficulty while signing up.


For more details log on to www.onioninsights.blogspot.com

Warm Regards,
OI Team