update ASP.NET SignalR docs to show observing send errors
parent
f514dc1a70
commit
14cb19b101
|
@ -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)]
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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 });
|
||||
}
|
|
@ -1,5 +1,5 @@
|
|||
public async Task NewContosoChatMessage(string name, string message)
|
||||
{
|
||||
await Clients.Others.addContosoChatMessageToPage(data);
|
||||
Clients.Caller.notifyMessageSent();
|
||||
await Clients.Caller.notifyMessageSent();
|
||||
}
|
|
@ -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);
|
||||
}
|
|
@ -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");
|
||||
}
|
|
@ -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);
|
||||
}
|
|
@ -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);
|
||||
}
|
|
@ -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
|
|
@ -1,12 +1,12 @@
|
|||
public class MyHub : Hub
|
||||
{
|
||||
public void Send(string message)
|
||||
public async Task Send(string message)
|
||||
{
|
||||
if(message.Contains("<script>"))
|
||||
{
|
||||
throw new HubException("This message will flow to the client", new { user = Context.User.Identity.Name, message = message });
|
||||
}
|
||||
|
||||
Clients.All.send(message);
|
||||
await Clients.All.send(message);
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue