Tiny tiny library for receiving azure service bus messages
dotnet add package fishbus
public class SomethingExcitingJustHappened
{
public string Somedata { get; set; }
}
public class DeleteUserHandler : IHandleMessage<SomethingExcitingJustHappened>
{
public async Task Handle(SomethingExcitingJustHappened message, Func<Task> markAsCompleted)
{
Log.Information("Received SomethingExcitingJustHappened");
await markAsCompleted();
}
}
Here we just have a console app, with a Program.cs, but this could just as well be a web app. We just need a host that can host a Microsoft.Extensions.Hosting.IHostedService implementation.
public static async Task<int> Main(string[] args)
{
try
{
await new HostBuilder()
.ConfigureHostConfiguration(configHost =>
{
configHost.SetBasePath(Directory.GetCurrentDirectory());
configHost.AddEnvironmentVariables(prefix: "ASPNETCORE_");
})
.ConfigureServices((hostContext, services) =>
{
services
.ConfigureMessaging() //will register messagehandlers from current assembly
.Configure<MessageSources>(configuration.GetSection("MessageSources")); //register the MessageSources
})
.RunConsoleAsync();
return 0;
}
catch (Exception ex)
{
Log.Fatal(ex, "Host terminated unexpectedly");
return 1;
}
}
The following appsettings.json file could be used to configure the messagsources that the application will listen to
{
"MessageSources": {
"Subscriptions": [{
"ConnectionString": "",
"Name": "<name of topic>"
}
],
"Queues": [{
"ConnectionString": ""
}
]
}
}