c# - 当我将它存储到静态变量时,我无法访问 OperationContext.Current

标签 c# .net wcf nettcpbinding wsdualhttpbinding

我有一个通过 net.tcp 绑定(bind)配置的 WCF 服务。我可以通过客户端访问服务并调用它的方法。我也可以访问 OperationContext.Current。如:

[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);
   }
}

当客户端调用 Service 的 Register 方法时,我可以看到 channel 存储在 CallBacker.Client 上,但是当我调用 CallBacker > Call(string message) 的“Call”方法时,Client 为空。

奇怪的是,当我将服务配置从 net.tcp 设置为 wsdualhttpbinding 时,它工作得很好。 net.tcp 和 wsdualhttpbinding 之间有什么不同会导致这个奇怪的问题吗?

最佳答案

CallbackContract 似乎应该是 IServiceCallback 类型 [ServiceContract(CallbackContract = typeof(IServiceCallback))]

下面是 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>

请同时找到使用的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>

CallBacker.Client在使用netTcpBinding时持有CallBackContract的值,如果将CallBackContract改为IServiceCallback似乎没有任何问题

关于c# - 当我将它存储到静态变量时,我无法访问 OperationContext.Current,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22808691/

相关文章:

c# - 如何在一个时间跨度内限制对 WCF 服务的调用?

wcf - 在 WCF-CustomIsolated Receive Location 上创建失败消息

c# - 在WEB API 2中模拟WCF路由

c# - Entity Framework 问题 - ASP.NET 4 MVC 3 - SportsStore 项目

c# - 比较浮点值

.net - XML 单元测试 : XmlUnit alternatives for . NET?

c# - 使用 Expression<Func, T>> 的 NHibernate 查询不起作用

c# - 是否有可能通过反射获得属性(property)的私有(private)二传手?

c# - 简化winforms代码跨线程调用

c# - 将 C 方法/类导入 C# 项目