views:

166

answers:

2

I'm invoking a REST service via http which returns a stream as the response. My client code looks like so:

                Uri uri = new Uri(remoteAddress);
                var webRequest = (HttpWebRequest)WebRequest.Create(uri);
                webRequest.Method = "GET";
                webRequest.ContentType = "multipart/mixed";
                webRequest.BeginGetResponse(responseResult =>
                {
                    HttpWebResponse response = (HttpWebResponse)webRequest.EndGetResponse(responseResult);
                    this.messages = ParseResponse(response);
                    Complete(false);
                }, null);

I'm getting the following error on this line:

 HttpWebResponse response = (HttpWebResponse)webRequest.EndGetResponse(responseResult);

System.Net.ProtocolViolationException was unhandled by user code Message=Operation is not valid due to the current state of the object. StackTrace: at System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state) at System.Net.Browser.BrowserHttpWebRequest.EndGetResponse(IAsyncResult asyncResult) at LaharSubProxy.SubscribeAsyncResult`1.<>c_DisplayClass1.b_0(IAsyncResult responseResult) 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:

This is driving me absolutely nuts since the exception is not pointing to anything useful. I have enabled the "streaming" option in fiddler but I'm not seeing any traffic/errors.

Please help!

A: 

Well whatdya know, the problem is this line:

webRequest.ContentType = "multipart/mixed";

Seems like this is a SL bug/nuance. I commented that line out and the error disappears! I should also add that the code as is works just fine in a traditional console/windows app.

This post pointed me the right direction. Thanks Microsoft!!!!!!

Abhijeet Patel
A: 

Setting the ContentType property implies that you are going to provide a message body with your request. It would seem that the .net libraries don't want to allow this, though I can find nothing in the HTTP spec to suggest that it should be fobidden.

Mark