wcf - 即使在调用 'void' 异步方法时,后退按钮上的“WebException”错误

标签 wcf windows-phone-7 silverlight-toolkit

我有一个允许用户与之交互的 Windows Phone 应用程序。每次交互都将始终导致异步 WCF 调用。
除此之外,一些交互会导致打开浏览器、 map 、电子邮件等......

问题是,当点击后退按钮时,我有时会收到以下错误"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) 之前打开了另一个应用程序(浏览器、 map 等)。 .很公平...

真正令人讨厌的是,它似乎应该通过克隆服务器端方法来解决,只是使它成为 void与以下运营契约(Contract) [OperationContract(IsOneWay = true)]但我仍然收到错误消息。

更糟糕的是,异常是在系统生成的代码部分中引发的,因此无法手动捕获,从而导致应用程序崩溃。

我只是不明白执行 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)
{
}

最佳答案

唯一能保证该消息是 已发货单向绑定(bind)是打开可靠消息传递并确保没有通信错误(错误的地址、绑定(bind)等)。这是支持可靠消息传递的绑定(bind)列表:

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>

关于wcf - 即使在调用 'void' 异步方法时,后退按钮上的“WebException”错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9959825/

相关文章:

wcf - 如何在 WCF 流模式下获取 Stream 上的 Seek 方法?

WCF 接收带有 SWA 负载的请求

Silverlight JetPack 主题工具包错误

c# - 滚动时 WP7 列表框项目不同步

wcf - Schema First WCF 开发

c# - 为什么处置/关闭 WCF 客户端代理很重要

windows-phone-7 - WP7 RTM 模拟器在右侧显示帧计数器和 dx 信息 - 这是默认设置吗?

windows-phone-7 - 尝试绑定(bind)独立存储镜像时应用程序崩溃

windows-phone-7 - 更改 UI 以响应 Storyboard.Completed 事件。我们需要 BeginInvoke 吗?

windows-phone-7 - 将地理坐标分配给图钉