Last active
October 29, 2024 22:27
-
-
Save Calabonga/bb79d857a01403bbe99417b8ef36adde to your computer and use it in GitHub Desktop.
MassTransit (RabbitMQ) connection configuration for ASP.NET Core
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <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(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"MassTransit": { | |
"Url": "localhost", | |
"Host": "<VirtualHost>", | |
"UserName": "<UserName>", | |
"Password": "<Password>" | |
} |
Никак. Эти настройки я придумал сам сюда вынести. Из значения не зависят от версии.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Здравствуйте. Как бы вы обновили данное подключение, используя последнюю версию MassTransit?