【问题标题】:Sending file to WCF service, socket connection aborted将文件发送到 WCF 服务,套接字连接中止
【发布时间】:2016-01-19 05:18:17
【问题描述】:

我有一个 WCF 服务(服务器),其方法的签名是

public void SetProfilePic(String pSession, Applicant pApplicant)

申请人拥有财产:

public byte[] Photo { get; set; }

我从客户端得到一张图像并调用 SetProfilePic。当图像很小时,大约 1cm X 1cm,它可以完美地工作,但是,当图像较大时,4cm X 3cm,我得到下面的错误

"The socket connection was aborted. 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 '01:49:59.9779945'."

我在 app.config 文件中增加了 最大限制,但我仍然遇到同样的错误。我在 缓冲 模式下使用 NetTCPBinding。我也试过 MTOM。由于该项目仍处于初始开发阶段,因此客户端和服务当前都在同一台机器上运行。

我在服务器端和客户端都启用了消息记录和跟踪,但是日志中没有提供更多详细信息,这不是很有帮助。

请在下面找到客户端和服务器的 App.config 文件。

我对 WCF 的体验只有几个月。任何帮助表示赞赏。提前致谢。

客户端 app.config

    <?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.diagnostics>
        <sources>
            <source name="System.ServiceModel.MessageLogging" switchValue="Warning,ActivityTracing">
                <listeners>
                    <add type="System.Diagnostics.DefaultTraceListener" name="Default">
                        <filter type="" />
                    </add>
                    <add name="ServiceModelMessageLoggingListener">
                        <filter type="" />
                    </add>
                </listeners>
            </source>
            <source propagateActivity="true" name="System.ServiceModel" switchValue="Warning,ActivityTracing">
                <listeners>
                    <add type="System.Diagnostics.DefaultTraceListener" name="Default">
                        <filter type="" />
                    </add>
                    <add name="ServiceModelTraceListener">
                        <filter type="" />
                    </add>
                </listeners>
            </source>
        </sources>
        <sharedListeners>
            <add initializeData="c:\users\...\app_messages.svclog"
                type="System.Diagnostics.XmlWriterTraceListener, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
                name="ServiceModelMessageLoggingListener" traceOutputOptions="Timestamp">
                <filter type="" />
            </add>
            <add initializeData="c:\users\...\app_tracelog.svclog"
                type="System.Diagnostics.XmlWriterTraceListener, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
                name="ServiceModelTraceListener" traceOutputOptions="Timestamp">
                <filter type="" />
            </add>
        </sharedListeners>
    </system.diagnostics>
    <system.serviceModel>
        <diagnostics>
            <messageLogging logMalformedMessages="true" logMessagesAtServiceLevel="true"
                logMessagesAtTransportLevel="true" />
        </diagnostics>
        <bindings>
            <netTcpBinding>
                <binding name="NetTcpBinding" closeTimeout="01:50:00" openTimeout="01:50:00"
                    receiveTimeout="01:50:00" sendTimeout="01:50:00" maxBufferPoolSize="2147483647"
                    maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">
                    <readerQuotas maxDepth="128" maxStringContentLength="2147483647"
                        maxArrayLength="2147483647" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    <reliableSession inactivityTimeout="00:20:00" />
                </binding>
            </netTcpBinding>
        </bindings>
        <client>
            <endpoint address="net.tcp://localhost:9000/RecruitAidService"
                behaviorConfiguration="endpointBehavior" binding="netTcpBinding"
                bindingConfiguration="NetTcpBinding" contract="RecruitAidClientService.IRecruitAidService"
                name="NetTcpBinding" />
        </client>
      <behaviors>
        <endpointBehaviors>
          <behavior name="endpointBehavior">
            <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
          </behavior>
        </endpointBehaviors>
      </behaviors>
    </system.serviceModel>
</configuration>

服务/服务器 app.config

