0

我用 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 ...

为什么回调通道被中止?请帮帮我... :(

4

1 回答 1

1

因为“但是当我停止客户端应用程序时”就是答案。停止客户端应用程序后,System.ServiceModel 将立即释放对通道的保留。如果您终止服务或客户端,则不能将数据从服务发送到客户端,反之亦然。

于 2013-05-10T18:47:10.763 回答