question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Worker Template Suggestion - Use Different ConfigureServices Overload by Default

See original GitHub issue

Currently, when using the new Worker template, it includes a call to ConfigureServices to add the hosted service.

public static IHostBuilder CreateHostBuilder(string[] args) =>
	Host.CreateDefaultBuilder(args)
		.ConfigureServices(services =>
		{
			services.AddHostedService<Worker>();
		});

A common requirement I have for worker services is that we will have some configuration which we want to bind for use from IOptions.

To support this, I have to change to the overload of ConfigureServices which accepts Action<HostBuilderContext, IServiceCollection> so that I can access the Configuration from there.

For example:

public static IHostBuilder CreateHostBuilder(string[] args) =>
	Host.CreateDefaultBuilder(args)
		.ConfigureServices((hostContext, services) =>
		{
			services.Configure<MySettings>(hostContext.Configuration.GetSection("MySettings"));
			services.AddHostedService<Worker>();
		});

Since this is likely to be a common need, I would propose that perhaps the template should include that overload by default:

public static IHostBuilder CreateHostBuilder(string[] args) =>
	Host.CreateDefaultBuilder(args)
		.ConfigureServices((hostContext, services) =>
		{
			services.AddHostedService<Worker>();
		});

I would assume that this may help guide developers towards this path for the scenario where they need to bind configuration which in my experience would be the vast majority of the workers we have.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:4
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

3reactions
davidfowlcommented, Apr 24, 2019

I like the original suggestion, we should do this. It makes it easy to do environment checks etc.

0reactions
stevejgordoncommented, Apr 24, 2019

@davidfowl PR in.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Worker Template Suggestion - Use Different ConfigureServices ...
AddHostedService<Worker>(); });. Since this is likely to be a common need, I would propose that perhaps the template should include that overload by...
Read more >
How to automatically Configure services in ASP.NET core?
I'm using Scrutor just to make life easier. Normally, this is simple to achieve with code similar to this: services.Scan(scan => scan ....
Read more >
Dependency injection guidelines - .NET
In this article. Design services for dependency injection; Default service container replacement; Thread safety; Recommendations; Example anti- ...
Read more >
Application Insights for ASP.NET Core applications
To configure any default TelemetryModule , use the extension method ConfigureTelemetryModule<T> on IServiceCollection , as shown in the ...
Read more >
HttpClientFactory in ASP.NET Core 2.1 (Part 2) - Steve Gordon
In this post we'll continue looking at the new HttpClientFactory feature of ASP.NET Core 2.0 and explore named and typed clients.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found