4

クライアントプログラムを閉じた後、「既存の接続がリモートホストによって強制的に閉じられました」というエラーが表示されます。プログラムが閉じられたときにクライアント接続を確実に閉じるために、このコードを追加しました。

クライアントを閉じるためのボタンもあり、ボタンはエラーなしで機能します。

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{          
    try
    {
       client.Close();  
    }
    catch (CommunicationException ex)
    {
       client.Abort();
    }
    catch (TimeoutException ex)
    {
       client.Abort();
    }
    catch (Exception ex)
    {
       client.Abort();
       throw ex;
    }
}

ここで何かが恋しいですか?これはスタックトレースです:

< StackTrace>
bei System.ServiceModel.Channels.SocketConnection.HandleReceiveAsyncCompleted()
bei System.ServiceModel.Channels.SocketConnection.OnReceiveAsync(Object sender, SocketAsyncEventArgs eventArgs)
bei System.ServiceModel.Channels.SocketConnection.OnReceiveAsyncCompleted(Object sender, SocketAsyncEventArgs e)
bei System.Net.Sockets.SocketAsyncEventArgs.OnCompleted(SocketAsyncEventArgs e)
bei System.Net.Sockets.SocketAsyncEventArgs.FinishOperationAsyncFailure(SocketError socketError, Int32 bytesTransferred, SocketFlags flags)
bei System.Net.Sockets.SocketAsyncEventArgs.CompletionPortCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* nativeOverlapped)
bei System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP)
< /StackTrace>

スタックトレースが誰かに助けてくれることを願っています:D私はnettcpを使用していますが、プログラムを閉じたときにのみエラーが発生します。

ありがとう、マヌエル

更新:wcf構成:サーバー:

 <serviceBehaviors>
        <behavior name="MyBehaviour">
          <serviceMetadata/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
 <services>
      <service name="Service" behaviorConfiguration="MyBehaviour">
        <host>
        </host>

        <endpoint address="" binding="netTcpBinding" bindingConfiguration="TCP" contract="IService"/>
        <endpoint address="mextcp" binding="mexTcpBinding" contract="IMetadataExchange"/>

      </service>
    </services>

<netTcpBinding>
        <binding name="TCP" portSharingEnabled="true" receiveTimeout="01:30:00" sendTimeout="01:10:00"
          openTimeout="00:10:00" closeTimeout="00:10:00">
         <security mode="None"/>
        </binding>
      </netTcpBinding>

クライアント:

<configuration>
    <system.serviceModel>
        <bindings>
            <netTcpBinding>
               <binding name="NetTcpBinding_IService" receiveTimeout="01:30:00" sendTimeout="01:00:00" openTimeout="00:10:00" closeTimeout="00:10:00">
               <security mode="None" />
              </binding>
            </netTcpBinding>
        </bindings>
        <client>
            <endpoint address="net.tcp://host/Service.svc"
                binding="netTcpBinding" bindingConfiguration="NetTcpBinding_IService"
                contract="ServiceRef.IService" name="NetTcpBinding_IService" />
        </client>
    </system.serviceModel>
</configuration>
4

2 に答える 2

0

このエラーにはいくつかの原因が考えられますが、最も一般的な原因は、ピアが既に接続を閉じているときにデータを送信することです。つまり、アプリケーション プロトコル エラーです。接続を正常に閉じるのではなく、意図的にリセットすることによって、ピアが不正な動作をした可能性もあります。どちらの場合でも、ソケットを閉じてピアを忘れる以外に、実行時にできることはあまりありません。ただし、コンパイルする前に原因を調査する必要があります。

于 2012-12-09T20:45:27.590 に答える