Merge pull request #8960 from aspnet/anurse/aspnetsignalr-observe-exceptions
commit
d37d382cf0
|
@ -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.
|
||||
|
|
|
@ -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