From df584ca915e1a2374abfa690ff7686c970aa123c Mon Sep 17 00:00:00 2001 From: Pratik Pote Date: Wed, 3 Jul 2019 00:05:57 -0400 Subject: [PATCH] Update to Blazor 3.0.0-preview6 (#35) * Update to Blazor 3.0.0-preview6 - Update global.json to reflect the updated sdk version - Replace Json.Deserialize by JsonSerializer.Parse in class HubConnection - Add this. where missing in class HubConnection - Replace onclick and bind attributes in test razor pages by @onclick and @bind - Replace @functions by @code in test razor files - Remove UseSignalR and UseBlazor from test Server Startup class Configure method - Add UseAuthorization to Startup class - Comment out AddMessagePackProtocol and uncommented AddJsonProtocol in Startup class - Comment out AddMessagePackProtocol in ChatComponent class * Update .vsts-ci.yml and .vsts-release.yml to use the .NET Core Preview 6 SDK. --- .vsts-ci.yml | 2 +- .vsts-release.yml | 2 +- global.json | 2 +- .../Blazor.Extensions.SignalR.csproj | 2 +- .../HubConnection.cs | 79 ++++++++++--------- ...azor.Extensions.SignalR.Test.Client.csproj | 4 +- .../Pages/Chat.razor | 36 ++++----- .../Pages/ChatComponent.cs | 50 ++++++------ .../Shared/NavMenu.razor | 6 +- ...azor.Extensions.SignalR.Test.Server.csproj | 8 +- .../Startup.cs | 15 ++-- 11 files changed, 103 insertions(+), 103 deletions(-) diff --git a/.vsts-ci.yml b/.vsts-ci.yml index a942416..98f60bc 100644 --- a/.vsts-ci.yml +++ b/.vsts-ci.yml @@ -8,7 +8,7 @@ steps: - task: DotNetCoreInstaller@0 inputs: packageType: 'sdk' - version: '3.0.100-preview4-011223' + version: '3.0.100-preview6-012264' - task: DotNetCoreCLI@2 inputs: diff --git a/.vsts-release.yml b/.vsts-release.yml index a25a902..cc07180 100644 --- a/.vsts-release.yml +++ b/.vsts-release.yml @@ -8,7 +8,7 @@ steps: - task: DotNetCoreInstaller@0 inputs: packageType: 'sdk' - version: '3.0.100-preview4-011223' + version: '3.0.100-preview6-012264' - task: DotNetCoreCLI@2 inputs: diff --git a/global.json b/global.json index cb4df9f..cd8762c 100644 --- a/global.json +++ b/global.json @@ -1,5 +1,5 @@ { "sdk": { - "version": "3.0.100-preview4-011223" + "version": "3.0.100-preview6-012264" } } \ No newline at end of file diff --git a/src/Blazor.Extensions.SignalR/Blazor.Extensions.SignalR.csproj b/src/Blazor.Extensions.SignalR/Blazor.Extensions.SignalR.csproj index 7d74033..f16e6ec 100644 --- a/src/Blazor.Extensions.SignalR/Blazor.Extensions.SignalR.csproj +++ b/src/Blazor.Extensions.SignalR/Blazor.Extensions.SignalR.csproj @@ -15,7 +15,7 @@ - + diff --git a/src/Blazor.Extensions.SignalR/HubConnection.cs b/src/Blazor.Extensions.SignalR/HubConnection.cs index be39f94..28a6a77 100644 --- a/src/Blazor.Extensions.SignalR/HubConnection.cs +++ b/src/Blazor.Extensions.SignalR/HubConnection.cs @@ -1,6 +1,7 @@ using Microsoft.JSInterop; using System; using System.Collections.Generic; +using System.Text.Json.Serialization; using System.Threading.Tasks; namespace Blazor.Extensions @@ -20,63 +21,63 @@ public class HubConnection : IDisposable internal HttpConnectionOptions Options { get; } internal string InternalConnectionId { get; } - private Dictionary> _callbacks = new Dictionary>(); + private Dictionary> callbacks = new Dictionary>(); - private HubCloseCallback _closeCallback; - private IJSRuntime _runtime; + private HubCloseCallback closeCallback; + private IJSRuntime runtime; public HubConnection(IJSRuntime runtime, HttpConnectionOptions options) { - this._runtime = runtime; + this.runtime = runtime; this.Options = options; this.InternalConnectionId = Guid.NewGuid().ToString(); runtime.InvokeSync(CREATE_CONNECTION_METHOD, this.InternalConnectionId, - new DotNetObjectRef(this.Options)); + DotNetObjectRef.Create(this.Options)); } - public Task StartAsync() => this._runtime.InvokeAsync(START_CONNECTION_METHOD, this.InternalConnectionId); - public Task StopAsync() => this._runtime.InvokeAsync(STOP_CONNECTION_METHOD, this.InternalConnectionId); + public Task StartAsync() => this.runtime.InvokeAsync(START_CONNECTION_METHOD, this.InternalConnectionId); + public Task StopAsync() => this.runtime.InvokeAsync(STOP_CONNECTION_METHOD, this.InternalConnectionId); public IDisposable On(string methodName, Func handler) - => On(methodName, + => this.On(methodName, (t1, t2, t3, t4, t5, t6, t7, t8, t9, t10) => handler(t1)); public IDisposable On(string methodName, Func handler) - => On(methodName, + => this.On(methodName, (t1, t2, t3, t4, t5, t6, t7, t8, t9, t10) => handler(t1, t2)); public IDisposable On(string methodName, Func handler) - => On(methodName, + => this.On(methodName, (t1, t2, t3, t4, t5, t6, t7, t8, t9, t10) => handler(t1, t2, t3)); public IDisposable On(string methodName, Func handler) - => On(methodName, + => this.On(methodName, (t1, t2, t3, t4, t5, t6, t7, t8, t9, t10) => handler(t1, t2, t3, t4)); public IDisposable On(string methodName, Func handler) - => On(methodName, + => this.On(methodName, (t1, t2, t3, t4, t5, t6, t7, t8, t9, t10) => handler(t1, t2, t3, t4, t5)); public IDisposable On(string methodName, Func handler) - => On(methodName, + => this.On(methodName, (t1, t2, t3, t4, t5, t6, t7, t8, t9, t10) => handler(t1, t2, t3, t4, t5, t6)); public IDisposable On(string methodName, Func handler) - => On(methodName, + => this.On(methodName, (t1, t2, t3, t4, t5, t6, t7, t8, t9, t10) => handler(t1, t2, t3, t4, t5, t6, t7)); public IDisposable On(string methodName, Func handler) - => On(methodName, + => this.On(methodName, (t1, t2, t3, t4, t5, t6, t7, t8, t9, t10) => handler(t1, t2, t3, t4, t5, t6, t7, t8)); public IDisposable On(string methodName, Func handler) - => On(methodName, + => this.On(methodName, (t1, t2, t3, t4, t5, t6, t7, t8, t9, t10) => handler(t1, t2, t3, t4, t5, t6, t7, t8, t9)); public IDisposable On(string methodName, @@ -103,84 +104,84 @@ public IDisposable On 0) { - t1 = Json.Deserialize(payloads[0]); + t1 = JsonSerializer.Parse(payloads[0]); } if (payloads.Length > 1) { - t2 = Json.Deserialize(payloads[1]); + t2 = JsonSerializer.Parse(payloads[1]); } if (payloads.Length > 2) { - t3 = Json.Deserialize(payloads[2]); + t3 = JsonSerializer.Parse(payloads[2]); } if (payloads.Length > 3) { - t4 = Json.Deserialize(payloads[3]); + t4 = JsonSerializer.Parse(payloads[3]); } if (payloads.Length > 4) { - t5 = Json.Deserialize(payloads[4]); + t5 = JsonSerializer.Parse(payloads[4]); } if (payloads.Length > 5) { - t6 = Json.Deserialize(payloads[5]); + t6 = JsonSerializer.Parse(payloads[5]); } if (payloads.Length > 6) { - t7 = Json.Deserialize(payloads[6]); + t7 = JsonSerializer.Parse(payloads[6]); } if (payloads.Length > 7) { - t8 = Json.Deserialize(payloads[7]); + t8 = JsonSerializer.Parse(payloads[7]); } if (payloads.Length > 8) { - t9 = Json.Deserialize(payloads[8]); + t9 = JsonSerializer.Parse(payloads[8]); } if (payloads.Length > 9) { - t10 = Json.Deserialize(payloads[9]); + t10 = JsonSerializer.Parse(payloads[9]); } return handler(t1, t2, t3, t4, t5, t6, t7, t8, t9, t10); } ); - RegisterHandle(methodName, callback); + this.RegisterHandle(methodName, callback); return callback; } internal void RegisterHandle(string methodName, HubMethodCallback callback) { - if (this._callbacks.TryGetValue(methodName, out var methodHandlers)) + if (this.callbacks.TryGetValue(methodName, out var methodHandlers)) { methodHandlers[callback.Id] = callback; } else { - this._callbacks[methodName] = new Dictionary + this.callbacks[methodName] = new Dictionary { { callback.Id, callback } }; } - this._runtime.InvokeSync(ON_METHOD, this.InternalConnectionId, new DotNetObjectRef(callback)); + this.runtime.InvokeSync(ON_METHOD, this.InternalConnectionId, DotNetObjectRef.Create(callback)); } internal void RemoveHandle(string methodName, string callbackId) { - if (this._callbacks.TryGetValue(methodName, out var callbacks)) + if (this.callbacks.TryGetValue(methodName, out var callbacks)) { if (callbacks.TryGetValue(callbackId, out var callback)) { - this._runtime.InvokeSync(OFF_METHOD, this.InternalConnectionId, methodName, callbackId); + this.runtime.InvokeSync(OFF_METHOD, this.InternalConnectionId, methodName, callbackId); //HubConnectionManager.Off(this.InternalConnectionId, handle.Item1); callbacks.Remove(callbackId); if (callbacks.Count == 0) { - this._callbacks.Remove(methodName); + this.callbacks.Remove(methodName); } } } @@ -188,18 +189,18 @@ internal void RemoveHandle(string methodName, string callbackId) public void OnClose(Func callback) { - this._closeCallback = new HubCloseCallback(callback); - this._runtime.InvokeSync(ON_CLOSE_METHOD, + this.closeCallback = new HubCloseCallback(callback); + this.runtime.InvokeSync(ON_CLOSE_METHOD, this.InternalConnectionId, - new DotNetObjectRef(this._closeCallback)); + DotNetObjectRef.Create(this.closeCallback)); } public Task InvokeAsync(string methodName, params object[] args) => - this._runtime.InvokeAsync(INVOKE_ASYNC_METHOD, this.InternalConnectionId, methodName, args); + this.runtime.InvokeAsync(INVOKE_ASYNC_METHOD, this.InternalConnectionId, methodName, args); public Task InvokeAsync(string methodName, params object[] args) => - this._runtime.InvokeAsync(INVOKE_WITH_RESULT_ASYNC_METHOD, this.InternalConnectionId, methodName, args); + this.runtime.InvokeAsync(INVOKE_WITH_RESULT_ASYNC_METHOD, this.InternalConnectionId, methodName, args); - public void Dispose() => this._runtime.InvokeSync(REMOVE_CONNECTION_METHOD, this.InternalConnectionId); + public void Dispose() => this.runtime.InvokeSync(REMOVE_CONNECTION_METHOD, this.InternalConnectionId); } } diff --git a/test/Blazor.Extensions.SignalR.Test.Client/Blazor.Extensions.SignalR.Test.Client.csproj b/test/Blazor.Extensions.SignalR.Test.Client/Blazor.Extensions.SignalR.Test.Client.csproj index dc8390e..321f7ab 100644 --- a/test/Blazor.Extensions.SignalR.Test.Client/Blazor.Extensions.SignalR.Test.Client.csproj +++ b/test/Blazor.Extensions.SignalR.Test.Client/Blazor.Extensions.SignalR.Test.Client.csproj @@ -10,8 +10,8 @@ 3.0 - - + + diff --git a/test/Blazor.Extensions.SignalR.Test.Client/Pages/Chat.razor b/test/Blazor.Extensions.SignalR.Test.Client/Pages/Chat.razor index b6b292a..223e31c 100644 --- a/test/Blazor.Extensions.SignalR.Test.Client/Pages/Chat.razor +++ b/test/Blazor.Extensions.SignalR.Test.Client/Pages/Chat.razor @@ -5,7 +5,7 @@

Serialization Test

- +
@@ -13,7 +13,7 @@

Multiple Arguments

- +
@@ -21,7 +21,7 @@

Byte array Arguments

- +
@@ -29,44 +29,44 @@

To Everybody

- - - + + +

To Connection

- - - + + +

To Me

- - + +

Group Actions

- - - - - - + + + + + +
    - @foreach (var msg in _messages) + @foreach (var msg in Messages) {
  • @msg
  • } diff --git a/test/Blazor.Extensions.SignalR.Test.Client/Pages/ChatComponent.cs b/test/Blazor.Extensions.SignalR.Test.Client/Pages/ChatComponent.cs index f92a8e9..9015226 100644 --- a/test/Blazor.Extensions.SignalR.Test.Client/Pages/ChatComponent.cs +++ b/test/Blazor.Extensions.SignalR.Test.Client/Pages/ChatComponent.cs @@ -13,16 +13,16 @@ public class ChatComponent : ComponentBase [Inject] private HttpClient _http { get; set; } [Inject] private HubConnectionBuilder _hubConnectionBuilder { get; set; } // [Inject] private ILogger _logger { get; set; } - internal string _toEverybody { get; set; } - internal string _toConnection { get; set; } - internal string _connectionId { get; set; } - internal string _toMe { get; set; } - internal string _toGroup { get; set; } - internal string _groupName { get; set; } - internal List _messages { get; set; } = new List(); - - private IDisposable _objectHandle; - private IDisposable _listHandle; + internal string ToEverybody { get; set; } + internal string ToConnection { get; set; } + internal string ConnectionId { get; set; } + internal string ToMe { get; set; } + internal string ToGroup { get; set; } + internal string GroupName { get; set; } + internal List Messages { get; set; } = new List(); + + private IDisposable objectHandle; + private IDisposable listHandle; private IDisposable _multiArgsHandle; private IDisposable _multiArgsComplexHandle; private IDisposable _byteArrayHandle; @@ -44,7 +44,7 @@ protected override async Task OnInitAsync() return token; }; }) - .AddMessagePackProtocol() + //.AddMessagePackProtocol() .Build(); this._connection.On("Send", this.Handle); @@ -60,7 +60,7 @@ public Task DemoMethodObject(object data) { Console.WriteLine("Got object!"); Console.WriteLine(data?.GetType().FullName ?? ""); - this._objectHandle.Dispose(); + this.objectHandle.Dispose(); if (data == null) return Task.CompletedTask; return this.Handle(data); } @@ -69,7 +69,7 @@ public Task DemoMethodList(object data) { Console.WriteLine("Got List!"); Console.WriteLine(data?.GetType().FullName ?? ""); - this._listHandle.Dispose(); + this.listHandle.Dispose(); if (data == null) return Task.CompletedTask; return this.Handle(data); } @@ -108,7 +108,7 @@ private async Task GetJwtToken(string userId) private Task Handle(object msg) { Console.WriteLine(msg); - this._messages.Add(msg.ToString()); + this.Messages.Add(msg.ToString()); this.StateHasChanged(); return Task.CompletedTask; } @@ -118,49 +118,49 @@ private Task HandleArgs(params object[] args) string msg = string.Join(", ", args); Console.WriteLine(msg); - this._messages.Add(msg); + this.Messages.Add(msg); this.StateHasChanged(); return Task.CompletedTask; } internal async Task Broadcast() { - await this._connection.InvokeAsync("Send", this._toEverybody); + await this._connection.InvokeAsync("Send", this.ToEverybody); } internal async Task SendToOthers() { - await this._connection.InvokeAsync("SendToOthers", this._toEverybody); + await this._connection.InvokeAsync("SendToOthers", this.ToEverybody); } internal async Task SendToConnection() { - await this._connection.InvokeAsync("SendToConnection", this._connectionId, this._toConnection); + await this._connection.InvokeAsync("SendToConnection", this.ConnectionId, this.ToConnection); } internal async Task SendToMe() { - await this._connection.InvokeAsync("Echo", this._toMe); + await this._connection.InvokeAsync("Echo", this.ToMe); } internal async Task SendToGroup() { - await this._connection.InvokeAsync("SendToGroup", this._groupName, this._toGroup); + await this._connection.InvokeAsync("SendToGroup", this.GroupName, this.ToGroup); } internal async Task SendToOthersInGroup() { - await this._connection.InvokeAsync("SendToOthersInGroup", this._groupName, this._toGroup); + await this._connection.InvokeAsync("SendToOthersInGroup", this.GroupName, this.ToGroup); } internal async Task JoinGroup() { - await this._connection.InvokeAsync("JoinGroup", this._groupName); + await this._connection.InvokeAsync("JoinGroup", this.GroupName); } internal async Task LeaveGroup() { - await this._connection.InvokeAsync("LeaveGroup", this._groupName); + await this._connection.InvokeAsync("LeaveGroup", this.GroupName); } internal async Task DoMultipleArgs() @@ -181,8 +181,8 @@ internal async Task DoByteArrayArg() internal async Task TellHubToDoStuff() { - this._objectHandle = this._connection.On("DemoMethodObject", this.DemoMethodObject); - this._listHandle = this._connection.On("DemoMethodList", this.DemoMethodList); + this.objectHandle = this._connection.On("DemoMethodObject", this.DemoMethodObject); + this.listHandle = this._connection.On("DemoMethodList", this.DemoMethodList); await this._connection.InvokeAsync("DoSomething"); } } diff --git a/test/Blazor.Extensions.SignalR.Test.Client/Shared/NavMenu.razor b/test/Blazor.Extensions.SignalR.Test.Client/Shared/NavMenu.razor index 991ab68..6182ba0 100644 --- a/test/Blazor.Extensions.SignalR.Test.Client/Shared/NavMenu.razor +++ b/test/Blazor.Extensions.SignalR.Test.Client/Shared/NavMenu.razor @@ -2,12 +2,12 @@ -
    +
    -@functions { +@code { bool collapseNavMenu = true; void ToggleNavMenu() diff --git a/test/Blazor.Extensions.SignalR.Test.Server/Blazor.Extensions.SignalR.Test.Server.csproj b/test/Blazor.Extensions.SignalR.Test.Server/Blazor.Extensions.SignalR.Test.Server.csproj index eaaf5f3..4c2ed53 100644 --- a/test/Blazor.Extensions.SignalR.Test.Server/Blazor.Extensions.SignalR.Test.Server.csproj +++ b/test/Blazor.Extensions.SignalR.Test.Server/Blazor.Extensions.SignalR.Test.Server.csproj @@ -1,4 +1,4 @@ - + netcoreapp3.0 @@ -9,10 +9,10 @@ - - + + - + diff --git a/test/Blazor.Extensions.SignalR.Test.Server/Startup.cs b/test/Blazor.Extensions.SignalR.Test.Server/Startup.cs index 507db35..3ea88d9 100644 --- a/test/Blazor.Extensions.SignalR.Test.Server/Startup.cs +++ b/test/Blazor.Extensions.SignalR.Test.Server/Startup.cs @@ -33,8 +33,8 @@ public void ConfigureServices(IServiceCollection services) services.AddConnections(); services .AddSignalR(options => options.KeepAliveInterval = TimeSpan.FromSeconds(5)) - .AddMessagePackProtocol(); - //.AddJsonProtocol(); + //.AddMessagePackProtocol(); + .AddJsonProtocol(); services.AddAuthorization(options => { @@ -106,22 +106,21 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env) app.UseHsts(); } + app.UseAuthorization(); app.UseHttpsRedirection(); app.UseCookiePolicy(); app.UseCors("CorsPolicy"); - app.UseSignalR(routes => - { - routes.MapHub("/chathub"); - }); + + app.UseClientSideBlazorFiles(); app.UseRouting(); app.UseEndpoints(endpoints => { endpoints.MapDefaultControllerRoute(); + endpoints.MapHub("/chathub"); + endpoints.MapFallbackToClientSideBlazor("index.html"); }); - - app.UseBlazor(); } } }