我们正在使用WCF将数据从服务器应用程序传输到多个客户端。实际上,对于大多数流量,客户端和服务器都在同一台机器上运行,所以我希望传输速度非常快。
但是,在传输大型阵列(16位灰度图像)时,传输数据需要几秒钟。对于16 MB的图像,大约需要3-5秒!
最初,我们使用ushort
数组,因为它是存储16位灰度图像数据的最合适的数据类型。然而,这非常缓慢:16 MB大约20-25秒。当我们在使用byte
进行序列化之前将数据复制到Buffer.BlockCopy
数组时,由于某种原因,它会减少到3-5秒。但是,在同一台机器上运行16 MB从一个应用程序转移到另一个应用程序的时间为3-5秒对我来说似乎太长了!
因此,我的问题是:我们如何改善此类情景的效果?
我已经调查了Marc Gravell的protobuf-net,但我不确定它是否会对这种情况有所帮助......有任何经验或其他建议吗?
以下是我们的一个数据类(包含图像数据)的源代码:
[DataContract(IsReference = true)]
public class ImageData
{
private ushort[] m_pixelData;
public ushort[] PixelData
{
get
{
return m_pixelData;
}
set
{
m_pixelData = value;
OnPropertyChanged("PixelData");
}
}
[DataMember]
public override byte[] FileData
{
get
{
if (this.PixelData == null)
{
return null;
}
return ListHelper.ConvertToByteArray(this.PixelData);
}
set
{
if (value == null)
{
this.PixelData = null;
return;
}
this.PixelData = ListHelper.ConvertToUshortArray(value);
}
}
}
请注意,只有FileData
属性被标记为[DataMember]
,因此PixelData
属性未被序列化!
以下是服务器app.config
的相关部分:
<system.serviceModel>
<services>
<service name="Services.ImageDataService" behaviorConfiguration="ServicesBehavior">
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:8008/ImageDataService" />
</baseAddresses>
</host>
<endpoint address="" binding="netTcpBinding" bindingConfiguration="NetTcpBindingLargeFileTransfer" contract="Services.IImageDataService" />
<endpoint address="mex" binding="mexTcpBinding" bindingConfiguration="" contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ServicesBehavior">
<serviceMetadata httpGetEnabled="false" />
<serviceDebug includeExceptionDetailInFaults="false" />
<dataContractSerializer maxItemsInObjectGraph="2147483647"/>
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<netTcpBinding>
<binding
name="NetTcpBindingLargeFileTransfer"
openTimeout="00:01:00" closeTimeout="00:01:00" receiveTimeout="infinite" sendTimeout="infinite"
transactionFlow="false" transactionProtocol="OleTransactions"
transferMode="Buffered" hostNameComparisonMode="StrongWildcard" listenBacklog="10"
maxBufferPoolSize="2097152" maxBufferSize="1073741824" maxConnections="10" maxReceivedMessageSize="1073741824">
<readerQuotas
maxDepth="2147483647" maxStringContentLength="2147483647"
maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
<reliableSession ordered="true" inactivityTimeout="infinite" enabled="false" />
<security mode="None">
<transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" />
<message clientCredentialType="Windows" />
</security>
</binding>
</netTcpBinding>
</bindings>
</system.serviceModel>
客户端的app.config
:
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding
name="NetTcpBindingLargeFileTransfer"
openTimeout="00:01:00" closeTimeout="00:01:00" receiveTimeout="infinite" sendTimeout="00:10:00"
transactionFlow="false" transactionProtocol="OleTransactions"
transferMode="Buffered" hostNameComparisonMode="StrongWildcard" listenBacklog="10"
maxBufferPoolSize="2097152" maxBufferSize="1073741824" maxConnections="10" maxReceivedMessageSize="1073741824">
<readerQuotas
maxDepth="2147483647" maxStringContentLength="2147483647"
maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
<reliableSession ordered="true" inactivityTimeout="infinite" enabled="false" />
<security mode="None">
<transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" />
<message clientCredentialType="Windows" />
</security>
</binding>
</netTcpBinding>
</bindings>
<client>
<endpoint
address="net.tcp://localhost:8008/ImageDataService"
binding="netTcpBinding"
bindingConfiguration="NetTcpBindingLargeFileTransfer"
contract="Services.IImageDataService"
name="Services.IImageDataService"
behaviorConfiguration="ServicesBehavior" />
</client>
<behaviors>
<endpointBehaviors>
<behavior name="ServicesBehavior">
<dataContractSerializer maxItemsInObjectGraph="2147483647"></dataContractSerializer>
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
答案 0 :(得分:2)
在同一台机器上尝试命名管道。
答案 1 :(得分:1)
尝试使用命名管道传输进行流式传输。有关详细信息,请参阅此帖Large Data and Streaming
在将数据发送到服务器/客户端之前缓冲数据可能会出现问题。 Streaming将以块的形式发送您的数据。