简体   繁体   English

Silverlight 5中postAsync上的可移植Microsoft.Net.Http对象引用错误

[英]Portable Microsoft.Net.Http object reference error on postAsync in Silverlight 5

The code is very simple: 代码很简单:

var http = new HttpClient();
var uri = new Uri(Application.Current.Host.Source, "/api/query");
var postContext = new StringContent("test");
var response = await http.PostAsync(uri, postContext);

the error is : Object Null Reference 错误是:对象空引用

at System.Net.Http.DelegatingStream.get_CanSeek()
at System.Net.Http.StreamContent..ctor(Stream content, Int32 bufferSize)
at System.Net.Http.StreamContent..ctor(Stream content)
at System.Net.Http.HttpClientHandler.CreateResponseMessage(HttpWebResponse webResponse, HttpRequestMessage request)
at System.Net.Http.HttpClientHandler.TryGetExceptionResponse(WebException webException, HttpRequestMessage requestMessage, HttpResponseMessage& httpResponseMessage)
at System.Net.Http.HttpClientHandler.HandleAsyncException(RequestState state, Exception e)
at System.Net.Http.HttpClientHandler.GetResponseCallback(IAsyncResult ar)
at System.Net.Browser.BrowserHttpWebRequest.<>c__DisplayClass19.<InvokeGetResponseCallback>b__17(Object state2)
at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
at System.Threading.ThreadPoolWorkQueue.Dispatch()
at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()

GetAsync works very well. GetAsync效果很好。 But PostAsync crashes. 但是PostAsync崩溃了。

It's a bug in the Silverlight browser implementation of HttpWebRequest when a request fails (note HandleAsyncException in the stack). 请求失败(请注意堆栈中的HandleAsyncException)时,这是HttpWebRequest的Silverlight浏览器实现中的错误。 We have a workaround for it in a newer version of Microsoft.Net.Http which should be out soon. 我们已经在较新版本的Microsoft.Net.Http中提供了解决方法,该版本应该很快就会发布。

For now you can try using the client stack in SL which doesn't return a null stream. 现在,您可以尝试在SL中使用不返回空流的客户端堆栈。 http://msdn.microsoft.com/en-us/library/dd920295(v=VS.95).aspx http://msdn.microsoft.com/zh-CN/library/dd920295(v=VS.95).aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM