我在WinForm应用程序中使用了web服务。我生成了一个Web服务包装器并异步调用Web服务方法。我在结果完成事件处理程序中得到以下异常(该事件是从Web服务包装器类中引发的)。可能是什么问题呢?
System.Reflection.TargetInvocationException: An exception occurred during the operation, making the result invalid. Check InnerException for exception details.
---> System.Net.WebException: Unable to connect to the remote server
---> System.Net.Sockets.SocketException: A connect request was made on an already connected socket
at System.Net.Sockets.Socket.BeginConnectEx(EndPoint remoteEP, Boolean flowContext, AsyncCallback callback, Object state)
at System.Net.Sockets.Socket.UnsafeBeginConnect(EndPoint remoteEP, AsyncCallback callback, Object state)
at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Int32 timeout, Exception& exception)
--- End of inner exception stack trace ---
at System.Web.Services.Protocols.WebClientAsyncResult.WaitForResponse()
at System.Web.Services.Protocols.WebClientProtocol.EndSend(IAsyncResult asyncResult, Object& internalAsyncState, Stream& responseStream)
at System.Web.Services.Protocols.SoapHttpClientProtocol.InvokeAsyncCallback(IAsyncResult result)
--- End of inner exception stack trace ---
at System.ComponentModel.AsyncCompletedEventArgs.RaiseExceptionIfNecessary()
不幸的是我不能把完整的包装代码放在这里。但这是我访问的事件args(参见下面的Result属性)
<ComVisible(False), System.CodeDom.Compiler.GeneratedCodeAttribute("System.Web.Services", "2.0.50727.3053")> _
Public Delegate Sub MyCompletedEventHandler(ByVal sender As Object, ByVal e As MyCompletedEventArgs)
<ComVisible(False), _
System.CodeDom.Compiler.GeneratedCodeAttribute("System.Web.Services", "2.0.50727.3053"), _
System.Diagnostics.DebuggerStepThroughAttribute(), _
System.ComponentModel.DesignerCategoryAttribute("code")> _
Partial Public Class MyCompletedEventArgs
Inherits System.ComponentModel.AsyncCompletedEventArgs
Private results() As Object
Friend Sub New(ByVal results() As Object, ByVal exception As System.Exception, ByVal cancelled As Boolean, ByVal userState As Object)
MyBase.New(exception, cancelled, userState)
Me.results = results
End Sub
'''<remarks/>
Public ReadOnly Property Result() As MyResponse
Get
Me.RaiseExceptionIfNecessary()
Return CType(Me.results(0), MyResponse)
End Get
End Property
End Class
Private Sub OnMyOperationCompleted(ByVal arg As Object)
If (Not (Me.MyCompletedEvent) Is Nothing) Then
Dim invokeArgs As System.Web.Services.Protocols.InvokeCompletedEventArgs = CType(arg, System.Web.Services.Protocols.InvokeCompletedEventArgs)
RaiseEvent MyPointsCompleted(Me, New MyCompletedEventArgs(invokeArgs.Results, invokeArgs.Error, invokeArgs.Cancelled, invokeArgs.UserState))
End If
End Sub
答案 0 :(得分:1)
当我尝试多次重用套接字实例时遇到了类似的问题。即使使用socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
,它似乎也不可靠。
我不使用.NET Web服务包装器,但你应该寻找类似的东西(适应的实现......):
try
{
// socket connection...
}
catch (SocketException ex)
{
if (ex.SocketErrorCode == SocketError.AddressAlreadyInUse)
{
// retry with a new instance socket.
}
}
或者只是在每次调用之前创建一个新的Web服务请求实例......
答案 1 :(得分:0)
当您调用具有受限协议(例如TLS 1.2)的Web服务时,可能会发生SocketException
。
如果已确定是这种情况,则可以参考以下示例,了解有关如何为此修改代码的几个示例: Update .NET web service to use TLS 1.2