user359562
user359562

Reputation: 57

Need to stream large data in wcf services. Getting error system.outof memory exception

Server code

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();
        }

    }

Interface.

[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;
        }
    }
}

Upvotes: 0

Views: 854

Answers (2)

user359562
user359562

Reputation: 57

<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>

Upvotes: 0

Rajesh
Rajesh

Reputation: 7886

Can you try enable Tracing on your service. Also please post your service configuration. Make sure that you have set the below things in your service configuration:

<dataContractSerializer maxItemsInObjectGraph="2147483647"/>

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

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

Upvotes: 1

Related Questions