我使用WCF实现了发布者 - 订阅者机制。
这就是我订阅回调的方式:
public delegate void AsyncResponseEventHandler(AsyncResponse asyncResponse);
public static event AsyncResponseEventHandler AsyncResponseEvent;
public static IMyEvents Subscriber;
public void SubscribeEvent()
{
Subscriber = OperationContext.Current.GetCallbackChannel<IMyEvents>();
AsyncResponseEvent += new AsyncResponseEventHandler (Subscriber.AsyncResponseCallBack);
}
public static void RaiseAsyncResponse(AsyncResponse asyncResponse)
{
try
{
AsyncResponseEvent.Invoke(asyncResponse);
}
catch (Exception ex) {throw;}
}
这也是MySubscriber类代码:
public class MySubscriber : IMyEvents
{
public void AsyncResponseCallBack(AsyncResponse asyncResponse)
{
AsyncResponseEventArgs e = new AsyncResponseEventArgs()
{
Response = asyncResponse
};
OnAsyncResponseReceived(this, e);
}
}
我使用RaiseAsyncResponse方法来引发对客户端的回调,
当服务主机是Windows服务并且我启动服务并且我开始调试时,一切正常,但是当我停止客户端应用程序并重新启动调试而没有重新启动Windows服务时,我面临异常:通信对象System.ServiceModel.Channels.ServiceChannel
不能用于通信,因为它已被中止......
为什么回叫频道被中止? 请帮帮我... :(
答案 0 :(得分:1)
因为“但当我停止客户端应用程序时”就是答案。停止客户端应用程序后,System.ServiceModel将释放通道上的保留。如果您终止服务或客户端,则无法将数据从服务发送到客户端,反之亦然。