2

我有 WCF 服务,我正在发送以下数据合同

[DataContract]
public class Sample
{
    [DataMember]
    public int Type { get; set; }

    [DataMember]
    public string Name { get; set; }

    [DataMember]
    public string Value { get; set; }

    [DataMember]
    public byte[] ByteList { get; set; }
}

但是随着字节数组大小的增加,客户端不接受数据。他们成功接收到其他消息。我尝试增加.config 中的大小。我还尝试在使用 DataContractSerializer 序列化后发送对象,但对我没有任何作用。我知道这是我这边的一些错误,但我无法弄清楚。请让我知道你的意见

<netTcpBinding>
    <binding name="tcpbinding" closeTimeout="00:01:00"
        openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
        transactionFlow="false" transferMode="Buffered" transactionProtocol="OleTransactions"
        hostNameComparisonMode="StrongWildcard" listenBacklog="10"
        maxBufferPoolSize="524288" maxBufferSize="2147483646" maxConnections="10"
        maxReceivedMessageSize="2147483646">
      <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
      <reliableSession ordered="true" inactivityTimeout="00:10:00"
          enabled="true" />
      <security mode="None">
        <transport clientCredentialType="Windows" protectionLevel="EncryptAndSign"/>
        <message clientCredentialType="Windows"/>
      </security>
    </binding>
  </netTcpBinding> 
4

2 回答 2

2

Welcome to the wonderful world of WCF settings...

How large can the array be before you start losing messages (i.e. what is the largest array you can transfer successfully)?

From my experience, the setting relating to arrays is maxArrayLength (not maxItemsInObjectGraph, which deals with hierarchical data). Make sure this setting (and also maxReceivedMessageSize) is larger than any ByteList length in both server and client configurations.

WCF hides its error messages well, but you can set up tracing to find them. On the server, add this under the configuration node in the web.config file:

<system.diagnostics>
  <trace autoflush="true" />
  <sources>
    <source name="System.ServiceModel.MessageLogging">
      <listeners>
        <add name="messages"
            type="System.Diagnostics.XmlWriterTraceListener"
            initializeData="c:\logs\messages.svclog" />
      </listeners>
    </source>
    <source name="System.ServiceModel"
            switchValue="Warning, Critical, Error, Verbose"
            propagateActivity="true">
      <listeners>
        <add name="sdt"
            type="System.Diagnostics.XmlWriterTraceListener"
            initializeData= "c:\logs\service.svclog" />
      </listeners>
    </source>
  </sources>
</system.diagnostics>

<system.serviceModel>
  <diagnostics>
    <messageLogging
         logEntireMessage="true"
         logMalformedMessages="true"
         logMessagesAtServiceLevel="true"
         logMessagesAtTransportLevel="true"
         maxMessagesToLog="3000000"
         maxSizeOfMessageToLog="2000000"/>
  </diagnostics>
</system.serviceModel>

Note

  • You must manually create the log files folder (here: c:\logs) on the server first.
  • With these settings, the log files (service.svclog in particular) grow huge very fast. Only turn on this kind of logging when you really need it.

References

The latter also shows you how to open and read the log files. You should see at least one error message in service.svclog after you make a service call that doesn't return a message.

于 2012-12-27T18:31:06.610 回答
0

只需添加

<httpRuntime maxRequestLength="2048000" executionTimeout="3600" />

到你的配置。

如果那不能解决问题,请添加

<dataContractSerializer maxItemsInObjectGraph="2147483647"/>

也像下面

<behaviors>
      <serviceBehaviors>        
        <behavior name="myNetTcpBehaviour">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
          <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="myNetTcpEndPointBehaviour">
          <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
于 2012-12-21T04:47:16.423 回答