Created
January 23, 2016 07:04
-
-
Save vicentedealencar/48dd74e28e7a3584da8a to your computer and use it in GitHub Desktop.
fix owin context to read/write request/response
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.Owin; | |
using System; | |
using System.IO; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace CyberBar.BaseAPI.Infrastructure | |
{ | |
public class InspectableContext : IDisposable | |
{ | |
public Stream OriginalResponse { get; set; } | |
public MemoryStream BufferResponse { get; set; } | |
public StreamReader Reader { get; set; } | |
public IOwinContext Context { get; set; } | |
public InspectableContext(IOwinContext context) | |
{ | |
// http://stackoverflow.com/a/26216511/1206743 | |
OriginalResponse = context.Response.Body; | |
BufferResponse = new MemoryStream(); | |
context.Response.Body = BufferResponse; | |
Context = context; | |
} | |
public async Task<string> ReadResponseBody() | |
{ | |
BufferResponse.Seek(0, SeekOrigin.Begin); | |
Reader = new StreamReader(BufferResponse); | |
return await Reader.ReadToEndAsync(); | |
} | |
public string ReadRequestBody() | |
{ | |
Reader = new StreamReader(Context.Request.Body); | |
var body = Reader.ReadToEnd(); | |
byte[] requestData = Encoding.UTF8.GetBytes(body); | |
Context.Request.Body = new MemoryStream(requestData); | |
return body; | |
} | |
public void FlushResponseBody() | |
{ | |
BufferResponse = new MemoryStream(); | |
Context.Response.Body = BufferResponse; | |
} | |
public void SetRequestBody(string body) | |
{ | |
var requestData = Encoding.UTF8.GetBytes(body); | |
Context.Request.Body = new MemoryStream(requestData); | |
} | |
public void Dispose() | |
{ | |
BufferResponse.Seek(0, SeekOrigin.Begin); | |
BufferResponse.CopyToAsync(OriginalResponse).Wait(); | |
BufferResponse.Dispose(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.Owin; | |
using System; | |
using System.Threading.Tasks; | |
namespace CyberBar.BaseAPI.Infrastructure | |
{ | |
public class ConsoleLogMiddleware : OwinMiddleware | |
{ | |
public ConsoleLogMiddleware(OwinMiddleware next) : base (next) | |
{ | |
} | |
public async override Task Invoke(IOwinContext context) | |
{ | |
using (var c = new InspectableContext(context)) | |
{ | |
var reqBody = c.ReadRequestBody(); | |
Console.WriteLine(reqBody); | |
await this.Next.Invoke(context); | |
c.SetRequestBody(reqBody); | |
c.FlushResponseBody(); | |
await this.Next.Invoke(context); | |
var resBody = await c.ReadResponseBody(); | |
Console.WriteLine(resBody); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Why would you call Next.Invoke(context) twice?