Last active
April 5, 2019 07:09
-
-
Save jussimattila/f014775f0bbc1343bdd94ca430168a78 to your computer and use it in GitHub Desktop.
Code snippets showing the initialization of MassTransit locally (RabbitMQ) or in the cloud (Azure Service Bus) based on configuration
This file contains hidden or 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
// IComponentContext from Autofac, using DI container to create message consumers | |
public static IBusControl Create(IComponentContext componentContext) | |
{ | |
var serviceBusConfiguration = GetServiceBusConfiguration(); | |
var serviceBus = CreateServiceBus(serviceBusConfiguration, componentContext); | |
await serviceBus.StartAsync(); | |
return serviceBus; | |
} |
This file contains hidden or 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
private static void ConfigureReceiveEndpoint( | |
IReceiveEndpointConfigurator configurator, | |
IComponentContext componentContext) | |
{ | |
configurator.Consumer(componentContext.Resolve<Func<Message1Consumer>>()); | |
configurator.Consumer(componentContext.Resolve<Func<Message2onsumer>>()); | |
} |
This file contains hidden or 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
private static ServiceBusConfiguration GetServiceBusConfiguration() | |
{ | |
switch (AppConfiguration.ServiceBusTransport) | |
{ | |
case ServiceBusTransport.RabbitMq: | |
return GetRabbitMqConfiguration(); | |
case ServiceBusTransport.AzureServiceBus: | |
return GetAzureServiceBusConfiguration(); | |
case ServiceBusTransport.NullBus: | |
return GetNullBusConfiguration(); | |
default: | |
throw new ArgumentOutOfRangeException("Service bus transport not configured."); | |
} | |
} | |
private static ServiceBusConfiguration GetRabbitMqConfiguration() | |
{ | |
return new ServiceBusConfiguration | |
{ | |
Transport = ServiceBusTransport.RabbitMq, | |
HostUri = new Uri($"{AppConfiguration.RabbitMqHostUri}/{AppConfiguration.RabbitMqVirtualHostName}"), | |
QueueName = $"app.{AppConfiguration.AppEnvironment}" | |
}; | |
} | |
private static ServiceBusConfiguration GetAzureServiceBusConfiguration() | |
{ | |
return new ServiceBusConfiguration | |
{ | |
Transport = ServiceBusTransport.AzureServiceBus, | |
HostUri = ServiceBusEnvironment.CreateServiceUri( | |
AppConfiguration.AzureServiceBusScheme, | |
AppConfiguration.AzureServiceBusNamespace, | |
AppConfiguration.AzureServiceBusPath), | |
QueueName = $"app.{AppConfiguration.AppEnvironment}" | |
}; | |
} | |
private static ServiceBusConfiguration GetNullBusConfiguration() | |
{ | |
return new ServiceBusConfiguration | |
{ | |
Transport = ServiceBusTransport.NullBus, | |
HostUri = new Uri("http://null"), | |
QueueName = $"app.{AppConfiguration.AppEnvironment}" | |
}; | |
} |
This file contains hidden or 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
private static IBusControl CreateServiceBus( | |
ServiceBusConfiguration configuration, | |
IComponentContext componentContext) | |
{ | |
switch (configuration.Transport) | |
{ | |
case ServiceBusTransport.RabbitMq: | |
return CreateRabbitMqServiceBus(configuration, componentContext); | |
case ServiceBusTransport.AzureServiceBus: | |
return CreateAzureServiceBus(configuration, componentContext); | |
case ServiceBusTransport.NullBus: | |
return CreateNullBus(configuration, componentContext); | |
default: | |
throw new ArgumentOutOfRangeException("Service bus transport not configured.", new Exception()); | |
} | |
} | |
private static IBusControl CreateRabbitMqServiceBus( | |
ServiceBusConfiguration configuration, | |
IComponentContext componentContext) | |
{ | |
return Bus.Factory.CreateUsingRabbitMq( | |
c => | |
{ | |
c.UseSerilog(); | |
var host = c.Host( | |
configuration.HostUri, | |
conf => | |
{ | |
conf.Username(AppConfiguration.RabbitMqVirtualHostUsername); | |
conf.Password(AppConfiguration.RabbitMqVirtualHostPassword); | |
}); | |
c.ReceiveEndpoint( | |
host, | |
configuration.QueueName, | |
conf => ConfigureReceiveEndpoint(conf, componentContext)); | |
}); | |
} | |
private static IBusControl CreateAzureServiceBus( | |
ServiceBusConfiguration configuration, | |
IComponentContext componentContext) | |
{ | |
return Bus.Factory.CreateUsingAzureServiceBus( | |
c => | |
{ | |
c.UseSerilog(); | |
var tokenProvider = | |
TokenProvider.CreateSharedAccessSignatureTokenProvider( | |
AppConfiguration.AzureServiceBusKeyName, | |
AppConfiguration.AzureServiceBusKey); | |
var host = c.Host( | |
configuration.HostUri, | |
conf => { conf.TokenProvider = tokenProvider; }); | |
c.ReceiveEndpoint( | |
host, | |
configuration.QueueName, | |
conf => ConfigureReceiveEndpoint(conf, componentContext)); | |
}); | |
} | |
private static IBusControl CreateNullBus( | |
ServiceBusConfiguration configuration, | |
IComponentContext componentContext) | |
{ | |
return Bus.Factory.CreateUsingInMemory( | |
c => | |
{ | |
c.ReceiveEndpoint( | |
configuration.QueueName, | |
ep => { ConfigureReceiveEndpoint(ep, componentContext); }); | |
}); | |
} |
This file contains hidden or 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
class ServiceBusConfiguration | |
{ | |
public ServiceBusTransport Transport { get; set; } | |
public Uri HostUri { get; set; } | |
public string QueueName { get; set; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment