WCF 4: Download Files using WCF REST EndPoints
Posted by: Mahesh Sabnis
in Category Windows Communication Foundation (WCF)
Abstract: This article demonstrates how to make use of WCF REST service for downloading files.
While discussing about the content download facility development using WCF with one of my clients, I proposed the solution of defining operation contracts with a Stream return type from WCF services and consuming it in client application. However the client’s requirement was to make use of the WCF REST service for file downloading e.g. for .jpg, png, docs etc. WCF REST services provides XML or JSON conversion for the response from the WCF service , however the question that was bothering me was how to make use of REST for file download?
After searching the System.ServiceModel.Web namespace, I found the ‘WebOperationContext’ class. This is a helper class that provides easy access to the contextual properties of the Web request and response communication. Then while working with this class I found that using the WebOperationContext.Current.OutgoingResponse.ContentType, the content type header from the outgoing web response can be managed with the value: “application/octet-stream”. This represents a generic value for Raw binary data which can contain format like, png,jpg,zip,doc etc. So I thought to make a use of this class for our application
Step 1: Open VS2010 and create a WCF service application and name it ‘WCF_FileDownload_REST’. Add a folder named ‘FileServer’ in the application with following files: (Note: You can enter your files here)
Step 2: Rename IService1.cs to IService.cs and Service1.svc to Service.svc. Open IService.cs and write the following ServiceContract in it:
The above code shows the OperationContract with WebGet attribute. The WebGet attribute is set with UriTemplate property to make the REST url user friendly.
Step 3: Open Service.svc.cs and implement the IService in the service class as shown below:
The DownloadFile method accepts two parameters fileName and fileExpension. The code then generates the file path using FileServer folder in the WCF application and generates the outgoing response using the raw binary format.
Step 4 : Right-Click on Service.svc and select View Markup and change the @ServiceHost as below:
Step 5: Open Web.Config file and add the webHttpBinding protocol mapping so that the WCF REST endpoint can be exposed and also define the Help page for the WCF service as below:
Step 6: Publish the WCF service on your IIS web server and view the Service.svc in the browser. The result should be as shown below:
Click on the ‘Service Help page’ and the user friendly url will be displayed:
In the browser, type the url as shown below:
Here the file is ADOEF.png. Similarly also try to download the Docx/Excel/zip file and you should get a similar download experience.
The advantage of this approach is that the client who knows about this can now directly get connected to your WCF File Download service and use the download facility without using any file IO code.
The entire source code of this article can be download over here
This article has been editorially reviewed by Suprotim Agarwal.
C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.
We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).
Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.
Click here to Explore the Table of Contents or Download Sample Chapters!