如何正确关闭 WCF Netnamedpipebinding 通道?
How to properly close a WCF Netnamedpipebinding Channel?
我有一个从我的应用程序到 Office 加载项的 WCF Netnamedpipe 绑定。我注意到当办公室应用程序忙于做其他事情时,我的应用程序在使用 WCF 方法时会被锁定。我已经添加了我的代码示例。代码似乎停止并等待 channel.close 方法。
- 解决办法是把channel.close改成channel.BeginClose吗?
我需要传递给 BeginClose 方法的状态对象是什么?
Public Function RequestPersonStatus(ByVal id As String, ByVal email As String)
Using factory As New ChannelFactory(Of IToOffice)(New NetNamedPipeBinding(), New EndpointAddress("net.pipe://localhost/" + XXXXXX))
Dim OfficeChannel As IToOffice = factory.CreateChannel()
Try
OfficeChannel.RequestPersonStatus(id:=id, email:=email)
Catch ex As Exception
Return False
Finally
CloseChannel(CType(OfficeChannel, ICommunicationObject))
End Try
End Using
Return True
End Function
和关闭频道
Private Sub CloseChannel(ByVal channel As ICommunicationObject)
Try
If channel.State = CommunicationState.Opened Then
Dim caller As New AsyncCallback(AddressOf callback)
channel.BeginClose(caller, New Object)
' channel.Close()
End If
Catch ex As Exception
Log(LogTypes.AllExceptions, "CloseChannel - Error closing the channel. ", ex.ToString)
Finally
channel.Abort()
End Try
End Sub
据我所知,像超时error/connection/communication这样的错误,大部分都是由于channel/client代理没有正确关闭造成的。将客户端 proxy/Service 通道放在 using 块中将消除该问题。
using (ServiceReference1.ServiceClient client=new ServiceClient())
{
var result = client.Test();
Console.WriteLine(result);
}
Using 语句用于在完成调用后自动关闭服务proxy/service 通信通道。此外,Service client proxy类似于ChannelFactory创建的通信通道。
如果有什么我可以帮忙的,请随时告诉我。
关于清理/处置/关闭频道的内容和时间似乎有很多讨论。我只是在这里发布我现在正在做的事情以及我对问题的回答。
Private Sub CloseChannel(ByVal channel As ICommunicationObject)
Try
If channel.State <> CommunicationState.Closed AndAlso channel.State <> CommunicationState.Faulted Then
channel.BeginClose(Sub(asr)
Try
channel.EndClose(asr)
Catch
channel.Abort()
End Try
End Sub, Nothing)
Else
channel.Abort()
End If
Catch commEx As CommunicationException
channel.Abort()
Catch ex As Exception
channel.Abort()
Finally
End Try
End Sub
我有一个从我的应用程序到 Office 加载项的 WCF Netnamedpipe 绑定。我注意到当办公室应用程序忙于做其他事情时,我的应用程序在使用 WCF 方法时会被锁定。我已经添加了我的代码示例。代码似乎停止并等待 channel.close 方法。
- 解决办法是把channel.close改成channel.BeginClose吗?
我需要传递给 BeginClose 方法的状态对象是什么?
Public Function RequestPersonStatus(ByVal id As String, ByVal email As String) Using factory As New ChannelFactory(Of IToOffice)(New NetNamedPipeBinding(), New EndpointAddress("net.pipe://localhost/" + XXXXXX)) Dim OfficeChannel As IToOffice = factory.CreateChannel() Try OfficeChannel.RequestPersonStatus(id:=id, email:=email) Catch ex As Exception Return False Finally CloseChannel(CType(OfficeChannel, ICommunicationObject)) End Try End Using Return True End Function
和关闭频道
Private Sub CloseChannel(ByVal channel As ICommunicationObject)
Try
If channel.State = CommunicationState.Opened Then
Dim caller As New AsyncCallback(AddressOf callback)
channel.BeginClose(caller, New Object)
' channel.Close()
End If
Catch ex As Exception
Log(LogTypes.AllExceptions, "CloseChannel - Error closing the channel. ", ex.ToString)
Finally
channel.Abort()
End Try
End Sub
据我所知,像超时error/connection/communication这样的错误,大部分都是由于channel/client代理没有正确关闭造成的。将客户端 proxy/Service 通道放在 using 块中将消除该问题。
using (ServiceReference1.ServiceClient client=new ServiceClient())
{
var result = client.Test();
Console.WriteLine(result);
}
Using 语句用于在完成调用后自动关闭服务proxy/service 通信通道。此外,Service client proxy类似于ChannelFactory创建的通信通道。
如果有什么我可以帮忙的,请随时告诉我。
关于清理/处置/关闭频道的内容和时间似乎有很多讨论。我只是在这里发布我现在正在做的事情以及我对问题的回答。
Private Sub CloseChannel(ByVal channel As ICommunicationObject)
Try
If channel.State <> CommunicationState.Closed AndAlso channel.State <> CommunicationState.Faulted Then
channel.BeginClose(Sub(asr)
Try
channel.EndClose(asr)
Catch
channel.Abort()
End Try
End Sub, Nothing)
Else
channel.Abort()
End If
Catch commEx As CommunicationException
channel.Abort()
Catch ex As Exception
channel.Abort()
Finally
End Try
End Sub