CQRSMicroservices.Web.Middleware.CommandMiddleware.Invoke C# (CSharp) Method

Invoke() public method

public Invoke ( IOwinContext context ) : System.Threading.Tasks.Task
context IOwinContext
return System.Threading.Tasks.Task
    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);
      }
    }