在与WCF服务交互过程中传递用户信息等附加信息_第1页
在与WCF服务交互过程中传递用户信息等附加信息_第2页
在与WCF服务交互过程中传递用户信息等附加信息_第3页
在与WCF服务交互过程中传递用户信息等附加信息_第4页
在与WCF服务交互过程中传递用户信息等附加信息_第5页
已阅读5页,还剩5页未读, 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、 在与WCF服务交互过程中传递用户信息等附加信息 收藏新一篇: 在WCF中分页传输数据(Duplex方式) | 旧一篇: 在WCF中实现全局错误捕获  在Web应用中,我们可以使用Session或Cookies在浏览器与Server间维护会话状态及传递附加信息。假设有这样的需求,用户正常登陆系统后,Server产生一个UserIdentity发送到Client端,在此后的Client每次调用Service时都需要传递此UserIdentity传递到Server端以进行用户身份认证及权限审核。在WCF应用中有没有什么方法可以实

2、现同样功能呢?首先我们看看MSDN中对WCF的Session的说明:它们由调用应用程序显式启动和终止。会话期间传递的消息按照接收消息的顺序进行处理。会话将一组消息相互关联,从而形成对话。该关联的含义是抽象的。例如,一个基于会话的通道可能会根据共享网络连接来关联消息,而另一个基于会话的通道可能会根据消息正文中的共享标记来关联消息。可以从会话派生的功能取决于关联的性质。不存在与 WCF 会话相关联的常规数据存储区。最后一句告诉我们,WCF中的Session是无法像Web应用一样存储附加信息的。经过研究,我们可以通过扩展MessageHeader实现一个附加的数据存储区在Client端每次请求Ser

3、vice时发送到Server端。具体实现如下(以前述需求为例)。首先我们定义一个类作为交互过程中的UserIdentity,代码如下:using System;using System.Collections.Generic;using System.Text;namespace BNCommonPlus.BNCommonModel.    Serializable    public class UserIdentity   

4、; .        public string UserName = string.Empty;        public string UserRuningSystemID = string.Empty;        public string

5、0;UserSysGUID = string.Empty;                    这里使用Serializable作为序列化方式。在这个类中,UserName对应用户名,UserRuningSystemID对应用户已登陆的系统ID,UserSysGUID对应用户当前的有效登陆。用户登录后Server产生这个类的一个实例回传Client,Client将其保存在缓存

6、里,这里我定义了一个类来作为缓存,代码如下:using System;using System.Collections.Generic;using System.Text;using System.Data;using BNCommonPlus.BNCommonModel;namespace BNIIClientLayerPlus.    public class UserPermissionInfo    .  &

7、#160;     private UserPermissionInfo()        .         private static UserPermissionInfo _Instance = null;        publi

8、c static UserPermissionInfo GetInstance()        .            if (_Instance = null)            .  &#

9、160;             _Instance = new UserPermissionInfo();                        return _Instance;

10、                private UserIdentity _userIdentity = null;        public UserIdentity _UserIdentity        .&#

11、160;           get            .                return _userIdentity;     &#

12、160;                      public void SetUserIdentity(UserIdentity ui)        .         &#

13、160;  _userIdentity = ui;              这是一个单件类,Client正常登陆得到Server端回传的UserIdentity实例后可以通过如下代码将其存入缓存:UserPermissionInfo.GetInstance().SetUserIdentity(ServerReturnedUserIdentity);其中ServerReturnedUserIdentity就是Server产生并

14、回传的UserIdentity  下面我们扩展MessageHeader将我们自己定义的UserIdentity加入进去,代码如下:using System;using System.Collections.Generic;using System.Text;using System.ServiceModel;using System.ServiceProcess;using System.ServiceModel.Dispatcher;using System.ServiceModel.Description;u

15、sing System.ServiceModel.Channels;using System.ServiceModel.Configuration;namespace BNCommon.ClientHelper.public class BNClientMessageInspector : IClientMessageInspector    .        IClientMessageInspector

16、 成员#region IClientMessageInspector 成员        public void AfterReceiveReply(ref Message reply, object correlationState)        .        

