Skip to content

Instantly share code, notes, and snippets.

@MeikelLP
Last active August 24, 2021 20:21
Show Gist options
  • Save MeikelLP/0700689fbcdbd2c7d44cdbe7ff41d212 to your computer and use it in GitHub Desktop.
Save MeikelLP/0700689fbcdbd2c7d44cdbe7ff41d212 to your computer and use it in GitHub Desktop.
How to send message sync in MassTransit?
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MassTransit.Extensions.DependencyInjection" Version="7.1.8" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="5.0.1" />
<PackageReference Include="MassTransit" Version="7.1.8" />
</ItemGroup>
</Project>
using System;
using System.Threading.Tasks;
using MassTransit;
using Microsoft.Extensions.DependencyInjection;
namespace MinimalSyncExample
{
internal static class Program
{
private static async Task Main(string[] args)
{
var services = new ServiceCollection();
services.AddMassTransit(x =>
{
x.AddConsumer<TestJobConsumer>();
x.AddBus(p => Bus.Factory.CreateUsingInMemory(cfg =>
{
cfg.ReceiveEndpoint(ep =>
{
ep.ConfigureConsumers(p);
});
cfg.ConcurrentMessageLimit = 1;
Console.WriteLine("Using in memory");
}));
});
var provider = services.BuildServiceProvider();
var bus = provider.GetRequiredService<IBusControl>();
await bus.StartAsync();
var endpoint = provider.GetRequiredService<IPublishEndpoint>();
var data = new TestJob
{
Test = "Testificate"
};
await endpoint.Publish(data);
}
}
public class TestJobConsumer : IConsumer<TestJob>
{
public Task Consume(ConsumeContext<TestJob> context)
{
Console.WriteLine("Message: " + context.Message.Test);
return Task.CompletedTask;
}
}
public class TestJob
{
public string Test { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment