Web API 2 : Returning binary file with “application/octet-stream” not working with IIS Server : IIS/10.0 but working with IIS/8.5

Girish Khadse 1 Reputation point
2021-08-24T05:25:33.67+00:00

I have web api which returns binary file, which is consumed by Angular 12 client. Application is deployed on Dev and Integ environments. Now suprisingly, same api is working on Dev but not on Integ. When on Integ, instead of returning binary file, it returns JSON. The only difference is Dev IIS Server (hosting API) version is 8.5 while that of Integ is 10.0.

I have also added necessary response header like :

 result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");

Below is the sample response returned on Integ :

{
  "Version": {
    "_Major": 1,
    "_Minor": 1,
    "_Build": -1,
    "_Revision": -1
  },
  "Content": {
    "Headers": [
      {
        "Key": "Content-Type",
        "Value": [
          "application/octet-stream"
        ]
      }
    ]
  },
  "StatusCode": 200,
  "ReasonPhrase": "OK",
  "Headers": [],
  "RequestMessage": null,
  "IsSuccessStatusCode": true
}

Below is my API code :

 [HttpPost]
        [Route("api/Chart/test")]
        public async Task<HttpResponseMessage> test(TestEntity request)
        {
            try
            {
                var ms = await _service.testMethod(request.Data, request.ExportType, request.FileName);
                var result = new HttpResponseMessage(HttpStatusCode.OK)
                {
                    Content = new ByteArrayContent(ms.ToArray())
                };
                result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
                return result;
            }
            catch (Exception)
            {
                var result = new HttpResponseMessage(HttpStatusCode.InternalServerError);
                return result;
            }
        }

And below is my angular method code :

 angularTestMethod(request) {
    return this.http.post(`${this.apiUrl}api/Chart/test`, request, { withCredentials: true, responseType: 'blob'})
    .pipe(catchError(err => this.handleError(err)));
  }

Any idea what could be the issue? As code of angular and web api is same for both environments.

Below are the version details:

Angular : Angular 12,
Web API : ASP.Net Web API 2.2
.Net Framework : 4.6.1
Dev Windows Server and IIS : 2012 and 8.5 resp
Integ Windows Server and IIS : 2016 and 10.0 resp

As asked in the comment, Below is another example of issue I am getting :

The TestEntity Class is a complex request object. Rather I will provide another method giving me same issue. Its returning JSON instead of Binary File.

 [HttpGet, Route("api/Chart/download/{id}")]
        public HttpResponseMessage Download(int id)
        {
            try
            {
                var ms = _chartService.Download(id);
                var result = new HttpResponseMessage(HttpStatusCode.OK)
                {
                    Content = new ByteArrayContent(ms.ToArray())
                };
                result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
                return result;
            }
            catch (FileNotFoundException)
            {
                var result = new HttpResponseMessage(HttpStatusCode.NotFound);
                return result;
            }
            catch (Exception)
            {
                var result = new HttpResponseMessage(HttpStatusCode.InternalServerError);
                return result;
            }
        }

And below is Download method :

   public MemoryStream Download(int id)
            {
                var chart= _repository.GetById(id);
                if (chart != null)
                {
                    var stream = new MemoryStream();
                    var writer = new StreamWriter(stream);
                    writer.Write(chart.MetaData);
                    writer.Flush();
                    stream.Position = 0;
                    return stream;
                }

                throw new FileNotFoundException("Chart not found");
            }

Chart Class :

 public class Chart
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string MetaData { get; set; }
        public int? ChartFolderId { get; set; }
        public int? SortOrder { get; set; }
        public bool IsPublic { get; set; }
        public bool InActive { get; set; }
        public string Owner { get; set; }
        public string InActiveBy { get; set; }
        public DateTime? InActiveOn { get; set; }
        public string InsertedBy { get; set; }
        public DateTime InsertedOn { get; set; }
        public string ModifiedBy { get; set; }
        public DateTime ModifiedOn { get; set; }
    }
Internet Information Services
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,276 questions
ASP.NET API
ASP.NET API
ASP.NET: A set of technologies in the .NET Framework for building web applications and XML web services.API: A software intermediary that allows two applications to interact with each other.
300 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce Zhang-MSFT 3,736 Reputation points
    2021-08-26T08:03:56.547+00:00

    Hi @Girish Khadse ,

    I test the code in my local visial studio. It return like this.
    126579-2021-08-25-175559.jpg

    Then I deploy it on IIS8.5(windows server2012) and IIS10(windows server2016).
    126681-1.jpg
    126649-3.jpg

    Both of them have same result. I think you can try another server to test again. You can use IIS10 on windows server2019.

    0 comments No comments

  2. Girish Khadse 1 Reputation point
    2021-11-23T06:45:43.5+00:00

    Apparently it was the issue with System.net.http assembly. I had installed the System.net.http nuget package earlier but did not check in binding redirects. This answer here to another stack overflow question helped me with it.
    I uninstalled the nuget package for System.Net.http and installed it back. And then checked in all binding redirects generated in web.config.

    0 comments No comments