Hi,
I’ve trying to get my head around storing a message received through a ServerEventsClient into a database.
The application is a CLI/WinForms app, which has a ServerEventsClient running.
The ServerEventsClient is connected to a channel called “Default” and has a NamedReceiver registered for the selector “Default”.
The Receiver has a function “Any” which accepts a message and writes it to the console (for testing).
However, I would like to store the received message into a database managed by the application itself.
How can I access the Receiver or the message being processed?
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
try
{
var channels = new List<string>
{
"Default"
};
var client = new ServerEventsClient("http://localhost:61247", channels.ToArray());
client.RegisterNamedReceiver<MyReceiver>("Default");
client.Start();
Console.WriteLine(client.ConnectionInfo.ToJson());
Console.WriteLine(client.ReceiverTypes.ToJson());
Console.WriteLine(client.Status);
while (true)
{
Thread.Sleep(100);
//Console.WriteLine();
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
Console.ReadLine();
}
}
}
public class MyReceiver : ServerEventReceiver
{
public void Any(MyMessage request)
{
Console.WriteLine("Message received from {0}".Fmt(Client.BaseUri));
Console.WriteLine(request.Origin);
Console.WriteLine(request.Destination);
Console.WriteLine(request.Message);
}
public override void NoSuchMethod(string selector, object message) { }
}
}
The Receiver itself is working fine, I can see the messages being written to the command-line.
However, I cannot find a way to write those to a database, as the Receiver has no knowledge of the database.
What is the best advised way?
Subclassing the Receiver with a reference to a database connection, e.g. through OrmLite?
As I understand, every time a message is received on the channel with that specific selector, a new instance of MyReceiver is created, wouldn’t adding a reference to the Database create tremendous overhead?
Kind regards,
Jan