Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
MassTransit (RabbitMQ) connection configuration for ASP.NET Core
/// <summary>
/// MassTransit configurations for ASP.NET Core
/// </summary>
public class ConfigureServicesMassTransit
{
/// <summary>
/// ConfigureServices
/// </summary>
/// <param name="services"></param>
/// <param name="configuration"></param>
public static void ConfigureServices(IServiceCollection services, IConfiguration configuration)
{
var massTransitSection = Configuration.GetSection("MassTransit");
var url = massTransitSection.GetValue<string>("Url");
var host = massTransitSection.GetValue<string>("Host");
var userName = massTransitSection.GetValue<string>("UserName");
var password = massTransitSection.GetValue<string>("Password");
if (massTransitSection == null || url == null || host == null)
{
throw new MicroserviceArgumentNullException("Section 'mass-transit' configuration settings are not found in appSettings.json");
}
services.AddMassTransit(x =>
{
x.SetSnakeCaseEndpointNameFormatter();
x.UsingRabbitMq((context, cfg) =>
{
cfg.Host($"rabbitmq://{url}/{host}", configurator =>
{
configurator.Username(userName);
configurator.Password(password);
});
cfg.ClearMessageDeserializers();
cfg.UseRawJsonSerializer();
cfg.UseHealthCheck(context);
cfg.ConfigureEndpoints(context, SnakeCaseEndpointNameFormatter.Instance);
});
});
services.AddMassTransitHostedService();
}
}
"MassTransit": {
"Url": "localhost",
"Host": "<VirtualHost>",
"UserName": "<UserName>",
"Password": "<Password>"
}
@VitaliyPasko
Copy link

Здравствуйте. Как бы вы обновили данное подключение, используя последнюю версию MassTransit?

@Calabonga
Copy link
Author

Никак. Эти настройки я придумал сам сюда вынести. Из значения не зависят от версии.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment