WCF消息模式基本内容简述

开发 开发工具
WCF消息模式在实际应用中是一个比较基础的操作技术。我们今天将会在这里为大家详细讲解有关这一模式的各种方法参数的内容等等。

WCF使用方式比较灵活,在程序员眼中,它占据着非常重要的地位。我们在这篇文章中将会为大家详细讲解一下有关WCF消息模式的相关应用方式,以方便大家在实际应用中能够获得一些帮助。

最简单就是WCF消息模式就是方法参数,所有的基本类型可以直接被序列化。我们还可以使用 MessageParameterAttribute 为参数定义消息名称。

[ServiceContract]  
public interface IContract  
{  
[OperationContract]  
double Add(double a, double b);  
[OperationContract]  
void Test([MessageParameter(Name="myString")]string s);  

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

对于WCF消息模式中的自定义类型,我们可以使用 DataContractAttribute 或 MessageContractAttribute 来定义,这些在前面的章节已经提过,此处不再做进一步说明。

WCF 服务方法支持 ref / out 关键字,也就是说底层引擎会重新为添加了关键字的对象赋予返回值。我们使用 "Message Logging" 和 "Service Trace Viewer" 查看一下 Reply Message。

Server.cs

[ServiceContract]  
public interface IContract  
{  
[OperationContract]  
double Add(double a, ref double b);  
}  
public class MyService : IContract  
{  
public double Add(double a, ref double b)  
{  
b += 2;  
return a + b;  
}  

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.

client.cs

using (ContractClient client = new ContractClient
(new BasicHttpBinding(),   
new EndpointAddress("http://localhost:8080/myservice")))   {   double b = 2;   double c = client.Add(1, ref b);   Console.WriteLine("c={0};b={1}", c, b);  
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

Reply Message

< MessageLogTraceRecord> 
< s:Envelope xmlns:s="http://..."> 
< s:Header> 
< Action s:mustUnderstand="1" xmlns="http://...">
http://tempuri.org/IContract/AddResponse< /Action>  < /s:Header>  < s:Body>  < AddResponse xmlns="http://tempuri.org/">  < AddResult>5< /AddResult>  < b>4< /b>  < /AddResponse>  < /s:Body>  < /s:Envelope>  < /MessageLogTraceRecord> 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.

在 Reply Message 中除了返回值 "AddResult" 外,还有 "b"。:-)

在进行WCF消息模式的处理时,需要注意的是,即便我们使用引用类型的参数,由于 WCF 采取序列化传送,因此它是一种 "值传递",而不是我们习惯的 "引用传递"。看看下面的例子,注意方法参数和数据结果的不同。

Server.cs

[DataContract]  
public class Data  
{  
[DataMember]  
public int I;  
}  
[ServiceContract]  
public interface IContract  
{  
[OperationContract]  
void Add(Data d);  
[OperationContract]  
void Add2(ref Data d);  
}  
public class MyService : IContract  
{  
public void Add(Data d)  
{  
d.I += 10;  
}  
public void Add2(ref Data d)  
{  
d.I += 10;  
}  

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.

Client.cs

using (ContractClient client = 
new ContractClient(new BasicHttpBinding(),   
new EndpointAddress("http://localhost:8080/myservice")))   {   Data d = new Data();   d.I = 1;   client.Add(d);   Console.WriteLine("d.I={0}", d.I);     Data d2 = new Data();   d2.I = 1;   client.Add2(ref d2);   Console.WriteLine("d2.I={0}", d2.I);  
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.

输出:

d.I=1

d2.I=11

以上就是对WCF消息模式的相关介绍。

【编辑推荐】

  1. WCF调用服务异常基本解决方案介绍
  2. WCF体系结构基本概念分享
  3. 各种常用WCF术语内容总结
  4. WCF特点具体优势总结
  5. WCF安全参数相关设置方法详解
责任编辑:曹凯 来源: 博客园
相关推荐

2010-02-25 17:51:04

WCF服务地址

2010-03-02 15:53:02

WCF服务合同

2010-03-02 17:55:37

WCF终结点地址

2010-02-25 17:04:54

WCF实例上下文

2010-01-04 15:21:37

Silverlight

2010-04-22 09:36:56

Oracle数据字典

2010-01-28 15:33:37

Android程序架构

2010-03-05 11:53:20

Python命名约定

2017-10-25 06:50:27

数据科学数据数据分析

2010-03-01 14:50:30

WCF行为类型

2010-02-05 10:08:55

C++名字空间

2010-02-06 13:58:13

C++ Bost库

2010-03-02 13:14:38

WCF MSMQ队列

2010-02-04 15:51:07

C++迭代器

2010-03-03 15:26:54

Python编码规范

2010-04-12 12:52:54

WiMAX无线技术

2010-05-11 14:19:52

MySQL 5.0

2009-09-10 10:47:05

C# form

2010-02-03 15:06:02

C++可变参数表

2010-02-02 15:12:09

C++ explici
点赞
收藏

51CTO技术栈公众号