WCF使用MTOM上传50Mg文件

时间:2012-03-05 22:58:09

标签: c# wcf mtom

使用WCF上传50MG文件的最佳方式是什么?

我以为MTOM会自动处理这件事 我想我错了......

即使在LocalHost上运行也会发生异常 (我假设使用外部IIS会更糟糕)。

异常消息:

  

“接收HTTP响应时发生错误   http://localhost:7064/DataSyncService.svc。这可能是由于   服务端点绑定不使用HTTP协议。这也可以   是由于服务器中止了HTTP请求上下文   (可能由于服务关闭)。请参阅服务器日志了解更多   方式“。

服务器配置

    <system.serviceModel>
    <client>
        <remove contract="IMetadataExchange" name="sb" />
    </client>
    <bindings>
        <wsHttpBinding>
            <binding name="DataSyncBinding" messageEncoding="Mtom" maxReceivedMessageSize ="50000000" maxBufferPoolSize="50000000">
                <readerQuotas maxArrayLength="50000000" />
                <security mode="None" />
            </binding>
        </wsHttpBinding>
    </bindings>
    <services>
        <service behaviorConfiguration="Server.DataSyncServiceBehavior" name="Server.DataSyncService">
            <endpoint address="" binding="wsHttpBinding" bindingConfiguration="DataSyncBinding" name="DataSyncService"
                      contract="Server.IDataSyncService" />
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        </service>
    </services>
    <behaviors>
        <serviceBehaviors>
            <behavior name="Server.DataSyncServiceBehavior">
                <serviceMetadata httpGetEnabled="true" />
                <serviceDebug includeExceptionDetailInFaults="false" />
            </behavior>
        </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="false" />
</system.serviceModel>

客户端配置

    <system.serviceModel>
    <bindings>
        <wsHttpBinding>
            <binding name="DataSyncService" closeTimeout="00:05:00" openTimeout="00:05:00"
                receiveTimeout="00:30:00" sendTimeout="00:30:00" bypassProxyOnLocal="false"
                transactionFlow="false" hostNameComparisonMode="StrongWildcard"
                maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                messageEncoding="Mtom" textEncoding="utf-8" useDefaultWebProxy="true"
                allowCookies="false">
                <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                    maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                <reliableSession ordered="true" inactivityTimeout="00:10:00"
                    enabled="false" />
                <security mode="None">
                    <transport clientCredentialType="Windows" proxyCredentialType="None"
                        realm="" />
                    <message clientCredentialType="Windows" negotiateServiceCredential="true" />
                </security>
            </binding>
        </wsHttpBinding>
    </bindings>
    <client>
        <endpoint address="http://localhost:7064/DataSyncService.svc"
            binding="wsHttpBinding" bindingConfiguration="DataSyncService"
            contract="DataSyncServiceReference.IDataSyncService" name="DataSyncService" />
    </client>
</system.serviceModel>

先谢谢。

1 个答案:

答案 0 :(得分:2)

如果文件完全 50 MB,则maxReceivedMessageSize和maxArrayLength设置得太低,因为50MB实际上是50 * 1024 * 1024 = 52428800字节。此外,maxReceivedMessageSIze必须考虑消息本身的结构(SOAP Envelope等),而不仅仅是数据。

您可以使用流式传输,这是建议使用大型文件的方法。不幸的是,它不适用于wsHttpBinding,您可能需要使用basicHttpBinding或webHttpBinding之类的东西来启用流式传输。

有关流媒体的更多信息:

http://msdn.microsoft.com/en-us/library/ms733742.aspx