outputstream is not available when a custom textwriter is used asp net RRS feed

  • Question

  • User-1523573758 posted

    I have created a HttpResponse object inside a web api method as shown below and write some text into it. I need to read the HttpResponse.OutputStream into a Stream, but it throws the below exception. Could you please tell me why I am getting the exception.

    public class ReportsController : ApiController
            public HttpResponseMessage GetTest()
                HttpResponse response = new HttpResponse(new StreamWriter(new MemoryStream()));
                response.Write("From HttpResponse");
                Stream dataStream = response.OutputStream;
                HttpResponseMessage httpResponseMessage = new HttpResponseMessage();
                httpResponseMessage.Content = new StringContent("Test Success");
                return httpResponseMessage;
    "Message": "An error has occurred.",
      "ExceptionMessage": "OutputStream is not available when a custom TextWriter is used.",
      "ExceptionType": "System.Web.HttpException",
      "StackTrace": "   at System.Web.HttpResponse.get_OutputStream()\r\n   at SampleProject.WebApi.Controllers.ReportsController.GetTest() 
    in d:\\01 Mirrors\\MirrorsV3 IIS\\SampleProject.WebApi\\Controllers\\ReportsController.cs:line 26\r\n
    at lambda_method(Closure , Object , Object[] )\r\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.
    ActionExecutor.<>c__DisplayClass13.<GetExecutor>b__c(Object instance, Object[] methodParameters)\r\n
    at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)\r\n
    at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.<>c__DisplayClass5.<ExecuteAsync>b__4()\r\n
    at System.Threading.Tasks.TaskHelpers.RunSynchronously[TResult](Func`1 func, CancellationToken cancellationToken)"
    Tuesday, February 14, 2017 11:41 AM

All replies

  • User753101303 posted


    From the error message it seems OutputStream is not available because you created "by hand" the response. It is hard to suggest a fix as for now your intent is really unclear.

    What are you trying to do? It doesn't seems to really make sense or be needed at all. You shouldn't create a response but rather to write to the response provided by the ASP.NET infrastructure.

    Tuesday, February 14, 2017 12:07 PM
  • User-1523573758 posted

    Hi Patrice,

    Thanks for your response. Let me explain my extension.

    We have an existing web api application which is hosted in IIS which uses an external library and it writes the response in the HttpResponse. Now, we need to host our web api application using OWIN, as you know when the application is hosted outside IIS, the HttpContext will be null. So I am intending to initialize the HttpContext by hand and hoping that the external library will write its response to the Context.HttpResponse. Then I can read the response using httpResponse.OutStream and set it to HttpResponseMessage object which the api method will return to the client.

    Please let me know if you know any workaround for this.



    Tuesday, February 14, 2017 12:24 PM
  • User-2057865890 posted

    Hi Mani,

    PushStreamContent could be useful.

    public class ReportsController : ApiController
            private static readonly ConcurrentDictionary<StreamWriter, StreamWriter> _outputs = 
    new ConcurrentDictionary<StreamWriter, StreamWriter>(); [HttpGet] public HttpResponseMessage GetTest() { HttpResponseMessage httpResponseMessage = new HttpResponseMessage(); var content = new PushStreamContent((stream, httpContent, transportContext) => { OnStreamAvailable(stream, httpContent, transportContext); }, "text/plain"); httpResponseMessage.Content = content; return httpResponseMessage; } private void OnStreamAvailable(Stream stream, HttpContent content, TransportContext context) { StreamWriter sWriter = new StreamWriter(stream); _outputs.TryAdd(sWriter, sWriter); } }



    #Streaming data using Web API


    Best Regards,


    Thursday, February 23, 2017 9:11 AM