需要在wcf服务中传输大数据。获取错误system.outof内存异常

时间:2012-01-24 23:18:17

标签: wcf

服务器代码

public class SIIUnzipDecodeService : ISIIUnzipDecodeService
{
    //Upload the large data file

    public void UploadFile(RemoteFileInfo request)
    {
        FileStream targetStream = null;
        Stream sourceStream = request.FileByteStream;

        string uploadFolder = @"C:\upload\";
        string filePath = @"C:\upload\1GB.zip";
        //Path.Combine(uploadFolder, request.FileName);

        using (targetStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None))
        {
            //read from the input stream in 6K chunks
            //and save to output stream
            const int bufferLen = 65000;
            byte[] buffer = new byte[bufferLen];
            int count = 0;

            while ((count = sourceStream.Read(buffer, 0, bufferLen)) > 0)
            {
                targetStream.Write(buffer, 0, count);
            }

            targetStream.Close();
            sourceStream.Close();
        }

    }

接口。

[ServiceContract]
public interface ISIIUnzipDecodeService
{
    [OperationContract]
    void UnzipAndDecode(MemoryStream fileContent);

    [OperationContract]
    RemoteFileInfo DownloadFile(DownloadRequest request);

    [OperationContract]
    void UploadFile(RemoteFileInfo request);
}

[MessageContract]
public class DownloadRequest
{
    [MessageBodyMember]
    public string FileName;
}

[MessageContract]
public class RemoteFileInfo : IDisposable
{
    [MessageHeader(MustUnderstand = true)]
    public string FileName;

    [MessageHeader(MustUnderstand = true)]
    public long Length;

    [MessageBodyMember(Order = 1)]
    public System.IO.Stream FileByteStream;

    public void Dispose()
    {
        if (FileByteStream != null)
        {
            FileByteStream.Close();
            FileByteStream = null;
        }
    }
}

2 个答案:

答案 0 :(得分:1)

您可以尝试在服务上启用Tracing吗?另外,请发布您的服务配置。确保您已在服务配置中设置以下内容:

<dataContractSerializer maxItemsInObjectGraph="2147483647"/>

<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
            maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />

<basicHttpBinding>
        <binding transferMode="Buffered"/>
</basicHttpBinding>

答案 1 :(得分:0)

<basicHttpBinding>
      <binding closeTimeout="00:30:00"
          openTimeout="00:30:00" receiveTimeout="00:30:00" sendTimeout="00:30:00"
          allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
          maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647"
          messageEncoding="Text" 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>