0

In ConfigureServices(IServiceCollection services), I added a scoped service which I want to use later to get a value for Add authentication service.

services.AddScoped<IService1, Service1>();

// Add other services

 services.AddAuthentication(
 // need to use the Service1 to get a value 
)
H H
  • 263,252
  • 30
  • 330
  • 514
nuclectro
  • 31
  • 1
  • 5

1 Answers1

0
var yourService = services.BuildServiceProvider().GetRequiredService<IService1>();

More details about this matter can be found here and here.

Credit also to Kirk Larkin and L.F. from the comments.

  • Close, but you'd better set up a Scope with a `using(){}` – H H Jun 11 '19 at 09:24
  • It's generally a bad idea to use `BuildServiceProvider` in `ConfigureServices` like this. See [this](https://stackoverflow.com/questions/56042989/what-are-the-costs-and-possible-side-effects-of-calling-buildserviceprovider-i/56058498#56058498). – Kirk Larkin Jun 11 '19 at 09:41
  • @KirkLarkin Can you post a link to that question in order to delete my answer? – Florin-Constantin Ciubotariu Jun 11 '19 at 09:45
  • While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. – L. F. Jun 11 '19 at 09:56