Creating a Message Module Client
Use the Scaleout.Modules.Client NuGet package’s MessageModuleClient class to send messages to your message module.
Note
The .NET MessageModuleClient class is not the only way to send messages to a message module. The Messaging REST API can also be used from other languages and platforms.
Background
In this example, we will use the MessageModuleClient class in the Scaleout.Modules.Client NuGet package to send messages to the “Flight” message module created in the prior topics.
Prerequisites
.NET 8 SDK or higher.
Scaleout.Modules.Client NuGet Package.
A deployed Message Module. This walkthrough will create a client class for the “Flight” message module project created in the previous topic.
Procedure
1. Create a client project
In a new console application project (or in any existing application/library outside of the Flight message module), add a reference to the Scaleout.Modules.Client NuGet package.
2. Define a message
Messages sent by a client must match the expected format defined in the message module. The example “Flight” module expects a specific “ArrivalTimeMessage” message (or a “PassengerAddedMessage”, if you are following the multiple message types) example.
The class definition for a message can either be redefined in your client application or put into a separate, shared library that is used by both your client and the message module.
namespace MsgModuleExampleClient
{
public class ArrivalTimeMessage
{
public DateTime NewArrival { get; set; }
}
}
3. Use the client class
The MessageModuleClient class can be used in any application that needs to interact with the Flight message module. The client application uses a GridConnection instance to connect to the ScaleOut StateServer cluster. See Connecting to a ScaleOut Data Grid for more information on using the GridConnection class.
using Scaleout.Client;
using Scaleout.Modules.Client;
namespace MsgModuleExampleClient
{
internal class Program
{
static async Task Main(string[] args)
{
GridConnection conn = GridConnection.Connect("bootstrapGateways=localhost:721");
MessageModuleClient flightClient = new MessageModuleClient("Flight", conn);
var msg = new ArrivalTimeMessage
{ NewArrival = DateTime.UtcNow.AddHours(2) };
byte[] msgBytes = System.Text.Json.JsonSerializer.SerializeToUtf8Bytes(msg);
await flightClient.SendMessageAsync("flight123", msgBytes);
}
}
}