1 Star 0 Fork 2

mcgrady2009 / Benchmarks

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
MultipleUpdatesEfMiddleware.cs 2.07 KB
一键复制 编辑 原始数据 按行查看 历史
mcgrady2009 提交于 2018-08-25 13:26 . 添加项目文件。
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Threading.Tasks;
using Benchmarks.Configuration;
using Benchmarks.Data;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
namespace Benchmarks.Middleware
{
public class MultipleUpdatesEfMiddleware
{
private static readonly PathString _path = new PathString(Scenarios.GetPath(s => s.DbMultiUpdateEf));
private static readonly JsonSerializerSettings _jsonSettings = new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
};
private readonly RequestDelegate _next;
public MultipleUpdatesEfMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task Invoke(HttpContext httpContext)
{
if (httpContext.Request.Path.StartsWithSegments(_path, StringComparison.Ordinal))
{
var count = MiddlewareHelpers.GetMultipleQueriesQueryCount(httpContext);
var db = httpContext.RequestServices.GetService<EfDb>();
var rows = await db.LoadMultipleUpdatesRows(count);
var result = JsonConvert.SerializeObject(rows, _jsonSettings);
httpContext.Response.StatusCode = StatusCodes.Status200OK;
httpContext.Response.ContentType = "application/json";
httpContext.Response.ContentLength = result.Length;
await httpContext.Response.WriteAsync(result);
return;
}
await _next(httpContext);
}
}
public static class MultipleUpdatesEfMiddlewareExtensions
{
public static IApplicationBuilder UseMultipleUpdatesEf(this IApplicationBuilder builder)
{
return builder.UseMiddleware<MultipleUpdatesEfMiddleware>();
}
}
}
C#
1
https://gitee.com/Wang_Kai_/Benchmarks.git
git@gitee.com:Wang_Kai_/Benchmarks.git
Wang_Kai_
Benchmarks
Benchmarks
master

搜索帮助