gpt4 book ai didi

c# - 使用 Web 服务时出现 SocketException

转载 作者:太空宇宙 更新时间:2023-11-03 14:34:03 24 4
gpt4 key购买 nike

我在 WinForm 应用程序中使用了一个网络服务。我生成了一个 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()

很遗憾,我无法将完整的包装器代码放在这里。但这是我访问的事件参数(请参阅下面的结果属性)

<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

最佳答案

当我尝试多次重用套接字实例时,我遇到了类似的问题。即使使用 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 服务请求实例......

关于c# - 使用 Web 服务时出现 SocketException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1784571/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com