Welcome

首页 / 软件开发 / .NET编程技术 / 我的WCF之旅(6):在Winform Application中调用Duplex Service出现TimeoutExce

我的WCF之旅(6):在Winform Application中调用Duplex Service出现TimeoutExce2010-12-27 cnblogs.com artech我的WCF之旅(6):在Winform Application中调用Duplex Service出现TimeoutException原因和解决

几个星期之前写了一篇关于如何通过WCF进行 双向通信的文章([原创]我的WCF之旅(3):在WCF中实现双向通信(Bi-directional Communication) ),在文章中我提供了一个如果在Console Application 调用Duplex WCF Service的Sample。前几天有个网友在上面留言说,在没有做任何改动得情况下,把 作为Client的ConsoleApplication 换成Winform Application,运行程序的时候总是出现Timeout的错误。我觉得这是一个很好的问题,通过这个问题,我们可以更加深入地理解WCF的消息交换的机制。

1.问题重现

首先我们来重现这个错误,在这里我只写WinForm的代码,其他的内容请参考我的文章。Client端的Proxy Class(DuplexCalculatorClient)的定义没有任何变化。我们先来定义用于执行回调操作(Callback)的类——CalculatorCallbackHandler.cs。代码很简单,就是通过Message Box的方式显示运算的结果。

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using Artech.DuplexWCFService.Contract;
using System.ServiceModel;

namespace Artech. WCFService.Client
{
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple)]
public class CalculatorCallbackHandler : ICalculatorCallback
{
ICalculatorCallback Members#region ICalculatorCallback Members
public void ShowResult(double x, double y, double result)
{
MessageBox.Show(string.Format("x + y = {2} where x = {0} and {1}", x, y, result),"Result", MessageBoxButtons.OK, MessageBoxIcon.Information);
}

#endregion
}
}

接着我们来设计我们的UI,很简单,无需多说。