From b31aad8d98ca7d09f39d22610fba08a1a24fadfc Mon Sep 17 00:00:00 2001 From: BrennanConroy Date: Thu, 9 May 2019 08:11:45 -0700 Subject: [PATCH] Update migration for SignalR (#12305) * Update migration for SignalR * update * Apply suggestions from code review Co-Authored-By: BrennanConroy * Apply suggestions from code review Co-Authored-By: BrennanConroy * sub header * Update aspnetcore/migration/22-to-30.md Co-Authored-By: BrennanConroy * Update 22-to-30.md * update the ms.date metadata value * SignalR migration patch (#12352) --- aspnetcore/migration/22-to-30.md | 65 ++++++++++++++++++++------------ 1 file changed, 41 insertions(+), 24 deletions(-) diff --git a/aspnetcore/migration/22-to-30.md b/aspnetcore/migration/22-to-30.md index e7a617ef66..3e39a7fa16 100644 --- a/aspnetcore/migration/22-to-30.md +++ b/aspnetcore/migration/22-to-30.md @@ -4,7 +4,7 @@ author: tdykstra description: Learn how to migrate an ASP.NET Core 2.2 project to ASP.NET Core 3.0. ms.author: tdykstra ms.custom: mvc -ms.date: 04/17/2019 +ms.date: 05/09/2019 uid: migration/22-to-30 --- # Migrate from ASP.NET Core 2.2 to 3.0 @@ -437,35 +437,52 @@ The most significant change from `WebHostBuilder` to `HostBuilder` is in [depend ## Update SignalR code -If you call `AddJsonProtocol`, replace it with `AddNewtonsoftJsonProtocol`. +`System.Text.Json` is now the default Hub Protocol used by both the client and server. -* The following examples show server code before and after the change: +In `Startup.ConfigureServices`, call `AddJsonProtocol` to set serializer options. - ```csharp - services.AddSignalR(...) - .AddJsonProtocol(...) // 2.2 - ``` +**Server:** - ```csharp - services.AddSignalR(...) - .AddNewtonsoftJsonProtocol(...) // 3.0 - ``` +```csharp +services.AddSignalR(...) + .AddJsonProtocol(options => + { + options.WriteIndented = false; + }) +``` -* The following examples show .NET client code before and after the change: +**Client:** - ```csharp - connection = new HubConnectionBuilder() - .WithUrl(...) - .AddJsonProtocol(...) // 2.2 - .Build() - ``` +```csharp +new HubConnectionBuilder() + .WithUrl("/chatHub") + .AddJsonProtocol(options => + { + options.WriteIndented = false; + }) + .Build(); +``` - ```csharp - connection = new HubConnectionBuilder() - .WithUrl(...) - .AddNewtonsoftJsonProtocol(...) // 3.0 - .Build() - ``` +### Switch to Newtonsoft.Json + +If you're using features of `Newtonsoft.Json` that aren't supported in `System.Text.Json`, you can switch back to `Newtonsoft.Json`: + +1. Install the [Microsoft.AspNetCore.SignalR.Protocols.NewtonsoftJson](https://www.nuget.org/packages/Microsoft.AspNetCore.SignalR.Protocols.NewtonsoftJson) NuGet package. +1. On the client, chain an `AddNewtonsoftJsonProtocol` method call to the `HubConnectionBuilder` instance: + + ```csharp + new HubConnectionBuilder() + .WithUrl("/chatHub") + .AddNewtonsoftJsonProtocol(...) + .Build(); + ``` + +1. On the server, chain an `AddNewtonsoftJsonProtocol` method call to the `AddSignalR` method call in `Startup.ConfigureServices`: + + ```csharp + services.AddSignalR() + .AddNewtonsoftJsonProtocol(...); + ``` ## Opt in to runtime compilation