This one is real simple, run this Silverlight4 example with the ContentType property commented out and you'll get back a response from from my service in xml. Now uncomment the property and run it and you'll get an ProtocolViolationException, what should happen is the service returns JSON formatted data.
#if DEBUG
Address = "http://localhost.:55437/Services/GetFoodDescriptionsLookup(2100)";
#else
Address = "http://stephenpattenconsulting.com/Services/GetFoodDescriptionsLookup(2100)";
#endif
Uri httpSite = new Uri(Address);
HttpWebRequest wreq =
(HttpWebRequest)WebRequestCreator.ClientHttp.Create(httpSite);
//wreq.ContentType = "application/json"; // Wrong
wreq.Accept = "application/json"; // Right
wreq.BeginGetResponse((cb) =>
{
HttpWebRequest rq = cb.AsyncState as HttpWebRequest;
HttpWebResponse resp = rq.EndGetResponse(cb) as HttpWebResponse; // Exception
StreamReader rdr = new StreamReader(resp.GetResponseStream());
string result = rdr.ReadToEnd();
rdr.Close();
}, wreq);
New exception
System.NotSupportedException was unhandled by user code Message="" StackTrace: at System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state) at System.Net.Browser.BrowserHttpWebRequest.EndGetResponse(IAsyncResult asyncResult) at com.patten.silverlight.ViewModels.WebRequestLiteViewModel.b_0(IAsyncResult cb) at System.Net.Browser.BrowserHttpWebRequest.<>c_DisplayClassd.b__b(Object state2) at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object state) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx) at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem() at System.Threading.ThreadPoolWorkQueue.Dispatch() at System.Threading.ThreadPoolWaitCallback.PerformWaitCallback() InnerException: System.NotSupportedException Message=Specified method is not supported. StackTrace: at System.Net.Browser.BrowserHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult) at System.Net.Browser.BrowserHttpWebRequest.<>c_DisplayClass5.b_4(Object sendState) at System.Net.Browser.AsyncHelper.<>c_DisplayClass2.b__0(Object sendState) InnerException:
WORKING NOW
The exception that I was getting was due to a hack I use to get Fiddler to show the loop-back adapter i.e. http://localhost.:55437/Services/GetFoodDescriptionsLookup(2100), notice the extra DOT after the word localhost.
That was it!
I know what everyone is thinking, this could have been solved it I would have included some of these details.. like the debug flag that changes the URI, I removed most of the code to make it easier to read on SO, thinking that the problem was in the networking stack. Hard lesson(s) learned.
Thanks to everyone who took the time to get involved with this, both on stackoverflow and offline.
For completeness I have added the jQuery function that had me thinking this whole time I needed to be setting "content-type" instead of "accept" when in the SL4 application. (READ THE DOCS!)
function CallService(serviceUrl, data, callback) {
$.ajax({
url: serviceUrl,
data: data,
dataType: 'json',
contextType: "application/json",
cache: false,
success: callback,
error: function (msg) {
alert("Request Failed!");
}
});
}