自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

WCF消息模式基本內(nèi)容簡述

開發(fā) 開發(fā)工具
WCF消息模式在實際應用中是一個比較基礎的操作技術。我們今天將會在這里為大家詳細講解有關這一模式的各種方法參數(shù)的內(nèi)容等等。

WCF使用方式比較靈活,在程序員眼中,它占據(jù)著非常重要的地位。我們在這篇文章中將會為大家詳細講解一下有關WCF消息模式的相關應用方式,以方便大家在實際應用中能夠獲得一些幫助。

最簡單就是WCF消息模式就是方法參數(shù),所有的基本類型可以直接被序列化。我們還可以使用 MessageParameterAttribute 為參數(shù)定義消息名稱。

  1. [ServiceContract]  
  2. public interface IContract  
  3. {  
  4. [OperationContract]  
  5. double Add(double a, double b);  
  6. [OperationContract]  
  7. void Test([MessageParameter(Name="myString")]string s);  

對于WCF消息模式中的自定義類型,我們可以使用 DataContractAttribute 或 MessageContractAttribute 來定義,這些在前面的章節(jié)已經(jīng)提過,此處不再做進一步說明。

WCF 服務方法支持 ref / out 關鍵字,也就是說底層引擎會重新為添加了關鍵字的對象賦予返回值。我們使用 "Message Logging" 和 "Service Trace Viewer" 查看一下 Reply Message。

Server.cs

  1. [ServiceContract]  
  2. public interface IContract  
  3. {  
  4. [OperationContract]  
  5. double Add(double a, ref double b);  
  6. }  
  7. public class MyService : IContract  
  8. {  
  9. public double Add(double a, ref double b)  
  10. {  
  11. b += 2;  
  12. return a + b;  
  13. }  

client.cs

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

Reply Message

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

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

在進行WCF消息模式的處理時,需要注意的是,即便我們使用引用類型的參數(shù),由于 WCF 采取序列化傳送,因此它是一種 "值傳遞",而不是我們習慣的 "引用傳遞"。看看下面的例子,注意方法參數(shù)和數(shù)據(jù)結(jié)果的不同。

Server.cs

  1. [DataContract]  
  2. public class Data  
  3. {  
  4. [DataMember]  
  5. public int I;  
  6. }  
  7. [ServiceContract]  
  8. public interface IContract  
  9. {  
  10. [OperationContract]  
  11. void Add(Data d);  
  12. [OperationContract]  
  13. void Add2(ref Data d);  
  14. }  
  15. public class MyService : IContract  
  16. {  
  17. public void Add(Data d)  
  18. {  
  19. d.I += 10;  
  20. }  
  21. public void Add2(ref Data d)  
  22. {  
  23. d.I += 10;  
  24. }  

Client.cs

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

輸出:

d.I=1

d2.I=11

以上就是對WCF消息模式的相關介紹。

【編輯推薦】

  1. WCF調(diào)用服務異常基本解決方案介紹
  2. WCF體系結(jié)構基本概念分享
  3. 各種常用WCF術語內(nèi)容總結(jié)
  4. WCF特點具體優(yōu)勢總結(jié)
  5. WCF安全參數(shù)相關設置方法詳解
責任編輯:曹凱 來源: 博客園
相關推薦

2010-02-25 17:51:04

WCF服務地址

2010-03-02 15:53:02

WCF服務合同

2010-03-02 17:55:37

WCF終結(jié)點地址

2010-02-25 17:04:54

WCF實例上下文

2010-01-04 15:21:37

Silverlight

2010-04-22 09:36:56

Oracle數(shù)據(jù)字典

2010-01-28 15:33:37

Android程序架構

2010-03-05 11:53:20

Python命名約定

2017-10-25 06:50:27

數(shù)據(jù)科學數(shù)據(jù)數(shù)據(jù)分析

2010-03-02 13:14:38

WCF MSMQ隊列

2010-03-01 14:50:30

WCF行為類型

2010-02-05 10:08:55

C++名字空間

2010-02-06 13:58:13

C++ Bost庫

2010-02-04 15:51:07

C++迭代器

2010-03-03 15:26:54

Python編碼規(guī)范

2010-05-11 14:19:52

MySQL 5.0

2010-04-12 12:52:54

WiMAX無線技術

2009-09-10 10:47:05

C# form

2010-02-02 15:12:09

C++ explici

2010-02-05 13:35:19

C++虛析構函數(shù)
點贊
收藏

51CTO技術棧公眾號