1

我们正在使用 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>
4

2 回答 2

2

在同一台机器上尝试命名管道。

选择交通工具

于 2013-01-02T14:02:30.277 回答
1

尝试使用命名管道传输进行流式传输。有关详细信息,请参阅这篇文章大数据和流式传输

在将数据发送到服务器/客户端之前缓冲数据可能是个问题。流式传输将分块发送您的数据。

于 2013-01-02T16:02:57.113 回答