Last active
August 24, 2021 20:21
-
-
Save MeikelLP/0700689fbcdbd2c7d44cdbe7ff41d212 to your computer and use it in GitHub Desktop.
How to send message sync in MassTransit?
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
<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> |
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
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