-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathAspNetCoreService.cs
More file actions
145 lines (123 loc) · 5.29 KB
/
AspNetCoreService.cs
File metadata and controls
145 lines (123 loc) · 5.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
using System;
using System.Linq;
using System.Threading.Tasks;
using MessagePack;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Formatters;
using Microsoft.Extensions.DependencyInjection;
namespace WcfVsWebApiVsAspNetCoreBenchmark
{
public class AspNetCoreService<T>: RestServiceBase<T>
where T: class, new()
{
public void Start()
{
var startup = new AspNetCoreStartup<T>(_format);
_host = new WebHostBuilder()
.UseKestrel(opt => opt.Limits.MaxRequestBodySize = 180000000)
.ConfigureServices(startup.ConfigureServices)
.Configure(startup.Configure)
.UseUrls($"http://localhost:{_port}")
.Build();
_host.Start();
InitializeClients();
}
public void Stop()
{
_host?.StopAsync().Wait();
}
public AspNetCoreService(int port, SerializerType format, int itemCount)
: base(port, format, itemCount)
{
_port = port;
_format = format;
}
private readonly int _port;
private readonly SerializerType _format;
private IWebHost _host;
}
public class AspNetCoreStartup<T>
{
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc(opt =>
{
opt.InputFormatters.RemoveType<JsonPatchInputFormatter>();
var jsonInputFormatter = opt.InputFormatters.OfType<JsonInputFormatter>().First();
var jsonOutputFormatter = opt.OutputFormatters.OfType<JsonOutputFormatter>().First();
opt.InputFormatters.Clear();
opt.OutputFormatters.Clear();
switch(_format)
{
case SerializerType.Xml:
opt.InputFormatters.Add(new XmlSerializerInputFormatter());
opt.OutputFormatters.Add(new XmlSerializerOutputFormatter());
break;
case SerializerType.JsonNet:
opt.InputFormatters.Add(jsonInputFormatter);
opt.OutputFormatters.Add(jsonOutputFormatter);
break;
case SerializerType.MessagePack:
opt.InputFormatters.Add(new MessagePackInputFormatter<T>());
opt.OutputFormatters.Add(new MessagePackOutputFormatter());
break;
case SerializerType.Utf8Json:
opt.InputFormatters.Add(new Utf8Json.AspNetCoreMvcFormatter.JsonInputFormatter());
opt.OutputFormatters.Add(new Utf8Json.AspNetCoreMvcFormatter.JsonOutputFormatter());
break;
default:
throw new ArgumentOutOfRangeException();
}
});
}
public void Configure(IApplicationBuilder app)
{
app.UseMvc();
}
public AspNetCoreStartup(SerializerType format)
{
_format = format;
}
private readonly SerializerType _format;
}
public class AspNetCoreController: Controller
{
[HttpPost("api/operation/SmallItem")]
public SmallItem[] Operation([FromBody] SmallItem[] items, int itemCount)
{
return Cache.SmallItems.Take(itemCount).ToArray();
}
[HttpPost("api/operation/LargeItem")]
public LargeItem[] Operation([FromBody] LargeItem[] items, int itemCount)
{
return Cache.LargeItems.Take(itemCount).ToArray();
}
}
public class MessagePackInputFormatter<T>: InputFormatter
{
public override async Task<InputFormatterResult> ReadRequestBodyAsync(InputFormatterContext context)
{
var items = await MessagePackSerializer.DeserializeAsync<T[]>(context.HttpContext.Request.Body);
return InputFormatterResult.Success(items);
}
public MessagePackInputFormatter()
{
SupportedMediaTypes.Clear();
SupportedMediaTypes.Add("application/x-msgpack");
}
}
public class MessagePackOutputFormatter: OutputFormatter
{
public override Task WriteResponseBodyAsync(OutputFormatterWriteContext context)
{
return MessagePackSerializer.SerializeAsync(context.HttpContext.Response.Body, context.Object);
}
public MessagePackOutputFormatter()
{
SupportedMediaTypes.Clear();
SupportedMediaTypes.Add("application/x-msgpack");
}
}
}