背景知识
我正在构建一个API,以从无法更改的旧版系统接收数据。它将XML发布到我们的API,我们将其转换为JSON,然后使用JSON建模绑定到我们的EF类,并将数据保存到数据库中以供以后读取。
该服务器是运行RHEL 7的Ec2。
控制器方法
[HttpPost]
public async Task<IActionResult> ReceivePurchaseOrder()
{
using (var reader = new StreamReader(Request.Body))
{
var content = await reader.ReadToEndAsync();
var json = _documentConverter.XmlToJson(content);
var command = JsonConvert.DeserializeObject<ReceivePurchaseOrderCommand>(json, new JsonSerializerSettings
{
ContractResolver = new DefaultContractResolver()
{
NamingStrategy = new SnakeCaseNamingStrategy()
}
});
var response = await Mediator.Send(command);
return OkOrNotFound(response);
}
}
Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddResponseCompression();
services.AddMvc(config =>
{
// Add XML Content Negotiation
config.RespectBrowserAcceptHeader = true;
config.InputFormatters.Add(new XmlSerializerInputFormatter());
config.OutputFormatters.Add(new XmlSerializerOutputFormatter());
}).AddJsonOptions(options =>
{
options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
});
services.AddMediatR();
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(ValidationBehavior<,>));
services.AddAutoMapper();
services.AddAssemblyInfo();
services.AddAppConfig(Configuration);
services.AddAppUnitOfWork(Configuration);
services.AddSingleton<IDocumentConverter, DocumentConverter>();
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddSingleton<IEmailService, EmailService>();
services.AddSingleton<IExceptionEmailService, ExceptionEmailService>();
services.AddValidators();
}