wcf | dotnet

Download file using WCF REST Service

Download file using WCF Service

Abhith Rajan
Abhith RajanMarch 07, 2018 · 2 min read · Last Updated:

Recently, We deployed a web application in a load balanced environment. And one of the features in the app was to download some data as excel and it was working fine in DEV (as usual) but keep failing in the PROD. When I checked it on the individual servers, all worked fine but not under SLB (Software Load Balancing).

So I reviewed the code and found out that there are two requests being sent, one after another, one for generating the file in the server and another one for fetching the generated file. Pretty bad implementation, right? When is under load balanced environment, the first request may be served from one server and the second from another server. And there was no shared storage space (we used OSS). No sticky session either. So file generated in one server and it is being requested in another server which leads to 404.

To solve, We had to combine both, i.e generate the file and serve it on the same request. To do that, our code look like this,

Service Contract Interface

1[WebGet(UriTemplate = "{cultureName}/download/{someId}")]
3Stream GetReportByDate(string cultureName, string someId);

Of course it is a HTTP GET method. And the implementation looks like below,

1public Stream GetReportByDate(string cultureName, string someId)
3 try
4 {
5 ...
6 // Generate file the usual way
7 var filePath = SomeWayGenerateFileAndReturnFilePath();
8 if (WebOperationContext.Current != null)
9 {
10 WebOperationContext.Current.OutgoingResponse.Headers["Content-Disposition"] =
11 "attachment; filename=" + fileNameWithExtension;
12 WebOperationContext.Current.OutgoingResponse.ContentType = "application/octet-stream";
13 }
14 return File.OpenRead(filePath);
15 }
16 catch (Exception exception)
17 {
18 throw new WebProtocolException(HttpStatusCode.InternalServerError, $"ERROR: Operation faild. {exception.Message}", exception.InnerException);
19 }

Here ContentType can be whatever it is, if you know it. application/octet-stream is appropriate for entitites whose sole intended purpose is to be saved to disk. If you only generates say PDF, then the content type part can be changed to,

2 = "attachment; filename=MyFileName.pdf";
4 = "application/pdf";

That’s it.

Abhith Rajan

Written by Abhith Rajan
Abhith Rajan is an aspiring software engineer with more than nine years of experience and has a proven successful track record of delivering technology-based products and services.

This page is open source. Noticed a typo? Or something unclear?
Improve this page on GitHub

Related ArticlesView All

Related VideosView All

High-performance logging in .NET, the proper way

Fixing sync over async issues in .NET / C# with Visual Studio 2022

Machine Learning for .NET Developers Introducing ML NET

Related StoriesView All

Related Tools & ServicesView All


Try .NET | Runnable .NET code on your site

Try .NET is a service that allows you to embed runnable .NET code snippets into your website, blog, or documentation. Go beyond copy and paste samples to live snippets.