我有一个基于WCF双工服务的应用程序。当用户“重新启动”应用程序时,我遇到了问题...在幕后,客户端关闭与WCF服务的连接并创建另一个连接。服务契约定义如下:
[ServiceContract(Namespace="net.tcp://namespace.MyService",
SessionMode=SessionMode.Required,
CallbackContract=typeof(IServiceCallback))]
public interface IMyService
{
[OperationContract(IsOneWay=true)]
void DoWork();
}
public interface IServiceCallback
{
[OperationContract(IsOneWay=true)]
void SendMessage(string message);
}
该实现定义如下:
[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Single,
InstanceContextMode = InstanceContextMode.PerSession,
UseSynchronizationContext = false,
IncludeExceptionDetailInFaults = true)]
public class MyService : IMyService
{
public void DoWork()
{
var callback = OperationContext.Current.GetCallbackChannel<IServiceCallback>();
callback.SendMessage("Hello, world.");
}
}
客户端的配置如下所示:
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="net.tcp" receiveTimeout="02:00:00" sendTimeout="02:00:00" maxReceivedMessageSize="2147483647">
<security mode="None"/>
</binding>
</netTcpBinding>
</bindings>
<client>
<endpoint address="net.tcp://localhost:8000/MyService/MyService"
binding="netTcpBinding" bindingConfiguration="net.tcp" contract="ExternalServiceReference.IMyService">
</endpoint>
</client>
</system.serviceModel>
服务的配置:
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="serviceBehaviour">
<serviceMetadata />
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<netTcpBinding>
<binding name="netTcp" sendTimeout="01:00:00" receiveTimeout="01:00:00" >
<security mode="None">
</security>
</binding>
</netTcpBinding>
</bindings>
<services>
<service behaviorConfiguration="serviceBehaviour" name="MyService.MyService">
<endpoint address="MyService" binding="netTcpBinding" bindingConfiguration="netTcp" name="net.tcp" contract="MyService.IMyService" />
<endpoint binding="mexTcpBinding" bindingConfiguration="" name="net.tcp" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:8000/MyService" />
</baseAddresses>
</host>
</service>
</services>
在客户端的构造函数中:
var callback = new CallbackImplementation();
_context = new InstanceContext(callback);
_proxy = new MyServiceProxy(_context);
在建立新连接之前,我正在尝试以下操作:
try
{
if (_context != null)
{
_context.ReleaseServiceInstance();
_context.Close();
}
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
if (_context != null)
{
_context.Abort();
}
}
我发现的问题是_context.Close()调用总是超时并抛出异常。虽然我随后中止了通道,但这让我感到不对劲,我认为这是我的应用程序冻结的原因。有人知道为什么Close()调用会失败吗?
编辑:我之前漏掉了一些关于我的回调实现的内容,可能与此相关。它看起来像这样:
[CallbackBehavior(ConcurrencyMode = ConcurrencyMode.Single,
UseSynchronizationContext = false,
IncludeExceptionDetailInFaults = true)]
public class CallbackImplementation : IServiceCallback
{
public void SendMessage(string message)
{
// Do something with the message
}
}
异常信息是" ServiceHost 关闭操作在 00:00:30 后超时。这可能是因为客户端未能在所需时间内关闭会话通道。分配给此操作的时间可能是较长超时的一部分。"。没有内部异常。
谢谢
OnClose
事件处理程序。查看该事件是否在callback
服务器端被触发。 - mrtigTask.StartNew(()=>{_context.Close();});
- mrtig