-
Hi, Is there a way to subscribe to an incoming message in a similar way as .AddConsumer from DependencyInjection lib does with a callback, but in a non-ASP.Net app? This is for desktop clients written in .Net Framework as well as in .Net Core for WPF. Pawel |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hi @pgarbo, There is nothing built-in unfortunately, but you should be able to use DependencyInjection extensions package even in desktop/console apps. You just need to include class Program
{
static async Task Main()
{
var endpoint = Endpoint.Create("localhost", 5672, "guest", "guest");
var serviceCollection = new ServiceCollection();
var activeMqBuilder = serviceCollection.AddActiveMq("my-broker", new[] { endpoint });
activeMqBuilder.AddConsumer("my-address", RoutingType.Multicast, async (message, consumer, serviceProvider, cancellationToken) =>
{
// process your message here
// just remember to use Dispatcher.Invoke https://learn.microsoft.com/en-us/dotnet/api/system.windows.threading.dispatcher.invoke?view=windowsdesktop-7.0
// if you want to update your UI bits
await consumer.AcceptAsync(message).ConfigureAwait(false);
});
// build service provider
await using var serviceProvider = serviceCollection.BuildServiceProvider();
// and resolve IActiveMqClient to be able to start and stop ArtemisNetClient
var activeMqClient = serviceProvider.GetRequiredService<IActiveMqClient>();
// call this as part of your app boot logic
await activeMqClient.StartAsync(CancellationToken.None);
// call this as part of your app shutdown logic
await activeMqClient.StopAsync(CancellationToken.None);
}
} If for whatever reason, that's not an option, writing the subscribe logic shouldn't be too difficult. There are just a few special cases that should be handled. You can use ActiveMqConsumer.cs as a starting point and adjust the code to best suit your needs. Best, |
Beta Was this translation helpful? Give feedback.
Hi @pgarbo,
There is nothing built-in unfortunately, but you should be able to use DependencyInjection extensions package even in desktop/console apps. You just need to include
Microsoft.Extensions.DependencyInjection
so you have access toServiceCollection
andServiceProvider
types.