Large Binary (byte[]) File transfer through WCF

那年仲夏 提交于 2019-11-28 04:44:11

(While I agree that streaming transfer would be preferrable, the below should make it work without any other changes)

You also need to increase the maximum message length in the Web.config:

<configuration>
  <system.web>
  <httpRuntime maxMessageLength="409600"
    executionTimeoutInSeconds="300"/>
  </system.web>
</configuration>

This will set the maximum message length to 400 MB (parameter is in kB). Check this MSDN page for more information.

As pointed out, try using Streaming Transfer, here's some example code showing both sending and receiving (possibly) large amounts of data using streamed transfer.

Use a binding like this, notice the MaxReceivedMessageSize and TranferMode settings.

<binding name="Streaming_Binding" maxReceivedMessageSize="67108864"  
    messageEncoding="Text" textEncoding="utf-8" transferMode="Streamed">
</binding>

Add some service code:

[OperationContract]
public Stream GetLargeFile()
{
    return new FileStream(path, FileMode.Open, FileAccess.Read);
}

[OperationContract]
public void SendLargeFile(Stream stream)
{
    // Handle stream here - e.g. save to disk    
    ProcessTheStream(stream);

    // Close the stream when done processing it
    stream.Close();
}

And some client code:

public Stream GetLargeFile()
{
    var client = /* create proxy here */;
    try
    {
        var response = client.GetLargeFile();

        // All communication is now handled by the stream, 
        // thus we can close the proxy at this point
        client.Close();

        return response;
    }
    catch (Exception)
    {
        client.Abort();
        throw;
    }
}

public void SendLargeFile(string path)
{
    var client = /* create proxy here */;
    client.SendLargeFile(new FileStream(path, FileMode.Open, FileAccess.Read));
}

Also, make sure you are not getting a timeout, a large file might take a while to transfer (the default receiveTimeout is 10 minutes though).

You can download Microsoft WCF/WF sample code here (top C# link is broken at the time of writing but other samples code seems ok).

Have you had a look at using Streaming Transfer?

Windows Communication Foundation (WCF) can send messages using either buffered or streamed transfers. In the default buffered-transfer mode, a message must be completely delivered before a receiver can read it. In streaming transfer mode, the receiver can begin to process the message before it is completely delivered. The streaming mode is useful when the information that is passed is lengthy and can be processed serially. Streaming mode is also useful when the message is too large to be entirely buffered.

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

I'll echo what others have said and say that using a Streaming Transfer is the way to go when using Windows Communication Foundation. Below is an excellent guide that explains all of the steps in order to stream files over WCF. It's quite comprehensive and very informative.

Here it is: Guide on Streaming Files over WCF.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!