您的位置:首页 > 其它

那些年,我们一起学WCF--(3)消息通信模式

2012-09-19 11:48 507 查看
这一节,大家共同研究下WCF消息的通信模式,大体上来说,WCF共有3中通信模式
1.请求答复模式

2.单程模式

3.双工模式

接下来,我们一一研究.



1. 请求答复模式

请求答复模式是我们最常见和最常用的模式,WCF通信默认就采用的这种模式。这种模式就是客户端向服务器端发送消息后,需要等待服务器端执行完成,并返回结果

,客户端才能继续向下执行。

服务器代码

[ServiceContract]
public interface IService1
{

[OperationContract]
string GetData(int value);

[OperationContract]
CompositeType GetDataUsingDataContract(CompositeType composite);

// TODO: 在此添加您的服务操作
}


public class Service1 : IService1
{
public string GetData(int value)
{
System.Threading.Thread.Sleep(8000);
return string.Format("You entered: {0}", value);
}
}

客户端:

private void button1_Click(object sender, EventArgs e)
{
//请求响应模式
//等待服务器端执行完毕,客户端才继续向下执行
SingleService.Service1Client client = new SingleService.Service1Client();
string result = client.GetData(3);
MessageBox.Show(result);
}


2. 单程模式

单程模式是客户端向服务器端发送消息后,并不需要等待服务器端执行完成和返回结果,客户端就可以继续向下执行。

使用单程模式的时候,要把操作契约的属性isoneway设为true. [OperationContract(IsOneWay=true)]

单程模式的操作契约的返回值都为void,单程契约的方法参数中不能使用ref,out标记。

服务器端契约

[OperationContract(IsOneWay=true)]
void SingleGetData(string name);


客户端

private void button2_Click(object sender, EventArgs e)
{
//单程模型
//不用等待服务器端,直接直接向下执行
SingleService.Service1Client client = new SingleService.Service1Client();
client.SingleGetData("hello");
MessageBox.Show("hello world");
}

3.双工模式

双工模式就是客户端可以向服务器端发送信息,服务器端也可以向客户端发送信息,可以理解为客户端可以调用服务器端的方法,服务器端也可以

调用客户端的方法。双工模式要在契约中声明回调契约,服务器端通过回调契约调用客户端方法。声明回调契约用CallBackContract

双工模式实际上可以分为单程双工模式和请求答复双工模式

3.1 单程双工模式

服务器端契约

[ServiceContract(CallbackContract = typeof(ICallBackDuplService))]
public interface IDuplService
{
[OperationContract]
void DoWork();

[OperationContract(IsOneWay=true)]
void GetDuplData(string data);

}

public interface ICallBackDuplService
{
[OperationContract(IsOneWay=true)]
void ShowMsg(string msg);

}


public class DuplService : IDuplService
{
public void DoWork()
{
}

#region IDuplService 成员

public void GetDuplData(string data)
{
string str="欢迎使用双工" + data;
//获取当前操作的上下文
ICallBackDuplService callback = OperationContext.Current.GetCallbackChannel<ICallBackDuplService>();
callback.ShowMsg(str);
}
}

客户端代码

private void button3_Click(object sender, EventArgs e)
{
InstanceContext instanceContext = new InstanceContext(new CallBackDuplService());
DuplService.DuplServiceClient client = new DuplService.DuplServiceClient(instanceContext);
client.GetDuplData("Hello world");
}


public class CallBackDuplService:DuplService.IDuplServiceCallback
{

#region IDuplServiceCallback 成员

public void ShowMsg(string msg)
{
//显示数据
System.Windows.Forms.MessageBox.Show(msg);
}

#endregion

}


3.2请求答复双工模式

服务器端契约

[ServiceContract(CallbackContract = typeof(ICallBackDuplService))]
public interface IDuplService
{
[OperationContract]
void DoWork();

[OperationContract(IsOneWay = true)]
void HelloDupl(string name);
}

public interface ICallBackDuplService
{

[OperationContract]
void ConsoleMsg(string msg);
}


[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Reentrant)]
public class DuplService : IDuplService

{
#region IDuplService 成员

public void HelloDupl(string name)
{
string str = "双工通信模式2"+name;
ICallBackDuplService callback = OperationContext.Current.GetCallbackChannel<ICallBackDuplService>();
callback.ConsoleMsg(str);
}

#endregion
}


客户端

private void button4_Click(object sender, EventArgs e)
{
InstanceContext instanceContext = new InstanceContext(new CallBackDuplService());
DuplService.DuplServiceClient client = new DuplService.DuplServiceClient(instanceContext);
client.HelloDupl("请求答复");
}


public class CallBackDuplService:DuplService.IDuplServiceCallback
{

#region IDuplServiceCallback 成员

public void ShowMsg(string msg)
{
//显示数据
System.Windows.Forms.MessageBox.Show(msg);
}

#endregion

#region IDuplServiceCallback 成员

public void ConsoleMsg(string msg)
{
System.Windows.Forms.MessageBox.Show(msg);
}

#endregion
}


demo:http://download.csdn.net/detail/zx13525079024/4580027
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: