Skip to content

Instantly share code, notes, and snippets.

@gistlyn
Last active February 14, 2024 09:13
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save gistlyn/38e9791b920ed2c928e58fd08843a235 to your computer and use it in GitHub Desktop.
Use RabbitMQ
dotnet add package ServiceStack.RabbitMq
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using ServiceStack;
using ServiceStack.Messaging;
using ServiceStack.RabbitMq;
[assembly: HostingStartup(typeof(MyApp.ConfigureMq))]
namespace MyApp
{
/**
Register Services you want available via MQ in your AppHost, e.g:
var mqServer = appHost.Resolve<IMessageService>();
mqServer.RegisterHandler<MyRequest>(ExecuteMessage);
*/
public class ConfigureMq : IHostingStartup
{
public void Configure(IWebHostBuilder builder) => builder
.ConfigureServices((context, services) => {
services.AddSingleton<IMessageService>(c =>
new RabbitMqServer(context.Configuration.GetConnectionString("RabbitMq") ?? "localhost:5672"));
})
.ConfigureAppHost(afterAppHostInit: appHost => {
appHost.Resolve<IMessageService>().Start();
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment