0

I am currently attempting to send messages to an Azure ServiceBus queue via NServiceBus 7.1.9.

We are using

  • dotNet Core 2.0
  • NServiceBus 7.1.9
  • NServiceBus.MSDependencyInjection 0.1.4
  • NServiceBus.Persistence.AzureStorage 2.3.0
  • NServiceBus.Transport.AzureServiceBus 1.1.1

However, messages appear to send, but never arrive at the destination queue.

We are attempting to use the default Microsoft Dependency Injection, which again, appears to configure correctly, but doesn't send any messages.

In startup.cs we configure the service and add it to the DI container

private void ConfigureNServiceBus(IServiceCollection services)
    {
        // Sending Service
        services.AddScoped<IServiceBusSender, ServiceBusSender>();

        // Create endpoint
        var endpointConfiguration = new EndpointConfiguration("LinkGreenEndpoint");

        // Set Queue Name
        var context = Configuration["ServiceBusContext"]?.ToLower() ?? "local";
        endpointConfiguration.OverrideLocalAddress($"horticulture.nservicebusbackend.{context}");
        // Use Azure ServiceBus Queue
        var transport = endpointConfiguration.UseTransport<AzureServiceBusTransport>();
        transport.ConnectionString(Configuration["ConnectionStrings:LinkGreenServiceBus"]);
        // ConnectionStrings:LinkGreenServiceBus= "Endpoint=sb://xxx.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=xxx"

        endpointConfiguration.UsePersistence<AzureStoragePersistence>();

        endpointConfiguration.UseContainer<ServicesBuilder>(customizations =>
        {
            customizations.ExistingServices(services);
        });


        var endpoint = NServiceBus.Endpoint.Start(endpointConfiguration).GetAwaiter().GetResult();

        // Add to Dependency Injection Container        
        services.AddScoped(typeof(IEndpointInstance), x => endpoint);

    }

To send a message we use ServiceBusSender

 public class ServiceBusSender : IServiceBusSender
    {
        private readonly IEndpointInstance _endpointInstance;
        public ServiceBusSender(IEndpointInstance endpointInstance)
        {
            _endpointInstance = endpointInstance;
        }

        public Task Send(ICommand message)
        {
            // Also tried _endpointInstance.SendLocal(); - gives "handler not found"
            return _endpointInstance.Send(message);
        }
    }

And an example of a command that we send:

 public class CloneSupplierItemCommandBase : ICommand
    {
        public int BuyerId { get; set; }

        public IdAndQuantity[] CloneItems { get; set; }
    }

We currently use NServiceBus v5.0.0 in .NET 4.5 with this ServiceBus endpoint successfully.

Trevor Watson
  • 415
  • 1
  • 8
  • 20

2 Answers2

0

Found the issue and managed to get messages posted to a queue.

Not 100% sure on which of the following was the solution

Set the name of the queue on creation to the endpoint

var context = Configuration["ServiceBusContext"]?.ToLower() ?? "local";
var endpointConfiguration = new EndpointConfiguration($"horticulture.nservicebusbackend.{context}");

Add the endpoint name to the Send() command

return _endpointInstance.Send(_endpoint, message);
Trevor Watson
  • 415
  • 1
  • 8
  • 20
0

It appears your endpoint configuration in the ConfigureNServiceBus method does not have any routing defined. Without this, the endpoint will not know where to deliver command messages.

I suspect you got it to work because you added the destination endpoint name in the Send() command directly. This works, but it can become unmanageable when you have a lot of endpoints and/or you want to modify the routing at run-time.

The better approach is to configure routing during start up. See Command Routing on Particular's documentation site.

Scott Hoffman
  • 370
  • 2
  • 10