Reputation: 3
The following code just crashes the application, no errors are caught and the application simply shuts down when calling Publish. What I'm really trying to do is to perform unittests, this code is just the abstract in order to provide a clean example. When I run the corresponding code as a unittest, I get an errormessage saying that the thread is aborting. The code is using version 3.5.6 of the MassTransit nuget package.
Can anyone see what I am doing wrong?
using System;
using System.Threading.Tasks;
using MassTransit;
namespace CustomerService.Console
{
class Program
{
public static void Main(string[] args)
{
Task.Run(() => PerformMassTransitTest()).GetAwaiter().GetResult();
}
public static async void PerformMassTransitTest()
{
var busControl = Bus.Factory.CreateUsingInMemory(config =>
{
config.ReceiveEndpoint("myQueue", endpoint => { endpoint.Consumer<TestMessageConsumeer>(); });
});
try
{
await busControl.StartAsync();
await busControl.Publish(new TestMessage());
System.Console.ReadKey();
}
catch (Exception err)
{
System.Console.WriteLine(err);
}
System.Console.ReadLine();
}
}
public class TestMessage
{
}
public class TestMessageConsumeer : IConsumer<TestMessage>
{
public Task Consume(ConsumeContext<TestMessage> context)
{
System.Console.WriteLine("Consuming");
return Task.FromResult("Consumeed");
}
}
}
Upvotes: 0
Views: 206
Reputation: 19610
In order for your async method to be awaited properly, it must return Task
. Since you return void
, there is nothing to await so the thread gets disposed. This is not related to MassTransit, just regular async/await thing.
When you change your method to return task, you can simplify the call by using TaskUtil.Await(() => PerformMassTransitTest());
As per unit tests, you can use the bus harness to test your consumers, like it is shown here. The documentation is lagging behind but it is coming.
Upvotes: 1