2012-12-13 129 views
2

我有一个托管WCF服务的Windows服务,该服务向多个客户端广播信息。我想优雅地处理来自客户端的服务关闭,即检测服务已经消失或想要关闭,弹出消息并关闭应用程序。目前,如果我关闭该服务,我会得到以下结果。在Windows服务中托管的WCF双工会话中处理服务关闭

“ServiceHost的关闭操作00后超时:00:09.9910000这可能是因为客户端未能在规定的时间内关闭会话通道。”

问题:我如何检测关闭,请求关闭客户端?

客户端,我得到的服务关闭后“插座异常视频时现有的连接被强行关闭远程主机”

这里是接近代码。

If Not (_LivePricingHost Is Nothing) Then 
    _LivePricingHost.Close() 
    _LivePricingHost = Nothing 
End If 

这里是我用来设置双面打印会话课,我已经删除了样板处理代码,我集资最多客户端的事件。

Imports System.ServiceModel 

Public Class NotificationCallback 
    ' Implements LivePricingService.IMessageCallback 
    Implements IDisposable 

    Private _ns As LivePricingService.MessageClient 

    Public Sub New() 
     Dim context = New InstanceContext(Me) 

     _ns = New LivePricingService.MessageClient(context) 
     _ns.SubscribeForBroadcast() 
     ' _ns.AddMessage("PING") 
    End Sub 

#End Region 

末级

回答

1

的解决方案是增加一个“取消订阅”方法的回调接口。然后客户可以退订。 因此,当服务停止时,它会要求客户端断开连接。

Protected Overrides Sub OnStop() 
     pollTimer.Stop() 
     _LivePricingWCFService.UnsubscribeAll() 
     If Not (_LivePricingHost Is Nothing) Then 
      _LivePricingHost.Close() 
      _LivePricingHost = Nothing 
     End If 
    End Sub 

在WCF服务

Public Sub UnsubscribeAll() 
     Try 
      RemoveClosedSubscribers() 

      For Each callback As IMessageCallback In _subscribers 
       If DirectCast(callback, ICommunicationObject).State = CommunicationState.Opened Then 
        callback.ShutDown() 'request that the client disconnect 
        _subscribers.Remove(callback) 
       End If 
      Next 
     Catch ex As Exception 
      Logging.Logger.LogMessage(ex:=) 
     End Try 
    End Sub 

,并在客户端上

Public Sub ShutDown() Implements LivePricingService.IMessageCallback.ShutDown 
    _ns.Close() 
    Globals.LastException = New Exception("The Live Pricing Serice has shut down.") 

End Sub 
相关问题