簡體   English   中英

在Windows服務中托管的WCF雙工會話中處理服務關閉

[英]handle service shutdown in WCF duplex session hosted in windows service

我有一個托管WCF服務的Windows服務,該服務將信息廣播到多個客戶端。 我想從客戶端正常處理服務關閉,即檢測到服務已消失或想要關閉,彈出消息並關閉應用程序。 當前,如果我在關閉行關閉該服務,則會得到以下信息。

“ 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

最終班

解決方案是向回調接口添加“取消訂閱”方法。 然后,客戶可以退訂。 因此,當服務停止時,它會要求客戶端斷開連接。

   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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM