C
C#5mo ago
watermelon

How can I get service provider from service collection?

Hello, everyone. am working on console app using meditR, want to get service provider from service collection. I've got a compile error on ServiceCollection.BuildServiceProvider
IServiceCollection' does not contain a definition for 'BuildServiceProvider' and no accessible extension method 'BuildServiceProvider' accepting a first argument of type 'IServiceCollection' could be found (are you missing a using directive or an assembly reference?
IServiceCollection' does not contain a definition for 'BuildServiceProvider' and no accessible extension method 'BuildServiceProvider' accepting a first argument of type 'IServiceCollection' could be found (are you missing a using directive or an assembly reference?
No description
5 Replies
Angius
Angius5mo ago
Since you seem to be using DI here, why not make UrlController a part of the DI container as well, and simply inject the IMediator? Setting up DI and then not using DI is counterproductive
Pobiega
Pobiega5mo ago
var services = new ServiceCollection();
... // add your stuff here
var provider = services.BuildServiceProvider();

var mediator = provider.GetRequiredService<IMediator>();
var services = new ServiceCollection();
... // add your stuff here
var provider = services.BuildServiceProvider();

var mediator = provider.GetRequiredService<IMediator>();
but yeah, agree with ZZZZZZ, register your controller too let it resolve the mediator internally
watermelon
watermelonOP5mo ago
services.BuildServiceProvider(); I have a compile error here
Angius
Angius5mo ago
Are we... supposed to know what error it is?
watermelon
watermelonOP5mo ago
ah seems working now, vs code is full of bugs, restarted it and its fine now btw, I didnt do anything just restarted my IDE Thanks anyway @ZZZZZZZZZZZZZZZZZZZZZZZZZ @Pobiega

Did you find this page helpful?