Selaa lähdekoodia

add json dispatcher.

master
yangxiaodong 7 vuotta sitten
vanhempi
commit
dc9250ca04
1 muutettua tiedostoa jossa 37 lisäystä ja 0 poistoa
  1. +37
    -0
      src/DotNetCore.CAP/Dashboard/JsonDispatcher.cs

+ 37
- 0
src/DotNetCore.CAP/Dashboard/JsonDispatcher.cs Näytä tiedosto

@@ -0,0 +1,37 @@
using System;
using System.Net;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;

namespace DotNetCore.CAP.Dashboard
{
internal class JsonDispatcher : IDashboardDispatcher
{
private readonly Func<DashboardContext, object> _command;

public JsonDispatcher(Func<DashboardContext, object> command)
{
_command = command;
}

public async Task Dispatch(DashboardContext context)
{
var request = context.Request;
var response = context.Response;

object result = _command(context);
var settings = new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver(),
Converters = new JsonConverter[] { new StringEnumConverter { CamelCaseText = true } }
};
var serialized = JsonConvert.SerializeObject(result, settings);

context.Response.ContentType = "application/json";
await context.Response.WriteAsync(serialized);
}
}
}

Ladataan…
Peruuta
Tallenna