Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

Possibility of concurrently reading HTTP request body and writing HTTP response body?

$
0
0

Hi,

would it be possible to implement the IO of ASP.Net 5 such that it is possible to concurrently read the request body and write the response body?

E.g., using the following code in the Startup class:

        public void Configure(IApplicationBuilder app)
        {
            app.Use(async (context, next) =>
            {
                if (context.Request.Path == "/TestConcurrent")
                {
                    HttpRequest req = context.Request;
                    HttpResponse resp = context.Response;
Task writeTask = Task.Run(async () => { byte[] buf2 = new byte[32768]; for (int i = 0; i < 100; i++) { await resp.Body.WriteAsync(buf2, 0, buf2.Length); } }); int read; byte[] buf = new byte[8192]; while ((read = await req.Body.ReadAsync(buf, 0, buf.Length)) > 0) { System.Diagnostics.Debug.WriteLine("Written: " + read); } writeTask.Wait(); } }); }

This starts a new task to write to the response body while reading from the request body in the main task.

However (as it was also the case with classic ASP.Net), when running the code I get the following exception in most cases (in resp.Body.WriteAsync):

 System.InvalidOperationException
An outstanding I/O operation is already in progress.
at Microsoft.AspNet.Loader.IIS.Infrastructure.HeliosHttpContext.CompletionCallbackManager.MarkIOOperationAboutToStart() at Microsoft.AspNet.Loader.IIS.Infrastructure.HeliosHttpContext.Microsoft.AspNet.Loader.IIS.IHttpResponse.WriteEntityBodyAsync(Byte[] buffer, Int32 offset, Int32 count) at Microsoft.AspNet.Loader.IIS.FeatureModel.ResponseBody.<FirstWriteAsync>d__29.MoveNext() at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter.GetResult() at TTTTTWebApplication1.Startup.<>c__DisplayClass1_0.<<Configure>b__1>d.MoveNext() in C:\Users\developer4\Documents\Visual Studio 2015\Projects\TTTTTWebApplication1\src\TTTTTWebApplication1\Startup.cs:line 43

If there a possibility to implement a concurrent read and write in the runtime/IIS?

The background is that I'm trying to write a proxy-like module that forwards requests to another server like Tomcat using a HTTP/2 like protocol. To support use cases like Comet (where the remote server could write a response before completely reading the request), I would have to be able to read from the request's inputstream while writing to the request body, because in the protocol there is no way to determine when the server wants to read something and when it wants to write something.

Otherwise the only thing I could do is completely read the request body and then starting to write the response.

Thanks!


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>