gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 13:24:39 25 4
gpt4 key购买 nike

我有一个通过 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com