17、        public object BeforeSendRequest(ref Message request, IClientChannel channel)        .            MessageHeader mh

18、0;= MessageHeader.CreateHeader("UserIdentity", "UINS", BNIIClientLayerPlus.UserPermissionInfo.GetInstance()._UserIdentity);            request.Headers.Add(mh);       

19、0;    return null;                #endregion     这个类实现了IClientMessageInspector接口,实现该接口可以在Client每次向Server请求前及请求返回后控制Client的行为对发送和接收的数据进行处理。现在我们需要实现BehaviorExtensionElement, IEn

20、dpointBehavior将刚刚建立的行为加入Client行为集合,代码如下:using System;using System.Collections.Generic;using System.Text;using System.ServiceModel;using System.ServiceProcess;using System.ServiceModel.Dispatcher;using System.ServiceModel.Description;using System.ServiceModel.C

21、hannels;using System.ServiceModel.Configuration;namespace BNCommon.ClientHelper.    public class BNClientEndpointBehavior : BehaviorExtensionElement, IEndpointBehavior    .        IEndp

22、ointBehavior 成员#region IEndpointBehavior 成员        public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)        .  

23、0;     public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)        .            clientRuntime.MessageInspectors.Add(

24、new BNClientMessageInspector();                public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)       

25、60;.                public void Validate(ServiceEndpoint endpoint)        .            return;  

26、;              #endregion        public override Type BehaviorType        .          

27、  get . return typeof(BNClientEndpointBehavior);                 protected override object CreateBehavior()        .   &

28、#160;        return new BNClientEndpointBehavior();              现在我们定义了Client在用户正常登陆后,每次请求都会发送缓存里面的UserIdentity实例。然后需要修改Client端的代理类,这里要分两种情况:如果客户端使用的是”SvcUtil.exe”工具生成的代理类,则需要修改对应Clien

29、t的构造方法,加入如下代码:Endpoint.Behaviors.Add(new BNCommonPlus.ClientHelper.BNClientEndpointBehavior();如果使用ChanelFactory则需要在Create对应得ChanelFactory后加入如下代码:_ChannelFactory.Endpoint.Behaviors.Add(new BNClientEndpointBehavior();当然也可以通过配置文件实现上述目的。搞定Client端后,再来看看Server端如何取Client每次发送过来的附加信息.首先在Server端要实现IDispatchMe

30、ssageInspector接口,代码如下:using System;using System.Collections.Generic;using System.Text;using System.ServiceModel;using System.ServiceModel.Dispatcher;using System.ServiceModel.Description;using System.ServiceModel.Channels;using System.ServiceModel.Configuratio

31、n;using BNCommonPlus.BNCommonModel;namespace BNCommonPlus.ServiceHelper.    public class DispatchMessageInspector : IDispatchMessageInspector    .        IDispatchMessageInspector 成员#re

32、gion IDispatchMessageInspector 成员        public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)        . 

33、60;          /得到身份信息            UserIdentity ui = request.Headers.GetHeader<UserIdentity>("UserIdentity", "UINS");     

34、       ./这里写验证用户身份,检测权限的代码。            return null;                public void BeforeSendReply(ref Messag

35、e reply, object correlationState)        .                #endregion      和客户端一样将上面定义的行为加入行为集合,代码如下:using System;using System.Coll

36、ections.Generic;using System.Text;using System.ServiceModel;using System.ServiceModel.Dispatcher;using System.ServiceModel.Description;using System.ServiceModel.Channels;using System.ServiceModel.Configuration;using BNCommonPlus.BNCommonModel;namespace BNCommo

37、nPlus.ServiceHelper.       public class BNServiceOperationBehavior : BehaviorExtensionElement, IEndpointBehavior   .       IEndpointBehavior 成员#region IEndpointBehavior 成员

38、0;       public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)        .             

39、   public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)        .                public void Appl

40、yDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)        .            endpointDispatcher.DispatchRuntime.MessageInspectors.Add(new DispatchMessageInspector();                 public void Validate(ServiceEndpoint endpoint)        .      

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论