我有一个从我的应用程序到Office的WCF Netnamedpipebinding。我注意到,当office应用程序忙着做其他事情时,我的应用程序在使用WCF方法时会锁定它。我添加了一个代码示例。代码似乎停止并使用channel.close方法等待。
和closeChannel
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发布于 2020-05-07 05:40:56
关于清理/处置/关闭通道的内容和时间,似乎有很多讨论。我只是在这里张贴我现在正在做的事情,从而回答我的问题。
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发布于 2020-04-30 07:05:42
据我所知,像超时错误/连接/通信错误这样的大多数情况都是由于通道/客户端代理没有正确关闭而引起的。将客户端代理/服务通道放置在使用块中将消除该问题。
using (ServiceReference1.ServiceClient client=new ServiceClient())
{
var result = client.Test();
Console.WriteLine(result);
}Using语句对于在调用完成后自动关闭服务代理/服务通信通道非常有用。此外,服务客户端代理类似于ChannelFactory创建的通信通道。
如果有什么需要我帮忙的,请随时通知我。
https://stackoverflow.com/questions/61473115
复制相似问题