简体   繁体   English

WCF返回Stream时客户端中止

[英]Client aborted when WCF return Stream

I'm currently playing with a WCF service and a C# basic Winform application. 我目前正在使用WCF服务和C#基本Winform应用程序。 Both are on different server. 两者都在不同的服务器上。 I'm trying to have the C# application get a file from the other server using a WCF. 我正在尝试让C#应用程序使用WCF从其他服务器获取文件。 Here's the code for the WCF: 这是WCF的代码:

public Stream GetData(string fileName)
{
  string filePath;
  FileStream file = null;
  try
  {
    // Stuff

    file = File.OpenRead(Path.Combine(filePath, fileName));

    file.Position = 0L;

    return (Stream)file;

  }
  catch (Exception ex)
  {
    // Trace stuff
    return Stream.Null;
  } 
}

and this is the operation contract: 这是运营合同:

[OperationContract]
Stream GetData(string fileName);

This is the client configuration file: 这是客户端配置文件:

   <system.serviceModel>
    <bindings>
      <netTcpBinding>
        <binding name="NetTcpBindingEndpoint"  transferMode="Streamed" closeTimeout ="10:00:00" openTimeout="10:00:00" sendTimeout="10:00:00" receiveTimeout ="10:00:00" maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">
          <security mode="None" />
        </binding>
      </netTcpBinding>
    </bindings>
    <client>
      <endpoint address="net.tcp://SERVER/UpdaterUtilsWCF" binding="netTcpBinding"
        bindingConfiguration="NetTcpBindingEndpoint" contract="UpdaterUtilsWCF.IUpdaterUtilsWCF"
        name="NetTcpBindingEndpoint">
        <identity>
          <dns value="localhost" />
        </identity>
      </endpoint>
    </client>
  </system.serviceModel>

and this is the WCF configuration file: 这是WCF配置文件:

<system.serviceModel>
   <bindings>
  <netTcpBinding>
    <binding  name="TCPBinding_IDataService" openTimeout="10:00:00"   
                 closeTimeout="10:00:00"   
                 sendTimeout="10:00:00"   
                 receiveTimeout="10:00:00" 
         maxBufferPoolSize="2147483647" 
                 maxBufferSize="2147483647" 
                 maxReceivedMessageSize="2147483647"
         transferMode="Streamed">
      <security mode="None" />
        </binding>
      </netTcpBinding>
    </bindings>
    <services>
      <service name="UpdaterWCF.UpdaterUtilsWCF">
        <endpoint address="" binding="netTcpBinding" bindingConfiguration="TCPBinding_IDataService"
          name="NetTcpBindingEndpoint" contract="UpdaterWCF.IUpdaterUtilsWCF">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexTcpBinding" bindingConfiguration=""
          name="MexTcpBindingEndpoint" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://SERVER/UpdaterUtilsWCF" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceThrottling maxConcurrentCalls="100"   maxConcurrentInstances="2147483647" maxConcurrentSessions="200" />
          <dataContractSerializer maxItemsInObjectGraph="2147483646"/>
          <serviceMetadata httpGetEnabled="false" httpsGetEnabled="false" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

Max concurrent calls, instances and sessions are set to maximum in the WCF configuration file. 在WCF配置文件中,最大并发调用,实例和会话数设置为最大值。

This is the code for the client application: 这是客户端应用程序的代码:

    using (Stream stream = WCFClient.GetData(textBox1.Text))
    {
      // stuff
    }

When i execute the application, it crash on the client side (or the WCF side) but when the Stream is passed to the client application. 当我执行应用程序时,它在客户端(或WCF端)崩溃,但是当Stream传递到客户端应用程序时。 I have trace the code on the WCF and the call have been made and the return code is called at the end of the WCF. 我已经在WCF上跟踪了代码,并且已经进行了调用,并且在WCF的末尾调用了返回代码。 The error appear immediately after the call and the file is very small (400kb) Here's the error: 该错误在呼叫后立即出现,并且文件很小(400kb),这是错误:

System.ServiceModel.CommunicationException: The socket connection was aborted. System.ServiceModel.CommunicationException:套接字连接已中止。 This could be caused by an error processing your message or a receive timeout being exceeded by the remote host, or an underlying network resource issue. 这可能是由于处理您的消息时出错,远程主机超出了接收超时或潜在的网络资源问题引起的。 Local socket timeout was '10:00:00'. 本地套接字超时为“ 10:00:00”。 System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags) at System.ServiceModel.Channels.SocketConnection.ReadCore(Byte[] buffer, Int32 offset, Int32 size, TimeSpan timeout, Boolean closing) System.Net.Sockets.SocketException:System.Net.Sockets.Socket.Receive(Byte []缓冲区,Int32偏移量,Int32大小,SocketFlags socketFlags)处的远程主机强制关闭了现有连接。 SocketConnection.ReadCore(Byte []缓冲区,Int32偏移量,Int32大小,TimeSpan超时,布尔值关闭)

Please help me with this. 请帮我解决一下这个。

  • EDITED I'v added entire system.model for both app 编辑我为两个应用程序添加了整个system.model

In my experience, this is usually a problem in the configuration. 以我的经验,这通常是配置中的问题。 Make sure you are following a known good example to start. 确保您遵循一个已知的好例子开始。 The Microsoft example is here: https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/how-to-enable-streaming may be a good start. Microsoft的示例位于: https : //docs.microsoft.com/zh-cn/dotnet/framework/wcf/feature-details/how-to-enable-streaming可能是一个不错的开始。 Set all three buffer sizes or test with a file that is only a few characters to verify basic functionality before sending a larger file. 设置所有三个缓冲区的大小,或者使用只有几个字符的文件进行测试,以在发送更大的文件之前验证基本功能。

<basicHttpBinding>
  <binding name="HttpStreaming" maxBufferPoolSize="2147483647" 
maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" transferMode="Streamed"/>

</basicHttpBinding>
<!-- an example customBinding using Http and streaming-->
<customBinding>
  <binding name="Soap12">
    <textMessageEncoding messageVersion="Soap12WSAddressing10" />
    <httpTransport transferMode="Streamed" maxReceivedMessageSize="67108864"/>
  </binding>
</customBinding>

After a bunch of debugging, i've found the problem. 经过一堆调试,我发现了问题。 In my WCF service code i've implemented the stream ressource release after the try - catch: 在我的WCF服务代码中,我在try-catch之后实现了流资源发布:

    finally
    {
      if (file != null)
      {
        file.Close();
        file = null;
      }
    }

The Stream was closing on the WCF side while the client was receiving the Stream. 客户端接收Stream时,Stream在WCF端关闭。

This is where i found my answer: IOException on streamed file upload via WCF over HTTP 这是我找到答案的地方: 通过HTTP通过WCF上传流文件时出现IOException

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM