最近要做一个上传功能的WCF,20M 70M都测试成后,但是在测试130M的时候,却跳出
"Failed to allocate a managed memory buffer of 268435456 bytes."
错误,
wcf web.config
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="VODServiceBinding" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" messageEncoding="Mtom">
<readerQuotas maxArrayLength="2147483647" maxStringContentLength="2147483647"/>
</binding>
</basicHttpBinding>
</bindings>
<services>
<service behaviorConfiguration="VOD.VODServiceBehavior"
name="VOD.VODService">
<endpoint address="" binding="basicHttpBinding" bindingConfiguration="VODServiceBinding" contract="VOD.IVODService" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="VOD.VODServiceBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
<dataContractSerializer maxItemsInObjectGraph="2147483647" ignoreExtensionDataObject="true" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="VODServiceBinding" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" messageEncoding="Mtom">
<readerQuotas maxArrayLength="2147483647" maxStringContentLength="2147483647"/>
</binding>
</basicHttpBinding>
</bindings>
<services>
<service behaviorConfiguration="VOD.VODServiceBehavior"
name="VOD.VODService">
<endpoint address="" binding="basicHttpBinding" bindingConfiguration="VODServiceBinding" contract="VOD.IVODService" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="VOD.VODServiceBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
<dataContractSerializer maxItemsInObjectGraph="2147483647" ignoreExtensionDataObject="true" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
client web.config
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IVODService" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:30:00" sendTimeout="00:30:00"
allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferSize="2147483647" maxBufferPoolSize="524288" maxReceivedMessageSize="2147483647"
messageEncoding="Mtom" transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647"
maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://xxxxxx.com/VODService.svc" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IVODService" contract="VODService.IVODService"
name="BasicHttpBinding_IVODService" />
</client>
</system.serviceModel>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IVODService" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:30:00" sendTimeout="00:30:00"
allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferSize="2147483647" maxBufferPoolSize="524288" maxReceivedMessageSize="2147483647"
messageEncoding="Mtom" transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647"
maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://xxxxxx.com/VODService.svc" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IVODService" contract="VODService.IVODService"
name="BasicHttpBinding_IVODService" />
</client>
</system.serviceModel>
然后从MSDN的论坛里找到了一种方法,就是分段把文件内容传到服务器上,追加到文件结尾
Code
byte[] buffer = new byte[65000];
int bytesRead = 0;
VODService.IVODService proxy = client.ChannelFactory.CreateChannel();
while ((bytesRead = fuUpload.PostedFile.InputStream.Read(buffer, 0, 65000)) > 0)
{
video.Data = buffer;
video.Offset = 0;
video.Count = bytesRead;
proxy.UploadVideo(video);
}
client.ChannelFactory.Close();
byte[] buffer = new byte[65000];
int bytesRead = 0;
VODService.IVODService proxy = client.ChannelFactory.CreateChannel();
while ((bytesRead = fuUpload.PostedFile.InputStream.Read(buffer, 0, 65000)) > 0)
{
video.Data = buffer;
video.Offset = 0;
video.Count = bytesRead;
proxy.UploadVideo(video);
}
client.ChannelFactory.Close();
我想可能是一次传的文件放在内存里太大了
希望对大家有帮助