### Project Output Example Source: https://github.com/challengermode/opentransit/blob/develop/doc/content/2.quick-starts/2.rabbitmq.md Example output when running the project with RabbitMQ configured. ```text Building... info: MassTransit[0] Configured endpoint Message, Consumer: GettingStarted.MessageConsumer info: Microsoft.Hosting.Lifetime[0] Application started. Press Ctrl+C to shut down. info: Microsoft.Hosting.Lifetime[0] Hosting environment: Development info: Microsoft.Hosting.Lifetime[0] Content root path: /Users/chris/Garbage/start/GettingStarted info: MassTransit[0] Bus started: rabbitmq://localhost/ info: GettingStarted.MessageConsumer[0] Received Text: The time is 3/24/2021 12:11:10 PM -05:00 ``` -------------------------------- ### Example Application Output Source: https://github.com/challengermode/opentransit/blob/develop/doc/content/2.quick-starts/4.amazon-sqs.md Example output from running the .NET project with Amazon SQS transport configured, showing the bus address starting with 'amazonsqs'. ```text Building... info: MassTransit[0] Configured endpoint Message, Consumer: GettingStarted.MessageConsumer info: Microsoft.Hosting.Lifetime[0] Application started. Press Ctrl+C to shut down. info: Microsoft.Hosting.Lifetime[0] Hosting environment: Development info: Microsoft.Hosting.Lifetime[0] Content root path: /Users/chris/Garbage/start/GettingStarted info: MassTransit[0] Bus started: amazonsqs://us-east-1/a-topic-name info: GettingStarted.MessageConsumer[0] Received Text: The time is 3/24/2021 12:11:10 PM -05:00 ``` -------------------------------- ### AutoStart Configuration Source: https://github.com/challengermode/opentransit/blob/develop/doc/content/3.documentation/1.concepts/6.requests.md Example of configuring the bus endpoint to start automatically with the bus by setting AutoStart = true. ```csharp services.AddMassTransit(x => { x.UsingRabbitMq((context, cfg) => { cfg.AutoStart = true; // default = false, starts the bus endpoint with the bus }); }); ``` -------------------------------- ### Configuration Example Source: https://github.com/challengermode/opentransit/blob/develop/doc/content/4.support/show-configuration.md An example of a configuration structure showing destination addresses and deserializers. ```json { "destinationAddress": "rabbitmq://[::1]:5672/test/bus-testservice-xhwyyybjcryy3ofjbdjcpnoenx_error?bind=true&queue=bus-testservice-xhwyyybjcryy3ofjbdjcpnoenx_error" } { "filterType": "deserialize", "deserializers": { "json": { "contentType": "application/vnd.masstransit+json" }, "bson": { "contentType": "application/vnd.masstransit+bson" }, "xml": { "contentType": "application/vnd.masstransit+xml" } }, "pipe": {} } ] ``` -------------------------------- ### Install Dependencies Source: https://github.com/challengermode/opentransit/blob/develop/doc/README.md Installs project dependencies using yarn. ```bash yarn install ``` -------------------------------- ### Pipe Configuration Example Source: https://github.com/challengermode/opentransit/blob/develop/doc/content/3.documentation/2.configuration/3.middleware/0.index.md Example of configuring a pipe with filters. ```csharp public interface CustomContext : PipeContext { string SomeThing { get; } } IPipe pipe = Pipe.New(x => { x.UseFilter(new CustomFilter(...)); }) ``` -------------------------------- ### Pipe Context Example Source: https://github.com/challengermode/opentransit/blob/develop/doc/content/3.documentation/2.configuration/3.middleware/0.index.md Example of a base pipe context implementation. ```csharp public class BaseCustomContext : BasePipeContext, CustomContext { public string SomeThing { get; set; } } await pipe.Send(new BaseCustomContext { SomeThing = "Hello" }); ``` -------------------------------- ### Send Order Example Source: https://github.com/challengermode/opentransit/blob/develop/doc/content/3.documentation/1.concepts/3.producers.md An example of how to obtain a send endpoint and send a message. ```csharp public record SubmitOrder { public string OrderId { get; init; } } public async Task SendOrder(ISendEndpointProvider sendEndpointProvider) { var endpoint = await sendEndpointProvider.GetSendEndpoint(_serviceAddress); await endpoint.Send(new SubmitOrder { OrderId = "123" }); } ``` -------------------------------- ### Additional Examples Source: https://github.com/challengermode/opentransit/blob/develop/doc/content/3.documentation/2.configuration/2.transports/4.amazon-sqs.md This example demonstrates subscribing any topic to a receive endpoint and configuring topic attributes. ```csharp services.AddMassTransit(x => { x.UsingAmazonSqs((context, cfg) => { cfg.Host("us-east-2", h => { h.AccessKey("your-iam-access-key"); h.SecretKey("your-iam-secret-key"); }); cfg.ReceiveEndpoint("input-queue", e => { // disable the default topic binding e.ConfigureConsumeTopology = false; e.Subscribe("event-topic", s => { // set topic attributes s.TopicAttributes["DisplayName"] = "Public Event Topic"; s.TopicSubscriptionAttributes["some-subscription-attribute"] = "some-attribute-value"; s.TopicTags.Add("environment", "development"); }); }); }); }); ``` -------------------------------- ### Run the Project Source: https://github.com/challengermode/opentransit/blob/develop/doc/content/2.quick-starts/5.postgresql.md Run the project after configuration. ```bash $ dotnet run ``` -------------------------------- ### Short Address Example Source: https://github.com/challengermode/opentransit/blob/develop/doc/content/3.documentation/1.concepts/3.producers.md Example of using a short address to get a send endpoint for a RabbitMQ queue. ```csharp GetSendEndpoint(new Uri("queue:input-queue")) ``` -------------------------------- ### Development Server Source: https://github.com/challengermode/opentransit/blob/develop/doc/README.md Starts the development server. ```bash yarn dev ``` -------------------------------- ### Run the Project Source: https://github.com/challengermode/opentransit/blob/develop/doc/content/2.quick-starts/2.rabbitmq.md Command to run the .NET project. ```bash dotnet run ``` -------------------------------- ### MultiBus Endpoint Name Formatter Configuration Source: https://github.com/challengermode/opentransit/blob/develop/doc/content/3.documentation/2.configuration/0.index.md Example of specifying a custom endpoint name formatter when configuring endpoints for a specific bus in a MultiBus setup. ```csharp cfg.ConfigureEndpoints(context, new KebabCaseEndpointNameFormatter(prefix: "Mobile", includeNamespace: false)); ``` -------------------------------- ### Run PostgreSQL Source: https://github.com/challengermode/opentransit/blob/develop/doc/content/2.quick-starts/5.postgresql.md Run the preconfigured official Docker image of Postgres. ```bash $ docker run -p 5432:5432 postgres ``` ```bash $ docker run --platform linux/arm64 -p 5432:5432 postgres ``` -------------------------------- ### PostgreSQL Entity Framework Saga Configuration Source: https://github.com/challengermode/opentransit/blob/develop/doc/content/3.documentation/2.configuration/4.persistence/entity-framework.md Example of configuring MassTransit with Entity Framework Core for PostgreSQL, including saga state machine setup and DbContext configuration. ```csharp services.AddMassTransit(cfg => { cfg.AddSagaStateMachine() .EntityFrameworkRepository(r => { r.ConcurrencyMode = ConcurrencyMode.Optimistic; // or use Pessimistic, which does not require RowVersion r.AddDbContext((provider,builder) => { builder.UseNpgsql(connectionString, m => { m.MigrationsAssembly(Assembly.GetExecutingAssembly().GetName().Name); m.MigrationsHistoryTable($ ``` -------------------------------- ### MassTransit Setup in Program.cs Source: https://github.com/challengermode/opentransit/blob/develop/documentation/docs/tutorials/basic-communication.md Configuration of MassTransit services, message broker connection, and consumer registration in the OrderService's Program.cs. ```csharp builder.Services.AddMassTransit(); // Configure the connection to the message broker // Register the consumers (if any) ``` -------------------------------- ### Transactional Enlistment Bus Usage in Console App Source: https://github.com/challengermode/opentransit/blob/develop/doc/content/3.documentation/2.configuration/3.middleware/transactions.md An example demonstrating the use of the transactional enlistment bus in a standalone console application without a dependency injection container. It shows how to manually create and start the bus, and then use the transactional bus for publishing and sending messages within a transaction scope. ```csharp public class Program { public static async Task Main() { var bus = Bus.Factory.CreateUsingRabbitMq(sbc => { sbc.Host("rabbitmq://localhost"); }); await bus.StartAsync(); // This is important! var transactionalBus = new TransactionalEnlistmentBus(bus); while(/*some condition*/) { using(var transaction = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { // Do whatever business logic you need. await transactionalBus.Publish(new ReportQueued{...}); await transactionalBus.Send(new CalculateReport{...}); // Maybe other business logic transaction.Complete(); } } Console.WriteLine("Press any key to exit"); await Task.Run(() => Console.ReadKey()); await bus.StopAsync(); } } ``` -------------------------------- ### Configure PostgreSQL Source: https://github.com/challengermode/opentransit/blob/develop/doc/content/2.quick-starts/5.postgresql.md Add the MassTransit.SqlTransport.PostgreSQL package to the project. ```bash $ dotnet add package MassTransit.SqlTransport.PostgreSQL ``` -------------------------------- ### Preview Build Source: https://github.com/challengermode/opentransit/blob/develop/doc/README.md Locally previews the generated build. ```bash yarn preview ``` -------------------------------- ### Console Application Example Source: https://github.com/challengermode/opentransit/blob/develop/doc/content/3.documentation/2.configuration/observability.md Example of configuring OpenTelemetry with a Console Exporter for MassTransit. ```csharp void ConfigureResource(ResourceBuilder r) { r.AddService("Service Name", serviceVersion: "Version", serviceInstanceId: Environment.MachineName); } Sdk.CreateTracerProviderBuilder() .ConfigureResource(ConfigureResource) .AddMeter(InstrumentationOptions.MeterName) // MassTransit Meter .AddConsoleExporter() // Any OTEL suportable exporter can be used here .Build() ``` -------------------------------- ### Send with Timeout Example Source: https://github.com/challengermode/opentransit/blob/develop/doc/content/3.documentation/1.concepts/3.producers.md An example of how to send a message with a timeout using a CancellationTokenSource. ```csharp var timeout = TimeSpan.FromSeconds(30); using var source = new CancellationTokenSource(timeout); await endpoint.Send(new SubmitOrder { OrderId = "123" }, source.Token); ``` -------------------------------- ### Edit Program.cs Source: https://github.com/challengermode/opentransit/blob/develop/doc/content/2.quick-starts/5.postgresql.md Change UsingInMemory to UsingPostgres. ```csharp public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureServices((hostContext, services) => { services.AddOptions().Configure(options => { options.Host = "localhost"; options.Database = "sample"; options.Schema = "transport"; options.Role = "transport"; options.Username = "masstransit"; options.Password = "H4rd2Gu3ss!"; // credentials to run migrations options.AdminUsername = "migration-user"; options.AdminPassword = "H4rderTooGu3ss!!"; }); // MassTransit will run the migrations on start up services.AddPostgresMigrationHostedService(); services.AddMassTransit(x => { // elided... x.UsingPostgres((context,cfg) => { cfg.ConfigureEndpoints(context); }); }); services.AddHostedService(); }); ``` -------------------------------- ### Run RabbitMQ Docker Container Source: https://github.com/challengermode/opentransit/blob/develop/doc/content/2.quick-starts/2.rabbitmq.md Command to run the MassTransit RabbitMQ Docker image. ```bash docker run -p 15672:15672 -p 5672:5672 masstransit/rabbitmq ``` -------------------------------- ### SubmitOrder Record Example Source: https://github.com/challengermode/opentransit/blob/develop/doc/content/3.documentation/1.concepts/1.messages.md An example of a simple message contract defined as a C# record. ```csharp public record SubmitOrder { public string Sku { get; init; } public int Quantity { get; init; } } ``` -------------------------------- ### Build for Edge Side Rendering Source: https://github.com/challengermode/opentransit/blob/develop/doc/README.md Builds the application for deployment to serverless environments. ```bash yarn build ``` -------------------------------- ### Client Publishing a Message Source: https://github.com/challengermode/opentransit/blob/develop/documentation/docs/tutorials/basic-communication.md Example of a Client publishing a SubmitOrder message. ```csharp await _bus.Publish(new SubmitOrder { OrderId = Guid.NewGuid(), OrderDate = DateTime.UtcNow }); ``` -------------------------------- ### Consumer Test Example Source: https://github.com/challengermode/opentransit/blob/develop/doc/content/3.documentation/1.concepts/5.testing.md Example of testing a consumer using the MassTransit Test Harness. ```csharp [Test] public async Task ASampleTest() { await using var provider = new ServiceCollection() .AddMassTransitTestHarness(cfg => { cfg.AddConsumer(); }) .BuildServiceProvider(true); var harness = provider.GetRequiredService(); await harness.Start(); var client = harness.GetRequestClient(); await client.GetResponse(new { OrderId = InVar.Id, OrderNumber = "123" }); Assert.IsTrue(await harness.Sent.Any()); Assert.IsTrue(await harness.Consumed.Any()); var consumerHarness = harness.GetConsumerHarness(); Assert.That(await consumerHarness.Consumed.Any()); // test side effects of the SubmitOrderConsumer here } ``` -------------------------------- ### Run RabbitMQ Docker Container (ARM) Source: https://github.com/challengermode/opentransit/blob/develop/doc/content/2.quick-starts/2.rabbitmq.md Command to run the MassTransit RabbitMQ Docker image on ARM platforms. ```bash docker run --platform linux/arm64 -p 15672:15672 -p 5672:5672 masstransit/rabbitmq ``` -------------------------------- ### Batch Consumer Example Source: https://github.com/challengermode/opentransit/blob/develop/doc/content/3.documentation/1.concepts/2.consumers.md An example of a batch consumer that consumes a batch of messages. ```csharp class BatchMessageConsumer : IConsumer> { public async Task Consume(ConsumeContext> context) { for(int i = 0; i < context.Message.Length; i++) { ConsumeContext message = context.Message[i]; } } } ``` -------------------------------- ### Example Job Consumer Implementation Source: https://github.com/challengermode/opentransit/blob/develop/doc/content/3.documentation/3.patterns/13.job-consumers.md An example implementation of a job consumer for a 'ConvertVideo' job. ```csharp public class ConvertVideoJobConsumer : IJobConsumer { public async Task Run(JobContext context) { await Task.Delay(30000, context.CancellationToken); } } ``` -------------------------------- ### Clone Repository Source: https://github.com/challengermode/opentransit/blob/develop/doc/README.md Clones the Docus starter template using nuxi. ```bash npx nuxi init docs -t nuxt-themes/docus-starter ``` -------------------------------- ### Concrete Consume Filter Example Source: https://github.com/challengermode/opentransit/blob/develop/doc/content/3.documentation/2.configuration/3.middleware/2.scoped.md Example of defining a concrete consume filter. ```csharp public class MyMessageConsumeFilter : IFilter> ``` -------------------------------- ### Local DynamoDB Client Example Source: https://github.com/challengermode/opentransit/blob/develop/doc/content/3.documentation/2.configuration/4.persistence/dynamodb.md Example of creating an AmazonDynamoDBClient for a local DynamoDB instance. ```csharp var dynamoDbClient = new AmazonDynamoDBClient(new AmazonDynamoDBConfig { ServiceURL = "http://localhost:4566" }); ``` -------------------------------- ### Add MassTransit.RabbitMQ Package Source: https://github.com/challengermode/opentransit/blob/develop/doc/content/2.quick-starts/2.rabbitmq.md Command to add the MassTransit.RabbitMQ NuGet package to a project. ```bash dotnet add package MassTransit.RabbitMQ ``` -------------------------------- ### Concurrency Limit Filter Example Source: https://github.com/challengermode/opentransit/blob/develop/doc/content/3.documentation/2.configuration/3.middleware/1.filters.md Example of using the concurrency limit filter. ```csharp cfg.ReceiveEndpoint("submit-order", e => { e.UseConcurrencyLimit(4); e.ConfigureConsumer(context); }); ```