Click here to Skip to main content
15,880,796 members
Please Sign up or sign in to vote.
4.67/5 (3 votes)
See more:
Hello everybody!
I try to send very big file(about 4 GB) to WCF service from client.
But I get error about low memory. Should I separate whole file into the parts or not?

code snippet:

Client side:

WCFClient.ServiceReference1.FileTransferServiceClient client=
                new WCFClient.ServiceReference1.FileTransferServiceClient();
            string uploadfilename = "Upload.txt";

            using (FileStream fs = new FileStream(@"C:\"+uploadfilename, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                client.UploadFile(uploadfilename, fs); <font color="red">// There is I have an error!!!</font>
            }


Server side:

[ServiceContract]
    public interface IFileTransferService
    {
        [OperationContract(IsOneWay = true)]
        void UploadFile(FileUploadMessage request);
        [OperationContract(IsOneWay = false)]
        FileDownloadReturnMessage DownloadFile(FileDownloadMessage request);
    }

    [MessageContract]
    public class FileUploadMessage
    {
        [MessageHeader(MustUnderstand = true)]
        public string Filename;
        [MessageBodyMember(Order = 1)]
        public Stream FileByteStream;
    }

    [MessageContract]
    public class FileDownloadMessage
    {
        [MessageHeader(MustUnderstand = true)]
        public string Filename;
    }

    [MessageContract]
    public class FileDownloadReturnMessage
    {
        public FileDownloadReturnMessage(string filename, Stream stream)
        {
            this.DownloadedFilename = filename;
            this.FileByteStream = stream;
        }

        [MessageHeader(MustUnderstand = true)]
        public string DownloadedFilename;
        [MessageBodyMember(Order = 1)]
        public Stream FileByteStream;
    }

public class ServiceClass : IFileTransferServiceMarco
    {
public void UploadFile(FileUploadMessage request)
        {
            //try
            {
                string basePath = @"C:\Downloads";
                string serverFileName = Path.Combine(basePath, request.Filename);

                using (FileStream outfile = new FileStream(serverFileName, FileMode.Create))
                {
                    const int bufferSize = 65536; // 64K

                    Byte[] buffer = new Byte[bufferSize];
                    int bytesRead = request.FileByteStream.Read(buffer, 0, bufferSize);

                    while (bytesRead > 0)
                    {
                        outfile.Write(buffer, 0, bytesRead);
                        bytesRead = request.FileByteStream.Read(buffer, 0, bufferSize);
                    }
                }
            }
           // catch (IOException e)
            {
            //    throw new FaultException<ioexception>(e);
            }
        }

        public FileDownloadReturnMessage DownloadFile(FileDownloadMessage request)
        {
            string localFileName = request.Filename;
            try
            {
                string basePath = @"C:\Downloads";
                string serverFileName = Path.Combine(basePath, request.Filename);

                Stream fs = new FileStream(serverFileName, FileMode.Open);

                return new FileDownloadReturnMessage(request.Filename, fs);
            }
            catch (IOException e)
            {
                throw new FaultException<ioexception>(e);
            }            
        }
}</ioexception></ioexception>


and app.config:

endpoint address="winceStream"
	                  binding="basicHttpBinding"
	                  bindingConfiguration="HttpBinding_MTOM"
	                  contract="EssentialWCF.IFileTransferService" 
....
bindings
      basicHttpBinding
        binding name="HttpBinding_MTOM" messageEncoding="Mtom" transferMode="Streamed" maxReceivedMessageSize="4294967294">
          
        binding
      basicHttpBinding       
    bindings


Could anybody help me?

Sorry for my English :)
Posted
Updated 21-Apr-11 15:37pm
v3
Comments
HimanshuJoshi 21-Apr-11 21:37pm    
Edited to add pre tags around code.
_Ares!!! 21-Apr-11 21:56pm    
Thank you :)
dan!sh 21-Apr-11 22:23pm    
1. Could you provide the error message?
2. Did you tried setting ReaderQuotas?
_Ares!!! 21-Apr-11 22:29pm    
I've got this error: "Failed to allocate a managed memory buffer of 536870912 bytes. The amount of available memory may be low."

You can try increasing the values in ReaderQuota in your configuration. Might help.

BTW, you should be using Streaming rather than MTOM for sending large files IMHO. I tried MSDN help but could not find a decent example. This[^] is the one I could find. I remember reading a post from a guy called Carlos on MSDN forums. He too had posted a very good code sample for streaming file. Cannot find that post though. I will update the reply if I can hit it.
 
Share this answer
 
I've solved my problem!

You should add maxBufferSize and maxReceivedMessageSize in your app.config file.

For example:
Client side:
XML
<bindings>
            <basicHttpBinding>
<binding name="BasicHttpBinding_IFileTransferServiceMarco" closeTimeout="00:01:00"
                    openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:10:00"
                    allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                    maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="4294967294"
                    messageEncoding="Text" textEncoding="utf-8" transferMode="Streamed"
                    useDefaultWebProxy="true">
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    <security mode="None">
                        <transport clientCredentialType="None" proxyCredentialType="None"
                            realm="" />
                        <message clientCredentialType="UserName" algorithmSuite="Default" />
                    </security>
                </binding>
            </basicHttpBinding>
        </bindings>
 <endpoint address="http://localhost:8080/EssentialWCF/winceStream"
                binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IFileTransferServiceMarco"
                contract="ServiceReference1.IFileTransferServiceMarco" name="BasicHttpBinding_IFileTransferServiceMarco" />


Server side:
XML
<bindings>
      <basicHttpBinding>
        <binding name="HttpBinding_MTOM" messageEncoding="Text" transferMode="Streamed" maxReceivedMessageSize="4294967294"
                 maxBufferSize="65536" maxBufferPoolSize="65536"          
        </binding>
      </basicHttpBinding>
    </bindings>

<endpoint address="winceStream"
	                  binding="basicHttpBinding"
	                  bindingConfiguration="HttpBinding_MTOM"
	                  contract="EssentialWCF.IFileTransferServiceMarco" />


Good luck!
 
Share this answer
 
SQL
I've solved my problem!

You should add maxBufferSize and maxReceivedMessageSize in your app.config file.


<bindings>
<basichttpbinding>
<binding name="BasicHttpBinding_IFileTransferServiceMarco" closetimeout="00:01:00">
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:10:00"
allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="4294967294"
messageEncoding="Text" textEncoding="utf-8" transferMode="Streamed"
useDefaultWebProxy="true">
<readerquotas maxdepth="32" maxstringcontentlength="8192" maxarraylength="16384">
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<security mode="None">
<transport clientcredentialtype="None" proxycredentialtype="None"> realm="" />
<message clientcredentialtype="UserName" algorithmsuite="Default">




<endpoint address="http://localhost:8080/EssentialWCF/winceStream">
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IFileTransferServiceMarco"
contract="ServiceReference1.IFileTransferServiceMarco" name="BasicHttpBinding_IFileTransferServiceMarco" />




<bindings>
<basichttpbinding>
<binding name="HttpBinding_MTOM" messageencoding="Text" transfermode="Streamed" maxreceivedmessagesize="4294967294">
maxBufferSize="65536" maxBufferPoolSize="65536"




<endpoint address="winceStream">
binding="basicHttpBinding"
bindingConfiguration="HttpBinding_MTOM"
contract="EssentialWCF.IFileTransferServiceMarco" />
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900