-1

I'm setting up a Worker Service, and I need to pass a connection string to UseSqlServerStorage().

IHost host = Host.CreateDefaultBuilder(args)
    .ConfigureServices(services =>
    {
        services.AddHostedService<WorkerService>();
        services.AddHangfire(configuration => configuration
            .UseSqlServerStorage("NEEDS CONNECTION STRING HERE!"));
        services.AddHangfireServer();

    })
    .UseWindowsService()
    .Build();

In order to do that, I need to call ConfigurationManager.GetConnectionString(). But how can I access ConfigurationManager here?

1 Answer 1

0

The ConfigureServices method is overloaded and includes a method that takes a HostBuilderContext as the first parameter.

IHost host = Host.CreateDefaultBuilder(args)
    .ConfigureServices((context, services) =>
    {
        services.AddHostedService<WorkerService>();
        services.AddHangfire(configuration => configuration
            .UseSqlServerStorage(context.Configuration.GetConnectionString("HangfireConnection")));
        services.AddHangfireServer();
    })
    .UseWindowsService()
    .Build();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.