1

Adding windows service support in Main method of console project:

var myVariable = "example";
Host.CreateDefaultBuilder().ConfigureServices((hostContext, services) =>
{
    services.AddHostedService<Worker>();
}).UseWindowsService().Build().Run();

Worker class:

public class Worker : BackgroundService
{
....
    protected override Task ExecuteAsync(CancellationToken stoppingToken)
    {
        // Need to use myVariable here
    }
}

How to pass myVariable to worker class instance?

1

1 Answer 1

1

If I have misunderstood please let me know, but if your worker had the appropriate constructor defined:

public class Worker : IWorker 
{
     private readonly string example = String.Empty;
     public Worker(string example) => this.example = example;

     ...
}

When you do your dependency injection you would do something along these lines:

var example = "Passed?";
services.AddSingleton<IWorker>(configure => new Worker(passed));

I believe that is what you are inquiring.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.