@dhq_boiler

Are you sure you want to delete the question?

Leaving a resolved question undeleted may help others!

Stream was not readable.

解決したいこと

ASP.NET Coreで"WinDesktopAppOnCloud"というWebシステムを作ろうとしています。

発生している問題・エラー

OnPost()は表示されているGUIアプリケーションの画像上をマウスポインタで移動すると実行されます。

OnPost()でResponse.Bodyからデータを読み取りたいのですが、
new StreamReader(Response.Body)をしようとすると、下記のように例外が発生します。

System.ArgumentException: 'Stream was not readable.'

どうすればResponse.Bodyからデータを読み取れるでしょうか。

ソースコード

WinDesktopAppOnCloud
https://github.com/dhq-boiler/WinDesktopAppOnCloud

git repository
https://github.com/dhq-boiler/WinDesktopAppOnCloud.git

コミット:4e4aa56

0 likes

1Answer

自己解決しました。

独自のミドルウェアを実装してそれを使うようにしました。

IOMiddleware.cs
    //https://newbedev.com/how-to-log-the-http-response-body-in-asp-net-core-1-0
    //の記事中のコードを改変して実装
    public class IOMiddleware
    {
        private readonly RequestDelegate _next;

        public IOMiddleware(RequestDelegate next)
        {
            _next = next;
        }

        public async Task Invoke(HttpContext context)
        {
            await LogRequest(context.Request);

            await LogResponseAndInvokeNext(context);
        }

        private async Task LogRequest(HttpRequest request)
        {
            using (var bodyReader = new StreamReader(request.Body))
            {
                string body = await bodyReader.ReadToEndAsync();

                request.Body = new MemoryStream(Encoding.UTF8.GetBytes(body));
                System.Diagnostics.Debug.Print(body);
            }
        }

        private async Task LogResponseAndInvokeNext(HttpContext context)
        {
            using (var buffer = new MemoryStream())
            {
                await context.Request.Body.CopyToAsync(buffer);
                //replace the context response with our buffer
                var stream = context.Response.Body;
                context.Response.Body = buffer;

                //invoke the rest of the pipeline
                await _next.Invoke(context);

                //reset the buffer and read out the contents
                buffer.Seek(0, SeekOrigin.Begin);
                var reader = new StreamReader(buffer);
                using (var bufferReader = new StreamReader(buffer))
                {
                    string body = await bufferReader.ReadToEndAsync();

                    //reset to start of stream
                    buffer.Seek(0, SeekOrigin.Begin);

                    //copy our content to the original stream and put it back
                    await buffer.CopyToAsync(stream);
                    context.Response.Body = stream;

                }
            }
        }
    }
Startup.cs
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseRouting();

            app.UseAuthorization();

            app.UseMiddleware<IOMiddleware>(); //追記

            app.UseSession();
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapRazorPages();
            });
        }
0Like

Your answer might help someone💌