我用 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
, 不能用于通信,因为它已经被 Abort ...
为什么回调通道被中止?请帮帮我... :(