Created
March 6, 2019 08:56
-
-
Save im-kashi/74934a815345ad04ece8cb7d87bb198e to your computer and use it in GitHub Desktop.
This file contains 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 System; | |
using System.IO; | |
using System.Threading.Tasks; | |
using Microsoft.AspNetCore.Mvc.Formatters; | |
using Microsoft.Net.Http.Headers; | |
using ProtoBuf; | |
namespace Inasync { | |
public class ProtobufInputFormatter : InputFormatter { | |
private readonly Func<InputFormatterContext, Action<byte[]>> _loggerFactory; | |
public ProtobufInputFormatter(Func<InputFormatterContext, Action<byte[]>> loggerFactory = null) { | |
SupportedMediaTypes.Add(MediaTypeHeaderValue.Parse("application/octet-stream")); | |
_loggerFactory = loggerFactory; | |
} | |
protected override bool CanReadType(Type type) { | |
if (!base.CanReadType(type)) { return false; } | |
if (type.GetCustomAttributes(typeof(ProtoContractAttribute), inherit: false).Length == 0) { return false; } | |
return true; | |
} | |
public override async Task<InputFormatterResult> ReadRequestBodyAsync(InputFormatterContext context) { | |
var logger = _loggerFactory?.Invoke(context); | |
object bidRequest; | |
if (logger != null) { | |
using (var memory = new MemoryStream()) { | |
await context.HttpContext.Request.Body.CopyToAsync(memory); | |
logger(memory.ToArray()); | |
bidRequest = Serializer.Deserialize(context.ModelType, memory); | |
} | |
} | |
else { | |
bidRequest = Serializer.Deserialize(context.ModelType, context.HttpContext.Request.Body); | |
} | |
return InputFormatterResult.Success(bidRequest); | |
} | |
} | |
} |
This file contains 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 System; | |
using System.IO; | |
using System.Threading.Tasks; | |
using Microsoft.AspNetCore.Mvc.Formatters; | |
using ProtoBuf; | |
namespace Inasync { | |
public class ProtobufOutputFormatter : OutputFormatter { | |
private readonly Func<OutputFormatterWriteContext, Action<byte[]>> _loggerFactory; | |
public ProtobufOutputFormatter(Func<OutputFormatterWriteContext, Action<byte[]>> loggerFactory = null) { | |
SupportedMediaTypes.Add("application/octet-stream"); | |
_loggerFactory = loggerFactory; | |
} | |
public override bool CanWriteResult(OutputFormatterCanWriteContext context) { | |
if (!base.CanWriteResult(context)) { return false; } | |
if (context.Object.GetType().GetCustomAttributes(typeof(ProtoContractAttribute), inherit: false).Length == 0) { return false; } | |
return true; | |
} | |
public override async Task WriteResponseBodyAsync(OutputFormatterWriteContext context) { | |
var logger = _loggerFactory?.Invoke(context); | |
if (logger != null) { | |
using (var memory = new MemoryStream()) { | |
Serializer.Serialize(memory, context.Object); | |
await memory.CopyToAsync(context.HttpContext.Response.Body); | |
logger(memory.ToArray()); | |
} | |
} | |
else { | |
Serializer.Serialize(context.HttpContext.Response.Body, context.Object); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment