using System.Text.Json for SignalRSamples as JsonTextWriter is a "synchronous IDisposable" ending end throwing when calling /deployment with AllowSynchronousIO turned off (#12165)

Thanks for finding and fixing this :)
This commit is contained in:
TeBeCo 2019-07-15 20:20:18 +02:00 committed by Brennan
parent 8f4214c8c0
commit 25db4331cf
2 changed files with 12 additions and 12 deletions

View File

@ -19,7 +19,6 @@
<Reference Include="Microsoft.AspNetCore.StaticFiles" /> <Reference Include="Microsoft.AspNetCore.StaticFiles" />
<Reference Include="Microsoft.Extensions.Configuration.CommandLine" /> <Reference Include="Microsoft.Extensions.Configuration.CommandLine" />
<Reference Include="Microsoft.Extensions.Logging.Console" /> <Reference Include="Microsoft.Extensions.Logging.Console" />
<Reference Include="Newtonsoft.Json" />
<Reference Include="System.Reactive.Linq" /> <Reference Include="System.Reactive.Linq" />
</ItemGroup> </ItemGroup>

View File

@ -9,8 +9,8 @@ using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using Newtonsoft.Json; using System.Text.Json;
using Newtonsoft.Json.Linq; using System.Text.Json.Serialization;
using SignalRSamples.ConnectionHandlers; using SignalRSamples.ConnectionHandlers;
using SignalRSamples.Hubs; using SignalRSamples.Hubs;
@ -18,6 +18,9 @@ namespace SignalRSamples
{ {
public class Startup public class Startup
{ {
private readonly JsonWriterOptions _jsonWriterOptions = new JsonWriterOptions { Indented = true };
// This method gets called by the runtime. Use this method to add services to the container. // This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940 // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services) public void ConfigureServices(IServiceCollection services)
@ -57,20 +60,19 @@ namespace SignalRSamples
endpoints.MapConnectionHandler<MessagesConnectionHandler>("/chat"); endpoints.MapConnectionHandler<MessagesConnectionHandler>("/chat");
endpoints.MapGet("/deployment", context => endpoints.MapGet("/deployment", async context =>
{ {
var attributes = Assembly.GetAssembly(typeof(Startup)).GetCustomAttributes<AssemblyMetadataAttribute>(); var attributes = Assembly.GetAssembly(typeof(Startup)).GetCustomAttributes<AssemblyMetadataAttribute>();
context.Response.ContentType = "application/json"; context.Response.ContentType = "application/json";
using (var textWriter = new StreamWriter(context.Response.Body)) await using (var writer = new Utf8JsonWriter(context.Response.BodyWriter, _jsonWriterOptions))
using (var writer = new JsonTextWriter(textWriter))
{ {
var json = new JObject(); writer.WriteStartObject();
var commitHash = string.Empty; var commitHash = string.Empty;
foreach (var attribute in attributes) foreach (var attribute in attributes)
{ {
json.Add(attribute.Key, attribute.Value); writer.WriteString(attribute.Key, attribute.Value);
if (string.Equals(attribute.Key, "CommitHash")) if (string.Equals(attribute.Key, "CommitHash"))
{ {
@ -80,13 +82,12 @@ namespace SignalRSamples
if (!string.IsNullOrEmpty(commitHash)) if (!string.IsNullOrEmpty(commitHash))
{ {
json.Add("GitHubUrl", $"https://github.com/aspnet/SignalR/commit/{commitHash}"); writer.WriteString("GitHubUrl", $"https://github.com/aspnet/SignalR/commit/{commitHash}");
} }
json.WriteTo(writer); writer.WriteEndObject();
await writer.FlushAsync();
} }
return Task.CompletedTask;
}); });
}); });
} }