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..0601ecfe93 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,11 @@ 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)] +Invoking a client method is an asynchronous operation and returns a `Task`. Use `await`: + +* To ensure the message is sent without error. +* To enable catching and handling errors in a try-catch block. + **JavaScript client using generated proxy** [!code-html[Main](hubs-api-guide-server/samples/sample24.html?highlight=1)] @@ -566,7 +571,7 @@ In VB.NET or in a strongly-typed hub, the caller state object can't be accessed ## How to handle errors in the Hub class -To handle errors that occur in your Hub class methods, use one or more of the following methods: +To handle errors that occur in your Hub class methods, first ensure you "observe" any exceptions from async operations (such as invoking client methods) by using `await`. Then 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. - 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. 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("