1 Star 0 Fork 2

mcgrady2009 / Benchmarks

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
SpanJsonMiddleware.cs 1.43 KB
一键复制 编辑 原始数据 按行查看 历史
mcgrady2009 提交于 2018-08-25 13:26 . 添加项目文件。
using System;
using System.Threading.Tasks;
using Benchmarks.Configuration;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
namespace Benchmarks.Middleware
{
public class SpanJsonMiddleware
{
private static readonly PathString _path = new PathString(Scenarios.GetPath(s => s.SpanJson));
private const int _bufferSize = 27;
private readonly RequestDelegate _next;
public SpanJsonMiddleware(RequestDelegate next)
{
_next = next;
}
public Task Invoke(HttpContext httpContext)
{
if (httpContext.Request.Path.StartsWithSegments(_path, StringComparison.Ordinal))
{
httpContext.Response.StatusCode = 200;
httpContext.Response.ContentType = "application/json";
httpContext.Response.ContentLength = _bufferSize;
var msg = new JsonMessage { message = "Hello, World!" };
var result = SpanJson.JsonSerializer.Generic.Utf8.SerializeAsync(msg, httpContext.Response.Body);
return result.IsCompleted ? Task.CompletedTask : result.AsTask();
}
return _next(httpContext);
}
}
public static class SpanJsonMiddlewareExtensions
{
public static IApplicationBuilder UseSpanJson(this IApplicationBuilder builder)
{
return builder.UseMiddleware<SpanJsonMiddleware>();
}
}
}
C#
1
https://gitee.com/Wang_Kai_/Benchmarks.git
git@gitee.com:Wang_Kai_/Benchmarks.git
Wang_Kai_
Benchmarks
Benchmarks
master

搜索帮助

53164aa7 5694891 3bd8fe86 5694891