我有一个允许用户与之互动的Windows手机应用。每次互动都将始终导致异步WCF呼叫 除此之外,一些互动将导致打开浏览器,地图,电子邮件等......
问题是,当按下后退按钮时,我有时会收到以下错误
"An error (WebException) occurred while transmitting data over the HTTP channel."
具有以下堆栈跟踪:
at System.ServiceModel.Channels.HttpChannelUtilities.ProcessGetResponseWebException(WebException webException, HttpWebRequest request, HttpAbortReason abortReason)
at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelAsyncRequest.CompleteGetResponse(IAsyncResult result)
at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelAsyncRequest.OnGetResponse(IAsyncResult result)
at System.Net.Browser.ClientHttpWebRequest.<>c__DisplayClassa.<InvokeGetResponseCallback>b__8(Object state2)
at System.Threading.ThreadPool.WorkItem.WaitCallback_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadPool.WorkItem.doWork(Object o)
at System.Threading.Timer.ring()
我的理解是,它正在发生,因为我的应用程序在有时间执行EndMyAsyncMethod(System.IAsyncResult result)
之前打开了另一个应用程序(浏览器,地图等)。足够公平......
真正令人讨厌的是它似乎应该通过克隆服务器端方法来解决,只能使用以下操作契约void
使其[OperationContract(IsOneWay = true)]
,但我仍然会收到错误。< / p>
更糟糕的是,异常会在系统生成的代码中抛出,因此无法手动捕获,导致应用程序崩溃。
我只是不明白在明确标记为Endxxx
和OneWay
时执行void
方法的必要性。
修改
我确实发现了类似的问题here。它似乎与到达服务的消息(而不是客户端回调)有关。我的下一个问题是:
如果我现在正在调用标记为AsyncPattern
和OneWay
的方法,那么我应该在客户端上等待确定消息是否已成功传输?
这是新的服务定义:
[OperationContract(IsOneWay = true, AsyncPattern = true)]
IAsyncResult BeginCacheQueryWithoutCallback(string param1, QueryInfoDataContract queryInfo, AsyncCallback cb, Object s);
void EndCacheQueryWithoutCallback(IAsyncResult r);
实施:
public IAsyncResult BeginCacheQueryWithoutCallback(string param1, QueryInfoDataContract queryInfo, AsyncCallback cb, Object s)
{
// do some stuff
return new CompletedAsyncResult<string>("");
}
public void EndCacheQueryWithoutCallback(IAsyncResult r)
{
}
答案 0 :(得分:0)
唯一可以保证消息已经传递用于单向绑定的唯一方法是打开可靠的消息传递并确保没有通信错误(错误的地址,绑定等)这是支持可靠消息传递的绑定列表:
basicHttpBinding - RM not supported
wsHttpBinding - RM supported, not default
wsDualHttpBinding - RM implicit
netTcpBinding - RM supported, not default
例如对于tcpBinding:
<binding>
<netTcpBinding>
<binding name="MyTcpBinding">
<reliableSession enabled="true" />
</binding>
</netTcpBinding>
</binding>
或者:
<customHttpBinding>
<binding configurationName="customReliableBinding">
<reliableSession ordered="true" />
</binding>
</customBinding>