简体   繁体   English

将其存储到静态变量时无法访问OperationContext.Current

[英]I cannot reach OperationContext.Current when I store it to a static variable

I have a WCF Service that is configured by net.tcp binding. 我有一个由net.tcp绑定配置的WCF服务。 I can reach the service by client and can call methods of it. 我可以通过客户端访问该服务,并可以调用其方法。 Also I can reach the OperationContext.Current. 我也可以到达OperationContext.Current。 Such as: 如:

[ServiceContract(CallbackContract = typeof(IServiceCallback))]
public interface IService
{
    [OperationContract(IsOneWay = true)]
    void Register();
}

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)]
    public class Service :IService
    {
        public void Register()
        {
             CallBacker.Client = OperationContext.Current.
                                 GetCallbackChannel<IServiceCallback>();

        }
    }

}

public class CallBacker 
{
   public static IServiceCallback Client { get; set; }

   public void Call(string message)
   {
       Client.Test(message);
   }
}

When the client calls the Register method of Service, I can see channel is stored on CallBacker.Client but when I call "Call" method of the CallBacker > Call(string message), the Client comes null. 当客户端调用Service的Register方法时,可以看到通道存储在CallBacker.Client上,但是当我调用CallBacker> Call(字符串消息)的“ Call”方法时,客户端为空。

The strange thing is when I set the service configuration from net.tcp to wsdualhttpbinding, it works perfect. 奇怪的是,当我将服务配置从net.tcp设置为wsdualhttpbinding时,它可以正常工作。 Is there any different between net.tcp and wsdualhttpbinding that can cause this strange problem ? net.tcp和wsdualhttpbinding之间是否有任何差异可以导致此奇怪的问题?

It seems that the CallbackContract should be of type IServiceCallback [ServiceContract(CallbackContract = typeof(IServiceCallback))] 似乎CallbackContract的类型应该为IServiceCallback [ServiceContract(CallbackContract = typeof(IServiceCallback))]

The below is the config for both wsDualHttpBinding and netTcpBinding 以下是wsDualHttpBinding和netTcpBinding的配置

<services>
  <service behaviorConfiguration="behavior" name="WCFCallbackTry.Service1">
    <endpoint address="" binding="wsDualHttpBinding" bindingConfiguration="HttpEndPoint"
      contract="WCFCallbackTry.IService" name="HttpEndPoint"/>
    <endpoint address="net.tcp://localhost:8004/Service1.svc"  bindingConfiguration="BindingConfiguration" binding="netTcpBinding"
      contract="WCFCallbackTry.IService" name="NetTcpEndPoint"/>
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
    <host>
      <baseAddresses>
        <add baseAddress="http://localhost:8018/Service1.svc"/>
      </baseAddresses>
    </host>
  </service>
</services>

Please also find the netTcpBinding used 也请找到使用的netTcpBinding

<netTcpBinding>
    <binding name="BindingConfiguration" receiveTimeout="infinite" sendTimeout="10.00:00:00" maxBufferPoolSize="1073741824" maxBufferSize="1073741824" maxReceivedMessageSize="1073741824">
      <readerQuotas maxDepth="32" maxStringContentLength="2147483647" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384"/>
      <reliableSession enabled="true" inactivityTimeout="01:00:00" ordered="true" />
    </binding>
  </netTcpBinding>

The CallBacker.Client holds the value of the CallBackContract while using netTcpBinding, and there does not seem to be any issue if the CallBackContract is changed to IServiceCallback 在使用netTcpBinding时,CallBacker.Client保留CallBackContract的值,并且如果CallBackContract更改为IServiceCallback,似乎没有任何问题。

You are misunderstanding the implication of static here. 您在这里误解了static的含义。 In this context static would mean, be the same for every CallBacker instance. 在这种情况下,静态意味着每个CallBacker实例都相同。 Then with your concurency.multiple multiple clients at the same time might use your callbacker.client to callback but the context won't be the one you expect. 然后使用concurency。多个客户端可以同时使用多个callbacker.client进行回调,但是上下文不会是您期望的。 Drop the static and see if you get any errors in the log file then. 删除静态文件,然后查看日志文件中是否有任何错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 OperationContext.Current中的WCF MessageHeaders - WCF MessageHeaders in OperationContext.Current 当OperationContext.Current为null时访问WCF MessageHeader - Accessing a WCF MessageHeader when OperationContext.Current is null 如何模拟OperationContext.Current(WCF消息) - How to mock OperationContext.Current (WCF Message) 我的第一个WCF服务器 - 为什么OperationContext.Current为null? - my first WCF Server - why OperationContext.Current is null? 为什么IAuthorizationPolicy.Evaluate()中的OperationContext.Current为null,而只有前两次? - Why is OperationContext.Current null in IAuthorizationPolicy.Evaluate() but only the first two times? 访问WCF编码器中的传入消息属性(OperationContext.Current为null) - Get access to incoming message properties in WCF encoder (OperationContext.Current is null) 使用xUnit编写单元测试时,如何在控制器方法内达到int变量 - How to reach int variable that inside my controller method when i write unit test with xUnit REST服务-我是否真的必须阅读OperationContext才能获得消息? - REST Services - Do I really have to read the OperationContext to get the message? 我无法到达我当前的实例来更改 label - I can't reach my current instance to change a label 当我尝试达到Rectangle时为空值 - Null values when I try to reach Rectangle
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM