WCF使用Header如何正确实现

开发 开发工具
我们如果想在WCF中使用自定义的Header的话,应该如何进行正确的操作呢?在这里大家就可以充分的掌握到WCF使用Header的相关技巧。

对于一个经验丰富的编程人员来说,它不可能不知道WCF为何物。作为一个.NET Framework 3.5的重要组成部件,为我们带来了非常大的好处。我们在这里先来了解一下WCF使用Header的相关应用技巧。

在WCF中如何实现登陆,典型的场景如下:

 

 

 

  1. [ServiceContract]  
  2. public interface ILogin {  
  3. [OperationContract]  
  4. bool Signin(string userName, string password);  
  5. }  
  6. [ServiceContract]  
  7. public interface IBizTest {  
  8. [OperationContract]  
  9. string GetWelcomeInfo();  

 

千万别从WCF自带的那个InstanceContextMode来想办法,因为WCF中的PerSession调用只是针对每个服务类而言的,除非你变态到服务端只有一个类来实现全部的接口;#t#

变个思路,能不能用类似.NET Remoting中的CallContext呢?但是查了一下WCF的手册,好像也没有这么个东西,怎么解决呢?那就是Custom header.

解决方案提出前,需要知道一点的就是,服务端取客户端送出的Header的方法:

先遍历OperationContext.Current.IncomingMessageHeaders找出客户端发送的Header Name,然后再用 OperationContext.Current.IncomingMessageHeaders.GetHeader<T>(i)得到值就可以啦。

下面的问题就剩下客户端怎么发送Custom Header了。

策略1:在每个客户端Proxy中增加类似如下的代码

 

  1. using (OperationContextScope scope = new 
    OperationContextScope(InnerChannel)) {  
  2. MessageHeader mh = MessageHeader.CreateHeader("HeaderName", 
    string.Empty, "HeaderValue");  
  3. OperationContext.Current.OutgoingMessageHeaders.Add(mh);  
  4. //…  
  5. }  
  6.  

 

 

但是每个客户端都要增加,这样的WCF使用Header的步骤太麻烦了,所以,引出

2.自定义一个CallContextAttribute,代码如下:

1. 先定义一个IClientMessageInspector接口的实现类

  1. public class ContextHeader : IClientMessageInspector {  
  2. public void AfterReceiveReply(ref System.ServiceModel.
    Channels.Message reply, object correlationState) {  
  3. //  
  4. }  
  5. public object BeforeSendRequest(ref System.ServiceModel.
    Channels.Message request, IClientChannel channel) {  
  6. MessageHeader clientHeader = MessageHeader.CreateHeader
    ("headerName", string.Empty, "headerValue");  
  7. request.Headers.Add(clientHeader);  
  8. return null;  
  9. }  

 

 

OK , 然后就可以实现CallContextAttribute了

  1. public class CallContextAttribute : Attribute, IEndpointBehavior,
     IOperationBehavior {  
  2. IEndpointBehavior Members#region IEndpointBehavior Members  
  3. public void AddBindingParameters(ServiceEndpoint endpoint, 
    BindingParameterCollection bindingParameters) {  
  4. }  
  5. public void ApplyClientBehavior(ServiceEndpoint endpoint, 
    ClientRuntime clientRuntime) {  
  6. clientRuntime.MessageInspectors.Add(new ContextHeader());  
  7. }  
  8. public void ApplyDispatchBehavior(ServiceEndpoint endpoint, 
    EndpointDispatcher endpointDispatcher) {  
  9. }  
  10. public void Validate(ServiceEndpoint endpoint) {  
  11. }  
  12. #endregion  
  13. IOperationBehavior Members#region IOperationBehavior Members  
  14. public void AddBindingParameters(OperationDescription operationDescription, 
    BindingParameterCollection bindingParameters) {  
  15. }  
  16. public void ApplyClientBehavior(OperationDescription operationDescription,
     ClientOperation clientOperation) {  
  17. clientOperation.Parent.MessageInspectors.Add(new ContextHeader ());  
  18. }  
  19. public void ApplyDispatchBehavior(OperationDescription operationDescription, 
    DispatchOperation dispatchOperation) {  
  20. }  
  21. public void Validate(OperationDescription operationDescription) {  
  22. }  
  23. #endregion  

 

 

完工大吉,***在我们Contract中加入CallContextAttribute就可以啦,客户端不用增加任何代码了。

  1. [ServiceContract]  
  2. [CallContext]  
  3. public interface IBizTest {  
  4. [OperationContract]  
  5. [CallContext]  
  6. string GetWelcomeInfo();  

以上就是我们为大家介绍的WCF使用Header的相关操作方法。

责任编辑:曹凯 来源: 博客园
相关推荐

2010-02-26 11:22:16

LitwareHR使用

2010-02-24 13:48:44

MSMQ使用WCF

2010-02-24 10:07:48

WCF跨越边界

2010-02-24 10:41:28

WCF服务保护

2009-12-21 10:09:26

WCF创建客户端服务对

2010-02-25 16:52:12

引用WCF服务

2010-02-25 13:48:23

WCF动态创建代码

2010-02-25 09:13:34

WCF异步调用

2010-02-26 08:59:10

WCF服务宿主程序

2009-12-03 11:11:57

PHP网站优化

2009-12-29 18:09:00

Silverlight

2009-12-04 12:51:27

PHP functio

2009-12-07 18:42:55

PHP与Javascr

2009-12-11 17:52:21

PHP获取博客数据

2010-02-26 10:30:03

ASP.NET Aja

2010-01-06 15:56:18

.Net Framew

2009-12-09 16:49:09

PHP显示文章发布时间

2009-12-08 14:31:31

PHP命令行读取参数

2010-01-15 16:03:48

VB.NET重载Win

2009-12-15 14:09:39

Ruby创建可参数化类
点赞
收藏

51CTO技术栈公众号