在net.tcp wcf上下文中传输用户信息

时间:2015-01-14 10:28:45

标签: c# wcf tcp

我想将数据从客户端服务传输到服务器服务,但我不希望在每次调用服务函数时将其作为参数传输 我试图在OutgoingMessageProperties中插入数据,但我没有在服务器端获取它,我收到错误:A property with the name 'Token' is not present, 为什么? 如果我不允许使用它,为什么它具有Add功能? 我正在使用的协议是net.tcp

客户端:

GeneralServicesClient Ret = new GeneralServicesClient(Consts.WcfGeneralChannels.TcpIp);
using (OperationContextScope scope = new OperationContextScope(Ret.InnerChannel))
         {
             OperationContext.Current.OutgoingMessageProperties.Add("Token", Guid.NewGuid());
             Ret.Func();
         }

服务器端:

  System.ServiceModel.ServiceSecurityContext identity = System.ServiceModel.ServiceSecurityContext.Current;
  OperationContext context = OperationContext.Current;
  MessageProperties messageProperties = context.IncomingMessageProperties;
  string token = messageProperties["Token"].ToString();//throw error: A property with the name 'Token' is not present

1 个答案:

答案 0 :(得分:1)

您应该将服务电话放在使用中。

GeneralServicesClient Ret = new GeneralServicesClient(Consts.WcfGeneralChannels.TcpIp);
using (OperationContextScope scope = new OperationContextScope(Ret.InnerChannel))
     {
         OperationContext.Current.OutgoingMessageProperties.Add("Token", Guid.NewGuid());
         Ret.Do();
     }