如何将数组添加到WCF数据协定?

时间:2019-05-28 20:44:25

标签: wcf

我试图在用Visual Studio 2012编写的Windows Communication Foundation项目中使用以下类:

[DataContract]
public class SimCoilData : SimInventoryItemData
{
    [DataMember]
    public UnitTemperature? RequiredCoolingHotspot { get; set; }
    [DataMember]
    public UnitTemperature? RequiredHeatingColdspot { get; set; }
    [DataMember]
    public UnitTemperature? HeatingHotspotLimit { get; set; }

    [DataMember]
    public TimeSpan? TimeToRequiredColdspot { get; set; }
    [DataMember]
    public TimeSpan? TimeToRequiredCoolingHotspot { get; set; }
    [DataMember]
    public TimeSpan? TimeToHeatingHotspotLimit { get; set; }

    [DataMember]
    public UnitLength Gauge { get; set; }
    [DataMember]
    public UnitLength Width { get; set; }
    [DataMember]
    public double[] Temperatures { get; set; }

    public SimCoilData() : base() 
    {

    }

}

在添加Temperatures数组之前,服务器和客户端之间的数据交换良好。我添加了Temperatures数组,重建了服务器应用程序,并运行了它。然后,在客户端项目中,我更新了服务参考并重建了项目。运行客户端时,收到一条错误消息,提示您收到了毫无意义的回复,这可能是由某些原因引起的,包括数据合同不匹配。我已经验证了服务器端正在填充该阵列。调整客户端应用程序以接受数组的正确步骤是什么?

1 个答案:

答案 0 :(得分:0)

问题似乎是我没有在客户端中为消息保留足够的空间。我发现我可以处理每个对象250次,但不能处理300次。 Google搜索“ WCF最大邮件大小”导致我找到maxBufferSize和maxReceivedMessageSize参数。通过为我的客户端进行此配置,我可以在数组中获取所有1800个值:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
    <system.serviceModel>
      <bindings>
        <netTcpBinding>
          <binding name="NetTcpBinding_ISimShopServiceLib"
            openTimeout="00:01:00"
            closeTimeout="00:00:05"
            sendTimeout="00:01:00"
            receiveTimeout="00:01:00"
            maxBufferSize="5000000"
            maxReceivedMessageSize="5000000">
          </binding>           
        </netTcpBinding>
      </bindings>
      <client>
        <endpoint address="net.tcp://localhost:1236/SimShopService" binding="netTcpBinding"
          bindingConfiguration="NetTcpBinding_ISimShopServiceLib" contract="SimShopServiceReference.ISimShopServiceLib"
          name="NetTcpBinding_ISimShopServiceLib">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
      </client>

    </system.serviceModel>
</configuration>