UpdateApplicantUpdateBy(pSession, applId);

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
  </configSections>
  <system.diagnostics>
    <sources>
      <source name="System.ServiceModel.MessageLogging" switchValue="Warning,ActivityTracing">
        <listeners>
          <add type="System.Diagnostics.DefaultTraceListener" name="Default">
            <filter type="" />
          </add>
          <add name="ServiceModelMessageLoggingListener">
            <filter type="" />
          </add>
        </listeners>
      </source>
      <source propagateActivity="true" name="System.ServiceModel" switchValue="Verbose,ActivityTracing">
        <listeners>
          <add type="System.Diagnostics.DefaultTraceListener" name="Default">
            <filter type="" />
          </add>
          <add name="ServiceModelTraceListener">
            <filter type="" />
          </add>
        </listeners>
      </source>
    </sources>
    <sharedListeners>
      <add initializeData="c:\users\...\app_messages.svclog"
        type="System.Diagnostics.XmlWriterTraceListener, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
        name="ServiceModelMessageLoggingListener" traceOutputOptions="Timestamp, Callstack">
        <filter type="" />
      </add>
      <add initializeData="c:\users\...\app_tracelog.svclog"
        type="System.Diagnostics.XmlWriterTraceListener, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
        name="ServiceModelTraceListener" traceOutputOptions="Timestamp">
        <filter type="" />
      </add>
    </sharedListeners>
  </system.diagnostics>
  <runtime>
    <loadFromRemoteSources enabled="true"/>
   </runtime>
      <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" />
  </system.web>
  <!-- When deploying the service library project, the content of the config file must be added to the host's 
  app.config file. System.Configuration does not support config files for libraries. -->
  <system.serviceModel>
    <diagnostics>
      <messageLogging logMalformedMessages="true" logMessagesAtServiceLevel="true"
        logMessagesAtTransportLevel="true" />
    </diagnostics>
    <services>
      <service name="RecruitAidServer.RecruitAidService">
        <clear />
        <endpoint address="net.tcp://localhost:9000/RecruitAidService"
          binding="netTcpBinding" bindingConfiguration="" name="NetTcpBinding"
          contract="RecruitAidServer.IRecruitAidService" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8733/Design_Time_Addresses/RecruitAidServer/Service1/" />
          </baseAddresses>
          <timeouts closeTimeout="00:01:50" />
        </host>
      </service>
    </services>

    <bindings>
      <basicHttpBinding>
        <binding name="NetTcpBinding" closeTimeout="01:50:00" openTimeout="01:50:00"
          receiveTimeout="01:50:00" sendTimeout="01:50:00" maxBufferPoolSize="21474836470000"
          maxBufferSize="2147483647" maxReceivedMessageSize="21474836470000"
          transferMode="Buffered" messageEncoding="Mtom">
          <readerQuotas maxDepth="128" maxStringContentLength="2147483647"
            maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="16384" />
        </binding>
      </basicHttpBinding>
    </bindings>

    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, 
          set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="True" httpsGetEnabled="True" />
          <!-- To receive exception details in faults for debugging purposes, 
          set the value below to true.  Set to false before deployment 
          to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="False" />
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="endpointBehavior">
          <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
  </system.serviceModel>

</configuration>

【问题讨论】:

  • 奇怪,配置看起来不错。我唯一提到的:在服务器端 endpointBehavior 并没有真正应用于服务,因为没有指定 behaviorConfiguration。
  • bindingConfiguration 相同,为空。您应该将其设置为 NetTcpBinding。我也没有得到你使用哪种绑定。您已配置 basicHttpBinding,但服务使用 netTcpBinding

标签: wcf c#-4.0 bytearray wcf-binding


【解决方案1】:

服务器端的服务配置应该是这样的

<endpoint address="net.tcp://localhost:9000/RecruitAidService"
    binding="netTcpBinding" bindingConfiguration="NetTcpBinding"
    name="NetTcpBinding" behaviorConfiguration="endpointBehavior"
    contract="RecruitAidServer.IRecruitAidService" />

和绑定

<bindings>
  <netTcpBinding>
    <binding name="NetTcpBinding" closeTimeout="01:50:00" openTimeout="01:50:00"
      receiveTimeout="01:50:00" sendTimeout="01:50:00" maxBufferPoolSize="21474836470000"
      maxBufferSize="2147483647" maxReceivedMessageSize="21474836470000"
      transferMode="Buffered" messageEncoding="Mtom">
      <readerQuotas maxDepth="128" maxStringContentLength="2147483647"
        maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="16384" />
    </binding>
  </netTcpBinding>
</bindings>

【讨论】:

  • 非常感谢 Mimas 的帮助。您提供的上述配置解决了该问题。我还删除了 messageEncoding="Mtom" 再次感谢。
猜你喜欢
  • 1970-01-01
  • 2017-01-06
  • 2011-04-28
  • 1970-01-01
  • 1970-01-01
  • 2019-12-28
  • 2011-10-15
  • 1970-01-01
  • 2011-07-09
相关资源
最近更新 更多