MessageHandler
in namespace DotVVM.BusinessPack.Messaging
Usage & Scenarios
Defines a handler for a client-side method call in the SignalR hub.
Sample 1: MessageHandler inside MessagingConnection
You can define handlers for individual methods inside the MessagingConnection control.
Use the MethodName
property to specify the name of the method on the client hub (case-sensitive).
The Command
property defines the command which will be triggered when the client method is called from the server.
Make sure the command is a lambda function with correct argument types. DotVVM has no way of checking the type safety on this place.
To send message in the hub, you can use the following code snippet (in the server-side code):
public async Task SendMessage(IHubContext<ChatHub> chatHub, ChatMessageDTO message)
{
await chatHub.Clients.All.SendAsync("IncomingMessage", message);
}
<bp:MessagingConnection ServiceUrl="/hubs/Chat">
<bp:MessageHandler MethodName="IncomingMessage"
Command="{staticCommand: (ChatMessageDTO o) => Messages.Add(o)}" />
</bp:MessagingConnection>
public class SampleViewModel : DotvvmViewModelBase
{
public List<ChatMessageDTO> Messages { get; set; } = new();
}
public class ChatMessageDTO
{
public string Text { get; set; }
}
Sample 1: MessageHandler inside MessagingConnection
Sometimes, the MessagingConnection control is defined on some other place (for example in the master page), so it is not possible to place the MessageHandler
inside.
You can define ConnectionId
property on both MessagingConnection
and MessageHandler
controls which will bind these two controls together.
<bp:MessagingConnection ServiceUrl="/hubs/Chat"
ConnectionId="Chat" />
...
<bp:MessageHandler MethodName="IncomingMessage"
ConnectionId="Chat"
Command="{staticCommand: (ChatMessageDTO o) => Messages.Add(o)}" />
public class SampleViewModel : DotvvmViewModelBase
{
public List<ChatMessageDTO> Messages { get; set; } = new();
}
public class ChatMessageDTO
{
public string Text { get; set; }
}
Properties
Name | Type | Description | Notes | Default Value | |
---|---|---|---|---|---|
ClientIDMode | ClientIDMode | Gets or sets the client ID generation algorithm. |
attribute
static value
|
Static | |
ConnectionId | String |
attribute
static value
bindable
|
null | ||
DataContext | Object | Gets or sets a data context for the control and its children. All value and command bindings are evaluated in context of this value. The DataContext is null in client-side templates. |
attribute
bindable
|
null | |
Group | String |
attribute
static value
bindable
|
null | ||
ID | String | Gets or sets the control client ID within its naming container. |
attribute
static value
bindable
|
null | |
IncludeInPage | Boolean | Gets or sets whether the control is included in the DOM of the page. |
attribute
bindable
|
True | |
MethodName | String |
attribute
static value
bindable
|
null |
Events
Name | Type | Description | |
---|---|---|---|
Command | ICommandBinding |