CommunicationException在一分钟后发生(在从windows phone 8应用程序调用wcf服务期间
本文关键字:应用程序 phone 调用 wcf 服务期 windows 在从 一分钟 CommunicationException | 更新日期: 2023-09-27 18:15:39
我已经实现了以下WCF服务:
namespace TeaService
{
public class TeaService : ITeaService
{
public string PrepareTea(string tea)
{
System.Threading.Thread.Sleep(61000);
return "A nice cup of " + tea + " tea will be ready in a few minutes.";
}
}
}
服务使用默认的basichttpbinding,并且绑定配置是这样配置的:
<bindings>
<basicHttpBinding>
<binding openTimeout="00:05:00" receiveTimeout="00:05:00" sendTimeout="00:05:00" closeTimeout="00:05:00"></binding>
</basicHttpBinding>
</bindings>
即所有超时值设置为5分钟。
Windows Phone 8客户端应用程序调用服务:
namespace TeaClient
{
public partial class MainPage : PhoneApplicationPage
{
public MainPage()
{
InitializeComponent();
var client = new TeaServiceClient();
client.PrepareTeaCompleted += Client_PrepareTeaCompleted;
client.PrepareTeaAsync("Rooibos");
}
private void Client_PrepareTeaCompleted(object sender, PrepareTeaCompletedEventArgs e)
{
tb.Text = e.Result;
}
}
}
"tb"是xaml视图中定义的一个文本框。
在ServicesReferences。ClientConfig中,basicHttpBinding的超时值设置如下:
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_ITeaService" maxBufferSize="2147483647"
maxReceivedMessageSize="2147483647" openTimeout="00:05:00" receiveTimeout="00:05:00" sendTimeout="00:05:00" closeTimeout="00:05:00">
<security mode="None" />
</binding>
</basicHttpBinding>
</bindings>
问题:一分钟后客户端抛出了一个CommunicationException。
$exception {System.ServiceModel.CommunicationException: The remote server returned an error: NotFound. ---> System.Net.WebException: The remote server returned an error: NotFound. ---> System.Net.WebException: The remote server returned an error: NotFound.
at System.Net.Browser.ClientHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)
at System.Net.Browser.ClientHttpWebRequest.<>c__DisplayClasse.<EndGetResponse>b__d(Object sendState)
at System.Net.Browser.AsyncHelper.<>c__DisplayClass1.<BeginOnUI>b__0(Object sendState)
--- End of inner exception stack trace ---
at System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state)
at System.Net.Browser.ClientHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelAsyncRequest.CompleteGetResponse(IAsyncResult result)
--- End of inner exception stack trace ---
at System.ServiceModel.AsyncResult.End[TAsyncResult](IAsyncResult result)
at System.ServiceModel.Channels.ServiceChannel.EndCall(String action, Object[] outs, IAsyncResult result)
at System.ServiceModel.ClientBase`1.ChannelBase`1.EndInvoke(String methodName, Object[] args, IAsyncResult result)
at TeaClient.TeaService.TeaServiceClient.TeaServiceClientChannel.EndPrepareTea(IAsyncResult result)
at TeaClient.TeaService.TeaServiceClient.TeaClient.TeaService.ITeaService.EndPrepareTea(IAsyncResult result)
at TeaClient.TeaService.TeaServiceClient.OnEndPrepareTea(IAsyncResult result)
at System.ServiceModel.ClientBase`1.OnAsyncCallCompleted(IAsyncResult result)} System.Exception {System.ServiceModel.CommunicationException}
我不明白为什么会这样。如果我在WPF桌面应用程序中实现完全相同的特定于WCF客户机的代码,则不会抛出异常。我可以确认服务工作正常,只要我删除Thread.Sleep(61000), windows phone应用程序也能正常工作。在我的"真实世界"生产场景中(这个简化的示例所反映的),客户端必须能够等待超过一分钟而不抛出CommunicationException。因为如果我在WPF应用程序中做同样的事情,这个例子就能正常工作,所以我怀疑这个问题与Windows Phone平台的限制有关。但是我找不到任何信息表明WCF调用在Windows Phone上不能超过一分钟。
此外,我尝试在客户端代理上设置OperationTimeout,如下所示:
client.InnerChannel.OperationTimeout = TimeSpan.FromMinutes(5.0);
但是运气不好。欢迎提出任何建议。
编辑:nvoigt标记的重复问题与HttpClient有关。这个问题是关于使用BasicHttpBinding的WCF客户端代理的。毫无疑问,潜在的问题是完全相同的,因此我们得出结论,这是一个平台限制。
Windows Phone应用程序没有app.config文件,所以指定的超时都不会应用。相反,您需要在客户端实例上设置它,像这样:
var client = new TeaServiceClient();
client.ChannelFactory.Endpoint.Binding.SendTimeout = new TimeSpan.FromMinutes(5);