C# 带有BeginGetRequestStream的InvalidOperationException
我遇到以下错误:C# 带有BeginGetRequestStream的InvalidOperationException,c#,.net,windows-phone-7,windows-phone-8,C#,.net,Windows Phone 7,Windows Phone 8,我遇到以下错误: System.InvalidOperationException was unhandled by user code HResult=-2146233079 Message=Operation is not valid due to the current state of the object. Source=System.Windows StackTrace: at System.Net.Browser.ClientHttpWebReques
System.InvalidOperationException was unhandled by user code
HResult=-2146233079
Message=Operation is not valid due to the current state of the object.
Source=System.Windows
StackTrace:
at System.Net.Browser.ClientHttpWebRequest.InternalEndGetRequestStream(IAsyncResult asyncResult)
at System.Net.Browser.ClientHttpWebRequest.EndGetRequestStream(IAsyncResult asyncResult)
at SophosMobileControl.MainPage.<>c__DisplayClass5.<PushTestButton_Click>b__3(IAsyncResult ar)
at System.Net.Browser.ClientHttpWebRequest.<>c__DisplayClass18.<InvokeGetRequestStreamCallback>b__16(Object state2)
InnerException:
用户代码未处理System.InvalidOperationException
HResult=-2146233079
Message=由于对象的当前状态,操作无效。
Source=System.Windows
堆栈跟踪:
位于System.Net.Browser.ClientHttpWebRequest.InternalEndGetRequestStream(IAsyncResult asyncResult)
位于System.Net.Browser.ClientHttpWebRequest.EndGetRequestStream(IAsyncResult asyncResult)
在SophosMobileControl.MainPage.c__显示Class5.b___3(IAsyncResult ar)
在System.Net.Browser.ClientHttpWebRequest.c__中显示Class18.b__16(对象状态2)
内部异常:
使用以下代码:
// Get the URI that the Microsoft Push Notification Service returns to the push client when creating a notification channel.
// Normally, a web service would listen for URIs coming from the web client and maintain a list of URIs to send
// notifications out to.
string subscriptionUri = (App.Current.Resources["Locator"] as ViewModelLocator).Main.PushUri;
HttpWebRequest sendNotificationRequest = (HttpWebRequest)WebRequest.Create(subscriptionUri);
// Create an HTTPWebRequest that posts the toast notification to the Microsoft Push Notification Service.
// HTTP POST is the only method allowed to send the notification.
sendNotificationRequest.Method = "POST";
// Create the toast message.
string toastMessage = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<wp:Notification xmlns:wp=\"WPNotification\">" +
"<wp:Toast>" +
"<wp:Text1>" + "Title" + "</wp:Text1>" +
"<wp:Text2>" + "Subtitle" + "</wp:Text2>" +
"</wp:Toast> " +
"</wp:Notification>";
// Set the notification payload to send.
byte[] notificationMessage = Encoding.UTF8.GetBytes(toastMessage);
// Set the web request content length.
sendNotificationRequest.ContentLength = notificationMessage.Length;
sendNotificationRequest.ContentType = "text/xml";
//sendNotificationRequest.Headers["X-WindowsPhone-Target"] = "toast";
//sendNotificationRequest.Headers["X-NotificationClass"] = "2";
sendNotificationRequest.BeginGetRequestStream(ar =>
{
using (Stream postStream = sendNotificationRequest.EndGetRequestStream(ar))
{
postStream.Write(notificationMessage, 0, notificationMessage.Length);
}
}, sendNotificationRequest);
sendNotificationRequest.BeginGetRequestStream(ar =>
{
using (Stream postStream = sendNotificationRequest.EndGetRequestStream(ar))
{
postStream.Write(notificationMessage, 0, notificationMessage.Length);
}
}, sendNotificationRequest);
//获取Microsoft推送通知服务在创建通知通道时返回给推送客户端的URI。
//通常,web服务将侦听来自web客户端的URI,并维护要发送的URI列表
//通知发送到。
字符串subscriptionUri=(App.Current.Resources[“Locator”]作为ViewModelLocator);
HttpWebRequest sendNotificationRequest=(HttpWebRequest)WebRequest.Create(subscriptionUri);
//创建一个HTTPWebRequest,将toast通知发布到Microsoft推送通知服务。
//HTTP POST是唯一允许发送通知的方法。
sendNotificationRequest.Method=“POST”;
//创建toast消息。
字符串toastMessage=“”+
"" +
"" +
“+”标题“+”+
“+”副标题“+”
" " +
"";
//设置要发送的通知有效负载。
byte[]notificationMessage=Encoding.UTF8.GetBytes(toastMessage);
//设置web请求内容长度。
sendNotificationRequest.ContentLength=notificationMessage.Length;
sendNotificationRequest.ContentType=“text/xml”;
//sendNotificationRequest.Headers[“X-WindowsPhone-Target”]=“toast”;
//sendNotificationRequest.Headers[“X-NotificationClass”]=“2”;
sendNotificationRequest.BeginGetRequestStream(ar=>
{
使用(Stream postStream=sendNotificationRequest.EndGetRequestStream(ar))
{
Write(notificationMessage,0,notificationMessage.Length);
}
},发送通知请求);
我的解决方案是更改以下代码:
// Get the URI that the Microsoft Push Notification Service returns to the push client when creating a notification channel.
// Normally, a web service would listen for URIs coming from the web client and maintain a list of URIs to send
// notifications out to.
string subscriptionUri = (App.Current.Resources["Locator"] as ViewModelLocator).Main.PushUri;
HttpWebRequest sendNotificationRequest = (HttpWebRequest)WebRequest.Create(subscriptionUri);
// Create an HTTPWebRequest that posts the toast notification to the Microsoft Push Notification Service.
// HTTP POST is the only method allowed to send the notification.
sendNotificationRequest.Method = "POST";
// Create the toast message.
string toastMessage = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<wp:Notification xmlns:wp=\"WPNotification\">" +
"<wp:Toast>" +
"<wp:Text1>" + "Title" + "</wp:Text1>" +
"<wp:Text2>" + "Subtitle" + "</wp:Text2>" +
"</wp:Toast> " +
"</wp:Notification>";
// Set the notification payload to send.
byte[] notificationMessage = Encoding.UTF8.GetBytes(toastMessage);
// Set the web request content length.
sendNotificationRequest.ContentLength = notificationMessage.Length;
sendNotificationRequest.ContentType = "text/xml";
//sendNotificationRequest.Headers["X-WindowsPhone-Target"] = "toast";
//sendNotificationRequest.Headers["X-NotificationClass"] = "2";
sendNotificationRequest.BeginGetRequestStream(ar =>
{
using (Stream postStream = sendNotificationRequest.EndGetRequestStream(ar))
{
postStream.Write(notificationMessage, 0, notificationMessage.Length);
}
}, sendNotificationRequest);
sendNotificationRequest.BeginGetRequestStream(ar =>
{
using (Stream postStream = sendNotificationRequest.EndGetRequestStream(ar))
{
postStream.Write(notificationMessage, 0, notificationMessage.Length);
}
}, sendNotificationRequest);
通过以下方式:
sendNotificationRequest.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), sendNotificationRequest);
void GetRequestStreamCallback(IAsyncResult callbackResult)
{
HttpWebRequest myRequest = (HttpWebRequest)callbackResult.AsyncState;
// End the stream request operation
Stream postStream = myRequest.EndGetRequestStream(callbackResult);
// Add the post data to the web request
postStream.Write(notificationMessage, 0, notificationMessage.Length);
postStream.Close();
}