From 14cb19b101eaf8ec8925e43557da608a042a34f8 Mon Sep 17 00:00:00 2001 From: Andrew Stanton-Nurse Date: Wed, 10 Oct 2018 11:03:09 -0700 Subject: [PATCH 1/5] update ASP.NET SignalR docs to show observing send errors --- .../overview/guide-to-the-api/hubs-api-guide-server.md | 4 ++++ .../hubs-api-guide-server/samples/sample13.cs | 4 ++-- .../hubs-api-guide-server/samples/sample23.cs | 4 ++-- .../hubs-api-guide-server/samples/sample25.cs | 4 ++-- .../hubs-api-guide-server/samples/sample41.cs | 2 +- .../hubs-api-guide-server/samples/sample42.cs | 4 ++-- .../hubs-api-guide-server/samples/sample46.cs | 2 +- .../hubs-api-guide-server/samples/sample58.cs | 4 ++-- .../hubs-api-guide-server/samples/sample59.cs | 4 ++-- .../hubs-api-guide-server/samples/sample60.vb | 4 ++-- .../hubs-api-guide-server/samples/sample63.cs | 4 ++-- .../guide-to-the-api/hubs-api-guide-server/samples/sample7.cs | 4 ++-- 12 files changed, 24 insertions(+), 20 deletions(-) diff --git a/aspnet/signalr/overview/guide-to-the-api/hubs-api-guide-server.md b/aspnet/signalr/overview/guide-to-the-api/hubs-api-guide-server.md index 6b065204f8..2f9b9628ba 100644 --- a/aspnet/signalr/overview/guide-to-the-api/hubs-api-guide-server.md +++ b/aspnet/signalr/overview/guide-to-the-api/hubs-api-guide-server.md @@ -298,6 +298,9 @@ To call client methods from the server, use the `Clients` property in a method i [!code-csharp[Main](hubs-api-guide-server/samples/sample23.cs?highlight=5)] +> [!NOTE] +> Invoking a client method is an asynchronous operation and returns a `Task`. Use `await` to ensure you wait for the message to be sent. If an error occurs while sending the message, `await` will also allow you to catch that exception using a try-catch block. + **JavaScript client using generated proxy** [!code-html[Main](hubs-api-guide-server/samples/sample24.html?highlight=1)] @@ -569,6 +572,7 @@ In VB.NET or in a strongly-typed hub, the caller state object can't be accessed To handle errors that occur in your Hub class methods, use one or more of the following methods: - Wrap your method code in try-catch blocks and log the exception object. For debugging purposes you can send the exception to the client, but for security reasons sending detailed information to clients in production is not recommended. +- Use `await` when invoking client methods to ensure that any exceptions that occur while trying to send the message are observed and can be caught in a try-catch block. - Create a Hubs pipeline module that handles the [OnIncomingError](https://msdn.microsoft.com/library/microsoft.aspnet.signalr.hubs.hubpipelinemodule.onincomingerror(v=vs.111).aspx) method. The following example shows a pipeline module that logs errors, followed by code in Startup.cs that injects the module into the Hubs pipeline. [!code-csharp[Main](hubs-api-guide-server/samples/sample61.cs)] diff --git a/aspnet/signalr/overview/guide-to-the-api/hubs-api-guide-server/samples/sample13.cs b/aspnet/signalr/overview/guide-to-the-api/hubs-api-guide-server/samples/sample13.cs index 0e9734b80c..5c7ff4f8fc 100644 --- a/aspnet/signalr/overview/guide-to-the-api/hubs-api-guide-server/samples/sample13.cs +++ b/aspnet/signalr/overview/guide-to-the-api/hubs-api-guide-server/samples/sample13.cs @@ -1,7 +1,7 @@ public class ContosoChatHub : Hub { - public void NewContosoChatMessage(string name, string message) + public async Task NewContosoChatMessage(string name, string message) { - Clients.All.addNewMessageToPage(name, message); + await Clients.All.addNewMessageToPage(name, message); } } \ No newline at end of file diff --git a/aspnet/signalr/overview/guide-to-the-api/hubs-api-guide-server/samples/sample23.cs b/aspnet/signalr/overview/guide-to-the-api/hubs-api-guide-server/samples/sample23.cs index 0e9734b80c..5c7ff4f8fc 100644 --- a/aspnet/signalr/overview/guide-to-the-api/hubs-api-guide-server/samples/sample23.cs +++ b/aspnet/signalr/overview/guide-to-the-api/hubs-api-guide-server/samples/sample23.cs @@ -1,7 +1,7 @@ public class ContosoChatHub : Hub { - public void NewContosoChatMessage(string name, string message) + public async Task NewContosoChatMessage(string name, string message) { - Clients.All.addNewMessageToPage(name, message); + await Clients.All.addNewMessageToPage(name, message); } } \ No newline at end of file diff --git a/aspnet/signalr/overview/guide-to-the-api/hubs-api-guide-server/samples/sample25.cs b/aspnet/signalr/overview/guide-to-the-api/hubs-api-guide-server/samples/sample25.cs index 8b0af5d8a7..d4180c1022 100644 --- a/aspnet/signalr/overview/guide-to-the-api/hubs-api-guide-server/samples/sample25.cs +++ b/aspnet/signalr/overview/guide-to-the-api/hubs-api-guide-server/samples/sample25.cs @@ -1,4 +1,4 @@ -public void SendMessage(string name, string message) +public async Task SendMessage(string name, string message) { - Clients.All.addContosoChatMessageToPage(new ContosoChatMessage() { UserName = name, Message = message }); + await Clients.All.addContosoChatMessageToPage(new ContosoChatMessage() { UserName = name, Message = message }); } \ No newline at end of file diff --git a/aspnet/signalr/overview/guide-to-the-api/hubs-api-guide-server/samples/sample41.cs b/aspnet/signalr/overview/guide-to-the-api/hubs-api-guide-server/samples/sample41.cs index 31458f09b2..4a7aad08cb 100644 --- a/aspnet/signalr/overview/guide-to-the-api/hubs-api-guide-server/samples/sample41.cs +++ b/aspnet/signalr/overview/guide-to-the-api/hubs-api-guide-server/samples/sample41.cs @@ -1,5 +1,5 @@ public async Task NewContosoChatMessage(string name, string message) { await Clients.Others.addContosoChatMessageToPage(data); - Clients.Caller.notifyMessageSent(); + await Clients.Caller.notifyMessageSent(); } \ No newline at end of file diff --git a/aspnet/signalr/overview/guide-to-the-api/hubs-api-guide-server/samples/sample42.cs b/aspnet/signalr/overview/guide-to-the-api/hubs-api-guide-server/samples/sample42.cs index a6970ad8ec..e0b38e5760 100644 --- a/aspnet/signalr/overview/guide-to-the-api/hubs-api-guide-server/samples/sample42.cs +++ b/aspnet/signalr/overview/guide-to-the-api/hubs-api-guide-server/samples/sample42.cs @@ -1,6 +1,6 @@ -public void NewContosoChatMessage(string name, string message) +public async Task NewContosoChatMessage(string name, string message) { string methodToCall = "addContosoChatMessageToPage"; IClientProxy proxy = Clients.All; - proxy.Invoke(methodToCall, name, message); + await proxy.Invoke(methodToCall, name, message); } \ No newline at end of file diff --git a/aspnet/signalr/overview/guide-to-the-api/hubs-api-guide-server/samples/sample46.cs b/aspnet/signalr/overview/guide-to-the-api/hubs-api-guide-server/samples/sample46.cs index 8599aef2c3..2b6ed32d78 100644 --- a/aspnet/signalr/overview/guide-to-the-api/hubs-api-guide-server/samples/sample46.cs +++ b/aspnet/signalr/overview/guide-to-the-api/hubs-api-guide-server/samples/sample46.cs @@ -1,5 +1,5 @@ public async Task JoinGroup(string groupName) { await Groups.Add(Context.ConnectionId, groupName); - Clients.Group(groupname).addContosoChatMessageToPage(Context.ConnectionId + " added to group"); + await Clients.Group(groupname).addContosoChatMessageToPage(Context.ConnectionId + " added to group"); } \ No newline at end of file diff --git a/aspnet/signalr/overview/guide-to-the-api/hubs-api-guide-server/samples/sample58.cs b/aspnet/signalr/overview/guide-to-the-api/hubs-api-guide-server/samples/sample58.cs index 8718575f8e..e29efde9e6 100644 --- a/aspnet/signalr/overview/guide-to-the-api/hubs-api-guide-server/samples/sample58.cs +++ b/aspnet/signalr/overview/guide-to-the-api/hubs-api-guide-server/samples/sample58.cs @@ -1,6 +1,6 @@ -public void NewContosoChatMessage(string data) +public async Task NewContosoChatMessage(string data) { string userName = Clients.Caller.userName; string computerName = Clients.Caller.computerName; - Clients.Others.addContosoChatMessageToPage(message, userName, computerName); + await Clients.Others.addContosoChatMessageToPage(message, userName, computerName); } \ No newline at end of file diff --git a/aspnet/signalr/overview/guide-to-the-api/hubs-api-guide-server/samples/sample59.cs b/aspnet/signalr/overview/guide-to-the-api/hubs-api-guide-server/samples/sample59.cs index d7e9ba385f..46cf224955 100644 --- a/aspnet/signalr/overview/guide-to-the-api/hubs-api-guide-server/samples/sample59.cs +++ b/aspnet/signalr/overview/guide-to-the-api/hubs-api-guide-server/samples/sample59.cs @@ -1,6 +1,6 @@ -public void NewContosoChatMessage(string data) +public async Task NewContosoChatMessage(string data) { string userName = Clients.CallerState.userName; string computerName = Clients.CallerState.computerName; - Clients.Others.addContosoChatMessageToPage(data, userName, computerName); + await Clients.Others.addContosoChatMessageToPage(data, userName, computerName); } \ No newline at end of file diff --git a/aspnet/signalr/overview/guide-to-the-api/hubs-api-guide-server/samples/sample60.vb b/aspnet/signalr/overview/guide-to-the-api/hubs-api-guide-server/samples/sample60.vb index 8391749d96..758525a0af 100644 --- a/aspnet/signalr/overview/guide-to-the-api/hubs-api-guide-server/samples/sample60.vb +++ b/aspnet/signalr/overview/guide-to-the-api/hubs-api-guide-server/samples/sample60.vb @@ -1,5 +1,5 @@ -Public Sub NewContosoChatMessage(message As String) +Public Async Function NewContosoChatMessage(message As String) As Task Dim userName As String = Clients.CallerState.userName Dim computerName As String = Clients.CallerState.computerName - Clients.Others.addContosoChatMessageToPage(message, userName, computerName) + Await Clients.Others.addContosoChatMessageToPage(message, userName, computerName) End Sub \ No newline at end of file diff --git a/aspnet/signalr/overview/guide-to-the-api/hubs-api-guide-server/samples/sample63.cs b/aspnet/signalr/overview/guide-to-the-api/hubs-api-guide-server/samples/sample63.cs index 690adb46f1..2971367ec0 100644 --- a/aspnet/signalr/overview/guide-to-the-api/hubs-api-guide-server/samples/sample63.cs +++ b/aspnet/signalr/overview/guide-to-the-api/hubs-api-guide-server/samples/sample63.cs @@ -1,12 +1,12 @@ public class MyHub : Hub { - public void Send(string message) + public async Task Send(string message) { if(message.Contains("