We cannot upgrade to the latest versions of the .Net Framework because we are still dependent on MSMQ. I wrote a .Net 4.8.1 web api. I am using the Microsoft.Extensions to be Core like. That said I have a service configuration registration class coded like this:
using System.Web.Http;
using Microsoft.Extensions.Hosting;
using NHibernate.Cfg;
using NServiceBus;
public class ServiceConfig
{
private static IHostBuilder builder;
public static void Register(HttpConfiguration config)
{
builder = Host.CreateDefaultBuilder();
builder.UseNServiceBus(HostBuilderContext => {
var endpointConfiguration = new EndpointConfiguration("XXXX.JobApp.WebService");
// configure endpoint here
var transport = endpointConfiguration.UseTransport<MsmqTransport>();
transport.Routing().DisablePublishing();
endpointConfiguration.SendOnly();
endpointConfiguration.UseSerialization<XmlSerializer>();
return endpointConfiguration;
});
builder.ConfigureLogging();
builder.AddConfigurationBindings();
builder.ConfigureServices(x => x.AddSingleton<IPing, Ping>());
builder.ConfigureServices(x => x.AddTransient<IHireApplicantDataValidator, HireApplicantDataValidator>());
builder.ConfigureServices(x => x.AddTransient<IHireEligibilityVerifier, HireEligibilityVerifier>());
builder.ConfigureServices(x => x.AddTransient<HireProcessController>());
builder.ConfigureServices(x => x.AddTransient<RehireEligibleController>());
var host = builder.Build();
host.RunAsync();
var resolver = new DependencyResolver(host.Services);
config.DependencyResolver = resolver;
}
In my controller I dependency inject IMessageSession like this:
private readonly ApplicationSettings applicationSettings;
private readonly IHireApplicantDataValidator hireApplicantDataValidator;
private readonly IHireEligibilityVerifier hireEligibilityVerifier;
private readonly ILogger<HireProcessController> log;
private readonly IMessageSession messageSession;
private int storeNumber;
public HireProcessController(ILogger<HireProcessController> singletonLog,
IOptionsSnapshot<ApplicationSettings> optionsApplicationSettings, IMessageSession singletonMessageSession
, IHireApplicantDataValidator transientHireApplicantDataValidator
, IHireEligibilityVerifier transientHireEligibilityVerifier)
{
log = singletonLog ?? throw new ArgumentNullException(nameof(ILogger<HireProcessController>));
applicationSettings = optionsApplicationSettings.Value ??
throw new ArgumentNullException(nameof(ApplicationSettings));
messageSession = singletonMessageSession ??
throw new ArgumentNullException(nameof(singletonMessageSession));
hireApplicantDataValidator = transientHireApplicantDataValidator ??
throw new ArgumentNullException(nameof(ILogger<HireProcessController>));
hireEligibilityVerifier = transientHireEligibilityVerifier ??
throw new ArgumentNullException(nameof(transientHireEligibilityVerifier));
}
I created a very simple Get method to ping the controller and send a message. It looks like this:
public IHttpActionResult Get()
{
var testMessage = new Test();
testMessage.MessageLiteral = "JobApp Api HireProcess controller is responding";
try
{
var cancellationToken = new CancellationToken();
messageSession.SendLocal(testMessage, cancellationToken);
}
catch (Exception e)
{
log.LogCritical(e.Message);
return InternalServerError();
}
log.LogInformation(testMessage.MessageLiteral);
return Ok(testMessage.MessageLiteral);
}
The problem is I do not get any message at all sent to the queue defined as “XXXX.JobApp.WebService” I was expecting the queue to be automatically created as well. I feel like I am missing something very simple?
I am using NServiceBus 8.0 because it is the last package that is 4.8.1 compatible. Also using NServicebus.Extensions.Hosting.