So you have your WCF Service all set up and now you want to allow users to upload – or download, big files to your webservice. This is perfectly possible using WCF, however you do need to adjust some of the settings in your custom wsHttpBinding.
Your service is by default configured not to be able to handle the larger transfers. This is simply to shield it from possible DOS attacks and other crap.
First of all i would like you to know how to make the transfer of your file as efficient as possible. By default your soap message is just text, if you use the default text encoding your transfer will be slow (yet very interopable).
Enter Mtom. Mtom stands for Soap Message Transfer Optimization Mechanism and this is basically designed to handle large binary messages. Advantages of MTOM are mostly that the transmitted data isn’t as bloated as is the data went sent in base64 encoding (33%!) and Mtom also is interopable; assuring other technologies to be able to use your sweet-ass webservice.
For this example i’ll use Mtom encoding because Mtom packs some cool advantages and provides the middle way; optimization and interoperability. If you do not care about interopability you should likely go for the Binary encoding.
So there’s 3 available encodings and you can find out all their advantages and disadvantages right here on msdn.
Here’s what MSDN says about Mtom:
The MTOM encoding transmits most XML in textual form, but optimizes large blocks of binary data by transmitting them as-is, without conversion to text. In terms of efficiency, among the encoders WCF provides, MTOM is in-between text (the slowest) and binary (the fastest).
Changing the message encoding alone wont get you any far though, you’ll still need to increase the size of message which your service is allowed to take in (or let out).
This you’ll configure in your custom binding as well; change the maxReceivedMessageSize to the desired size (i increased it to 8mb or 8.000.000 in my example).
In the readerQuotas section in your binding (you might need to add this yourself) you change the maxArrayLength to the same desired size. This simply allows the internal XML reader to take in this certain amount of data.
You are now almost ready!
Please make sure dat you also add under system.web in the httpRuntime “maxRequestLength” with as value your desired filesize. This allows IIS to process the filesize you please.
This bit is often let out of blogs and can cause you some headaches.
What msdn has to say about maxRequestLength:
Indicates the maximum file upload size supported by ASP.NET. This limit can be used to prevent denial of service attacks caused by users posting large files to the server. The size specified is in kilobytes. The default is 4096 KB (4 MB).
If you will work with many simultaneously running file up- or download it will be worth a look at the streaming capacities of WCF (which i’ll not touch in this short demo).
Hope this is any help! Please let me know about all the things i’ve gotten wrong; i’m quite flawed.
