开发者

SocketException when consuming Web Service

开发者 https://www.devze.com 2022-12-12 12:34 出处:网络
I have a webservice being consumed in the WinForm application. I generated a web service wrapper and invoked a web service method asynchronously. I get following exception in my result completed event

I have a webservice being consumed in the WinForm application. I generated a web service wrapper and invoked a web service method asynchronously. I get following exception in my result completed event handler (the event is raised from within the web service wrapper class). What could be the problem?

    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()

Unfortunately I cannot put the complete wrap开发者_开发技巧per code here. But this is the event args that i access (see Result property below)

<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


I had a similar problem when I tried to reuse a socket instance multiple times. Even with socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);, it seems to be not reliable.

I don't use .NET web service wrapper, but you should perhaps looking for something like (implementation to adapt...):

try 
{
    // socket connection...
}
catch (SocketException ex)
{
    if (ex.SocketErrorCode == SocketError.AddressAlreadyInUse) 
    {
        // retry with a new instance socket.
    }
}

Or simply create a new instance of the web service request before each call...


A SocketException can happen when you're calling a web service that has a limited protocol such as TLS 1.2.

If you've established that this is the case, you can refer to the following for several examples on how to adapt your code for this: Update .NET web service to use TLS 1.2

0

精彩评论

暂无评论...
验证码 换一张
取 消