无法将数据写入传输连接:已建立的连接已被主机中的软件中止

时间:2015-06-16 09:00:24

标签: c# wcf

我正在编写适用于WCF服务的上传控件,当我尝试上传超过1MB的文件时,我发现此错误:

{System.IO.IOException: Unable to write data to the transport connection: 
 An established connection was aborted by the software in your host machine. 
---> System.Net.Sockets.SocketException: An established connection was aborted by the 
     software in your host machine
at System.Net.Sockets.Socket.Send(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
at System.Net.Sockets.NetworkStream.Write(Byte[] buffer, Int32 offset, Int32 size)
--- End of inner exception stack trace ---
at System.Net.Sockets.NetworkStream.Write(Byte[] buffer, Int32 offset, Int32 size)
at System.Net.PooledStream.Write(Byte[] buffer, Int32 offset, Int32 size)
at System.Net.ConnectStream.InternalWrite(Boolean async, Byte[] buffer, Int32 offset, Int32 size, AsyncCallback callback, Object state)
at System.Net.ConnectStream.Write(Byte[] buffer, Int32 offset, Int32 size)}

我不知道这是否与json max length参数有关。 我知道在web应用程序中我可以将它添加到web.config文件

<system.web.extensions>
<scripting>
  <webServices>
    <jsonSerialization maxJsonLength="50000000"/>
  </webServices>
</scripting>

但我是WCF的新手并且不知道如何处理它......

2 个答案:

答案 0 :(得分:0)

你可能会超时。首先,在绑定中打开keepalive。其次,检查请求上的时间戳并回复。如果发送方和接收方之间存在防火墙,请确保由于空闲超时而未关闭连接。

答案 1 :(得分:0)

我解决了。

  1. 删除[OperationContract],只留下界面中的<system.web> <compilation debug="true" targetFramework="4.0" /> <httpRuntime maxRequestLength="2147483647" /> </system.web> <system.serviceModel> <bindings> <webHttpBinding> <binding name="WebHttpBinding_IAttachmentService" closeTimeout="10:00:00" openTimeout="10:00:00" receiveTimeout="10:00:00" sendTimeout="10:00:00" maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647"> <readerQuotas maxDepth="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" maxStringContentLength="2147483647"/> <security mode="None" /> </binding> </webHttpBinding> </bindings> <client> <endpoint address="http://localhost:54893/AttachmentService.svc" binding="webHttpBinding" bindingConfiguration="WebHttpBinding_IAttachmentService" behaviorConfiguration="webHttpBehavior" contract="XXX.Interfaces.IAttachmentService" /> </client> <behaviors> <endpointBehaviors> <behavior name="webHttpBehavior"> <webHttp/> </behavior> </endpointBehaviors> </behaviors> </system.serviceModel> <system.webServer> <modules runAllManagedModulesForAllRequests="true"/> <security> <requestFiltering> <requestLimits maxAllowedContentLength="1073741824" /> </requestFiltering> </security> </system.webServer>
  2. 使用这些配置文件
  3. 客户端配置:

    <system.web>
        <compilation debug="true" targetFramework="4.0" />
        <httpRuntime maxRequestLength="2147483647" executionTimeout="3600" />
      </system.web>
    
      <system.serviceModel>
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
        <services>
          <service name="XXX.Implementation.AttachmentService">
            <endpoint binding="webHttpBinding"
                      behaviorConfiguration="webHttpBehavior"
                      bindingConfiguration="higherMessageSize"
                      contract="XXX.Interfaces.IAttachmentService" />
          </service>
        </services>
        <bindings>
          <webHttpBinding>
            <binding name="higherMessageSize" transferMode="Streamed" closeTimeout="10:00:00" openTimeout="10:00:00" receiveTimeout="10:00:00" sendTimeout="10:00:00"
                     maxBufferSize="2147483647"
                     maxBufferPoolSize="2147483647"
                     maxReceivedMessageSize="2147483647">
    
              <readerQuotas maxDepth="2147483647"
                            maxArrayLength="2147483647"
                            maxBytesPerRead="2147483647"
                            maxNameTableCharCount="2147483647"
                            maxStringContentLength="2147483647"/>
    
              <security mode="None"/>
            </binding>
          </webHttpBinding>
        </bindings>
    
        <behaviors>
          <serviceBehaviors>
            <behavior>
              <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
              <serviceDebug includeExceptionDetailInFaults="false"/>
            </behavior>
          </serviceBehaviors>
          <endpointBehaviors>
            <behavior name="webHttpBehavior">
              <webHttp/>
            </behavior>
          </endpointBehaviors>
        </behaviors>
      </system.serviceModel>
    
      <system.webServer>
        <modules runAllManagedModulesForAllRequests="true">
          <remove name="WebDAVModule"/>
        </modules>
         <handlers>
             <remove name ="WebDAV"/>
        </handlers>
        <security>
          <requestFiltering>
            <requestLimits maxAllowedContentLength="2147483647" />
          </requestFiltering>
        </security>
      </system.webServer>
    

    服务配置:

    future.isCompleted()