我已经实现了以下 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>
也就是说,所有超时值都设置为五分钟。
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 客户端代理。毫无疑问,根本问题完全相同,因此我们得出结论,这是一个平台限制。