Events configured through convention don't result in subscription

93 views Asked by At

My application is trying to receive events from an endpoint.
If I Subscribe<>() explicitly I see a subscription coming in on the publishing endpoint. If I try to get the same result by configuring a convention, it fails.

What am I missing?

My configuration looks like this:

public Subscriber(int maxConcurrency)
{
    var pingEventType = typeof(IMyEvent);
    var pingHandlerType = typeof(MyEventHandler);

    var busConfiguration = new BusConfiguration();
    busConfiguration.EndpointName("MyEndpointName");
    busConfiguration.UseSerialization<JsonSerializer>();
    busConfiguration.EnableInstallers();
    busConfiguration.UsePersistence<NHibernatePersistence>();
    busConfiguration
        .UseTransport<SqlServerTransport>()
        .CallbackReceiverMaxConcurrency(maxConcurrency);
    busConfiguration.AssembliesToScan(AllAssemblies
        .Matching("NServiceBus")
        .And("ServiceControl")
        .And(pingHandlerType.AssemblyQualifiedName)
        .And(pingEventType.AssemblyQualifiedName));
    busConfiguration
        .Conventions()
        .DefiningEventsAs(x => x.Namespace != null && x.Namespace == pingEventType.Namespace);

    var container = new ContainerBuilder().Build();
    busConfiguration.UseContainer<AutofacBuilder>(customizations =>
    {
        customizations.ExistingLifetimeScope(container);
    });
    _startableBus = Bus.Create(busConfiguration);

    // Uncommenting below line results in subscription on the publishing endpoint
    //_startableBus.Subscribe(pingEventType);
}

The most relevant documentation and sample code doesn't seem to mention anything else I should do https://docs.particular.net/nservicebus/messaging/conventions?version=core_5

2

There are 2 answers

0
Boris Callens On

Found it. AssemblyQualifiedName is the name of the type prefixed with the assembly. What I needed was the name of the assembly itself. So

busConfiguration.AssembliesToScan(AllAssemblies
    .Matching("NServiceBus")
    .And("ServiceControl")
    .And(pingHandlerType.AssemblyQualifiedName)
    .And(pingEventType.AssemblyQualifiedName));

Should have been

busConfiguration.AssembliesToScan(AllAssemblies
    .Matching("NServiceBus")
    .And("ServiceControl")
    .And(pingHandlerType.Assembly.GetName().Name)
    .And(pingEventType.Assembly.GetName().Name));

What threw me off for hours was that type.Assmembly.FullName is the name of the assembly with all it's version info etc attached. That doesn't work either.

1
Phil Sandler On

Do you have a handler for IMyEvent? Auto-subscribe only works if a handler exists for the given event in a scanned assembly.

https://docs.particular.net/nservicebus/messaging/publish-subscribe/controlling-what-is-subscribed?version=core_5#automatic-subscriptions