Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NServiceBus.Extensions.DependencyInjection can't be used #265

Open
DavidBoike opened this issue Jan 27, 2023 · 4 comments
Open

NServiceBus.Extensions.DependencyInjection can't be used #265

DavidBoike opened this issue Jan 27, 2023 · 4 comments
Labels
enhancement New feature or request

Comments

@DavidBoike
Copy link
Member

If you want to create a send-only endpoint, a ServiceBusTriggeredEndpointConfiguration doesn't make any sense, as there won't be a queue.

But setting up a send-only NServiceBus endpoint using NServiceBus.Extensions.DependencyInjection won't work either, because the .UseNServiceBus(…) extension method doesn't support IFunctionsHostBuilder.

I think this also affects the InProcess hosting model as IFunctionsHostBuilder is located in Microsoft.Azure.Functions.Extensions.DependencyInjection.

@timbussmann
Copy link
Contributor

@DavidBoike shouldn't this issue be focused on support for send-only configurations rather than on using Microsoft.Azure.Functions.Extensions.DependencyInjection then?

@DavidBoike
Copy link
Member Author

Perhaps. I wrote the issue at the time based on the problem the customer was having.

@timbussmann
Copy link
Contributor

FYI #220 also describes a workaround that should allow using "SendOnly" mode (using no trigger or message pump)

@timbussmann
Copy link
Contributor

timbussmann commented Apr 4, 2023

Using the worker mode, I think someting like the following approach should work as a workaround (also enabling usage of different transports to send messages) but I'd rather have a better integration with the known UseNServiceBus config API.

var host = new HostBuilder()
    .ConfigureFunctionsWorkerDefaults()
    .ConfigureServices(serviceCollection =>
    {
        var configuration = new EndpointConfiguration("SendOnlyEndpoint");

        var routing = configuration.UseTransport(new AzureServiceBusTransport(Environment.GetEnvironmentVariable(...)));

        configuration.SendOnly();

        var startableEndpoint = EndpointWithExternallyManagedContainer.Create(configuration, serviceCollection);

        serviceCollection.AddHostedService(sp => new NServiceBusService(sp, startableEndpoint));
        // inject IMessageSession into the function ctor:
        serviceCollection.AddSingleton<IMessageSession>(sp => NServiceBusService.endpointInstance);
    })
    .Build();

host.Run();


class NServiceBusService : IHostedService
{
    // if accessing too early, this will be null
    public static IEndpointInstance endpointInstance = null!;

    IServiceProvider serviceProvider;
    IStartableEndpointWithExternallyManagedContainer startableEndpoint;

    public NServiceBusService(IServiceProvider serviceProvider, IStartableEndpointWithExternallyManagedContainer startableEndpoint)
    {
        this.serviceProvider = serviceProvider;
        this.startableEndpoint = startableEndpoint;
    }

    public async Task StartAsync(CancellationToken cancellationToken)
    {
        var endpoint = await startableEndpoint.Start(serviceProvider);
        endpointInstance = endpoint;
    }

    public Task StopAsync(CancellationToken cancellationToken)
    {
        return endpointInstance.Stop();
    }
}

note that this doesn't properly configure logging in NServiceBus, so this isn't production ready code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants