2
votes

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!

2

2 Answers

1
votes

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!!!!!!

0
votes

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.