频道直达 - 专题 - 新闻 - 技巧 - 组网 - 开发 - 安全 - web编程 - 图像 - 操作系统 - 数据库 - 教育 - 旅游 - 健康 - 时尚 - 驱动 - 软件 - 游戏 - 多媒体 - ERP - 讨论组

WCF体验之旅(6):在Winform Application中调用Duplex Service出现Timeout Exception的原因和解决方案

来源:博客园 作者:Artech 出处:巧巧读书 2008-03-18 进入讨论组
下一页 1 2 3 

【引自Artech的博客】 几个星期之前写了一篇关于如何通过WCF进行 双向通信的文章([原创]我的WCF之旅(3):在WCF中实现双向通信(Bi-directional Communication) ),在文章中我提供了一个如果在Console Application 调用Duplex WCF Service的Sample。

前几天有个网友在上面留言说,在没有做任何改动得情况下,把 作为Client的Console Application 换成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,很简单,无需多说。

WCF体验之旅(6):在Winform Application中调用Duplex Service出现Timeout Exception的原因和解决方案(图一)
图1

代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace Artech. WCFService.Client
{
public partial class Form1 : Form
{
private DuplexCalculatorClient _calculator;
private double _op1;
private double _op2;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this._calculator = new DuplexCalculatorClient(

new System.ServiceModel.InstanceContext(new CalculatorCallbackHandler()));
}
private void Calculate()
{
this._calculator.Add(this._op1, this._op2);
}
private void buttonCalculate_Click(object sender, EventArgs e)
{
if (!double.TryParse(this.textBoxOp1.Text.Trim(), out this._op1))
{
MessageBox.Show("Please enter a valid number","Error",

MessageBoxButtons.OK,MessageBoxIcon.Error);
this.textBoxOp1.Focus();
}
if (!double.TryParse(this.textBoxOp2.Text.Trim(), out this._op2))
{
MessageBox.Show("Please enter a valid number","Error",

MessageBoxButtons.OK,MessageBoxIcon.Error);
this.textBoxOp1.Focus();
}
try
{
this.Calculate();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK,

MessageBoxIcon.Error);
}
}
}
}

启动Host,然后随启动Client,在两个Textbox中输入数字2和3,Click Calculate按钮,随后整个UI被锁住,无法响应用户操作。一分后,出现下面的错误。

静态页面:http://www.qqread.com/net-case/j401805.html 更多文章 更多内容请看数字化校园网解决方案大型实用解决方案专题应用解决方案专题,或进入讨论组讨论。
下一页 1 2 3 
收藏此文】【 】【打印】【关闭
相关图文阅读
频道图文推荐
健 康 咨 询
时 尚 咨 询
巧巧读书宗旨
相关专题
讨论组问题推荐
站内各频道最新更新文档
站内最新制作专题
热门关键字导读
Photoshop教 程照片处理 照片制作 PS快捷键 抠图
计 算 机 故 障XP系统修复
艺 术 与 设 计设计 流媒体 设计欣赏 边框
计 算 机 安 全ARP
站内频道文章精选
巧巧电脑频道编辑信箱  告诉我们您想看的专题或文章