我有一个由net.tcp绑定配置的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方法时,我可以看到通道存储在CallBacker.Client上,但是当我调用CallBacker> Call(字符串消息)的“ Call”方法时,客户端为空。
奇怪的是,当我将服务配置从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>
使用netTcpBinding时,CallBacker.Client保留CallBackContract的值,并且如果将CallBackContract更改为IServiceCallback,似乎没有任何问题。