@@ -33,12 +33,10 @@ private static async Task NotFound(IHttpRequestFeature req, IHttpResponseFeature
33
33
{
34
34
res . StatusCode = 404 ;
35
35
res . Headers . ContentType = "text/plain" ;
36
- res . Headers . ContentLength = HelloWorldPayload . Length ;
37
36
38
37
var body = features . GetResponseBodyFeature ( ) ;
39
38
40
39
await body . StartAsync ( ) ;
41
- body . Writer . Write ( HelloWorldPayload ) ;
42
40
await body . CompleteAsync ( ) ;
43
41
}
44
42
@@ -56,8 +54,6 @@ private static async Task Index(IHttpRequestFeature req, IHttpResponseFeature re
56
54
57
55
await body . StartAsync ( ) ;
58
56
body . Writer . Write ( IndexPayload ) ;
59
- await body . Writer . FlushAsync ( ) ;
60
- await body . CompleteAsync ( ) ;
61
57
}
62
58
63
59
private static ReadOnlySpan < byte > HelloWorldPayload => "Hello, World!"u8 ;
@@ -69,11 +65,8 @@ private static async Task Plaintext(IHttpRequestFeature req, IHttpResponseFeatur
69
65
res . Headers . ContentLength = HelloWorldPayload . Length ;
70
66
71
67
var body = features . GetResponseBodyFeature ( ) ;
72
-
73
68
await body . StartAsync ( ) ;
74
69
body . Writer . Write ( HelloWorldPayload ) ;
75
- await body . Writer . FlushAsync ( ) ;
76
- await body . CompleteAsync ( ) ;
77
70
}
78
71
79
72
private static readonly JsonSerializerOptions _jsonSerializerOptions = new ( JsonSerializerDefaults . Web ) ;
@@ -84,31 +77,32 @@ private static async Task Plaintext(IHttpRequestFeature req, IHttpResponseFeatur
84
77
private static async Task Json ( IHttpRequestFeature req , IHttpResponseFeature res , IFeatureCollection features )
85
78
{
86
79
res . StatusCode = 200 ;
87
- res . Headers . ContentType = "application/json" ;
80
+ res . Headers . ContentType = "application/json; charset=utf-8 " ;
88
81
89
- //Span<byte> buffer = stackalloc byte[256];
90
82
var bufferWriter = _bufferWriterPool . Get ( ) ;
91
83
var jsonWriter = _jsonWriterPool . Get ( ) ;
92
84
93
85
bufferWriter . ResetWrittenCount ( ) ;
94
86
jsonWriter . Reset ( bufferWriter ) ;
95
87
96
- JsonSerializer . Serialize ( jsonWriter , new { message = "Hello, World!" } , _jsonSerializerOptions ) ;
88
+ JsonSerializer . Serialize ( jsonWriter , new JsonMessage { message = "Hello, World!" } , _jsonSerializerOptions ) ;
97
89
98
90
res . Headers . ContentLength = bufferWriter . WrittenCount ;
99
91
100
92
var body = features . GetResponseBodyFeature ( ) ;
101
93
102
94
await body . StartAsync ( ) ;
103
- bufferWriter . WrittenSpan . CopyTo ( body . Writer . GetSpan ( bufferWriter . WrittenCount ) ) ;
104
- body . Writer . Advance ( bufferWriter . WrittenCount ) ;
105
- await body . Writer . FlushAsync ( ) ;
106
- await body . CompleteAsync ( ) ;
95
+ body . Writer . Write ( bufferWriter . WrittenSpan ) ;
107
96
108
97
_jsonWriterPool . Return ( jsonWriter ) ;
109
98
_bufferWriterPool . Return ( bufferWriter ) ;
110
99
}
111
100
101
+ private struct JsonMessage
102
+ {
103
+ public required string message { get ; set ; }
104
+ }
105
+
112
106
private class Utf8JsonWriterPooledObjectPolicy : IPooledObjectPolicy < Utf8JsonWriter >
113
107
{
114
108
private static readonly ArrayBufferWriter < byte > _dummyBufferWriter = new ( 256 ) ;
0 commit comments