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.

web.config of the Service adjusted to allow for large file transfers

web.config of the Service adjusted to allow for large file transfers

 

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

allowing IIS to crunch a request this large

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.

 

In a previous post i explained briefly how you can make a simple wcf service. In this post i’ll try to explain how you can extend that simple service to provide an RSS feed.

In fact, it’s a pretty easy task. It comes down to adding a single method to your service contract, implementing the method and then using it in a webpage. This webpage will render our feed.
So that’s 3 steps and i’ll run them down for you:

Preparing your WCF service to provide the RSS feed:

[OperationContract]
[WebGet]
Rss20FeedFormatter GetRSSFeed();

This method is added to your IService (or IWhateverService). The Rss20FeedFormatter serializes a SyndicationFeed to and from RSS 2.0. That’s pretty cool.

public System.ServiceModel.Syndication.Rss20FeedFormatter GetRSSFeed()
{
SyndicationFeed feed = new SyndicationFeed("feedTitle", "feedDescription", new Uri("http://localhost:8000/RSS"));
feed.Authors.Add(new SyndicationPerson("dries@fakemail.be"));

List items = new List();

SyndicationItem newItem = new SyndicationItem();
newItem.Title = new TextSyndicationContent(String.Format("item title"));

newItem.PublishDate = DateTime.Now;
newItem.Content = new TextSyndicationContent("html content of your post", TextSyndicationContentKind.XHtml);
items.Add(newItem);
feed.Items = items;
return new Rss20FeedFormatter(feed);
}

As you can see, this is the method that does all of the work. What we do here is create “the feed”, give it a name and description and then add a list of feed items.
Obviously, in a production environment you’ll want to fill the list based on a database query or something similar.

As you can see you can give the inner content of your rss post a html string. That works great and you can put images and links in your posts this way.

After the list is filled up with all our fine content we send it to the requesting client. In this case the client will be our webpage.

Setting up the webpage that will render the RSS feed

Create a new webpage in your site project and in the Page_Load request the feed from the WCF service.

protected void Page_Load(object sender, EventArgs e)
{
Service.ServiceClient Service = new Service.ServiceClient();
try
{
Rss20FeedFormatter feed = Service.GetRSSFeed(20, SessionProvider.CustomerID, "feed title", "feed description", Request.Url.OriginalString.Replace("/rss.aspx", ""));
Service.Close();
XmlWriter feedWriter = XmlWriter.Create(Response.OutputStream);

feed.WriteTo(feedWriter);
feedWriter.Close();
}
catch (Exception)
{

throw;
}
}

When all went well you can now surf to your rss.aspx page and you’ll see your rss feed.


So that’s basically all it takes. Ofcourse, this is a brief example again of what you can do but i hope it gives you a clearer sight on how to handle things.
It’s merely meant as a basis from which you can create something more complex.

kick it on DotNetKicks.com

© 2012 Dries Hoebeke on .NET Suffusion theme by Sayontan Sinha