public override async Task Invoke(IOwinContext context)
{
if(context.Request.Method.Equals("post", StringComparison.OrdinalIgnoreCase) &&
context.Request.Path.GetExtension().Equals("command", StringComparison.OrdinalIgnoreCase))
{
try
{
// Deserialize the command and dispatch it
var command = DeserializeFromBody(context.Request.Body);
await CqrsApplication.GetService<CommandBus>().Dispatch(command);
context.Response.StatusCode = 200;
await context.Response.WriteAsync("{ \"commandAccepted\": true }");
}
catch(AggregateException aEx)
{
var ex = aEx.InnerExceptions.FirstOrDefault();
ExceptionHandler(ex ?? aEx);
context.Response.StatusCode = 500;
await context.Response.WriteAsync($"{{ \"commandAccepted\": false, \"error\": \"{ex?.Message}\" }}");
}
catch(Exception ex)
{
ExceptionHandler(ex);
context.Response.StatusCode = 500;
await context.Response.WriteAsync($"{{ \"commandAccepted\": false, \"error\": \"{ex.Message}\" }}");
}
}
else
{
await Next.Invoke(context);
}
}