C
C#

help

ServiceProvider not providing registered service [Answered]

FFoxtrek_648/15/2022
For some reason, despite registering this api wrapper as so:
services.TryAddScoped<IInContactRestAPI>
(
serviceProvider => new InContactRestAPI
(
serviceProvider.GetRequiredService<IRestHttpClient>(),
serviceProvider.GetRequiredService<IOptionsMonitor<JsonSerializerOptions>>().Get(InContact),
serviceProvider.GetRequiredService<IOptions<InContactConfig>>(),
serviceProvider.GetRequiredService<ITokenStore>()
)
);
services.TryAddScoped<IInContactRestAPI>
(
serviceProvider => new InContactRestAPI
(
serviceProvider.GetRequiredService<IRestHttpClient>(),
serviceProvider.GetRequiredService<IOptionsMonitor<JsonSerializerOptions>>().Get(InContact),
serviceProvider.GetRequiredService<IOptions<InContactConfig>>(),
serviceProvider.GetRequiredService<ITokenStore>()
)
);
Attempting to retrieve it from the service provider results in an InvalidOperationException because the type is not in the service provider. This happens whether I request the interface or the concrete implementation. Rider does in fact confirm that the scoped rest api wrapper is present in the service collection when stepping through.
FFoxtrek_648/15/2022
In the screeshotted sample above, I changed GetRequiredService() to GetService() to avoid the throw since I'm using a result mechanism, but as you can see it hits the return Result.FromError() line because the returned service instance is null.
TTvde18/15/2022
how are you retrieving it from the service provider? you are retrieving the IInContactRestAPI, right? be sure to create a scope
FFoxtrek_648/15/2022
var restApi = serviceProvider.GetService<IInContactRestAPI>();
if (restApi is null)
{
return Result.FromError
(new InvalidOperationError($"Unable to retrieve an instance of {nameof(InContactRestAPI)}"));
}
var restApi = serviceProvider.GetService<IInContactRestAPI>();
if (restApi is null)
{
return Result.FromError
(new InvalidOperationError($"Unable to retrieve an instance of {nameof(InContactRestAPI)}"));
}
You know what, that might be it entirely. I don't know if the plugin service creates a scope per plugin
TTvde18/15/2022
you need to do
using var scope = serviceProvider.CreateScope();
var restApi = scope.ServiceProvider.GetService<IInContactRestAPI>();
using var scope = serviceProvider.CreateScope();
var restApi = scope.ServiceProvider.GetService<IInContactRestAPI>();
typed that without intellisense so I don't know if it's correct
FFoxtrek_648/15/2022
FFoxtrek_648/15/2022
Looks like it's happy now
TTvde18/15/2022
😁
FFoxtrek_648/15/2022
await using var serviceScope = serviceProvider.CreateAsyncScope();
var restApi = serviceScope.ServiceProvider.GetService<IInContactRestAPI>();
if (restApi is null)
{
return Result.FromError
(new InvalidOperationError($"Unable to retrieve an instance of {nameof(InContactRestAPI)}"));
}
await using var serviceScope = serviceProvider.CreateAsyncScope();
var restApi = serviceScope.ServiceProvider.GetService<IInContactRestAPI>();
if (restApi is null)
{
return Result.FromError
(new InvalidOperationError($"Unable to retrieve an instance of {nameof(InContactRestAPI)}"));
}
TTvde18/15/2022
async scope, fancy
FFoxtrek_648/15/2022
I'll add that as an issue to the plugin library I think. Plugins should have their own scopes.
TTvde18/15/2022
You can /close it if it all works ^^
AAccord8/15/2022
✅ This post has been marked as answered!

Looking for more? Join the community!

Want results from more Discord servers?
Add your server
Recommended Posts
Index NavLink not working in NavMenuI have two pages, Index and Register My navbar's `SkullDugDungeon` text should redirect to Index/HomRefactoring if elseHelp ! ```cs partial void OnNameChanging(string value) { _validationService.CleaCustom MSBuild SDK not found after uploading new versionI'm fiddling with a custom MSBuild SDK, and everything has worked fine up until now. I just tried toHow to create an observable collection that filtered from other observable collectionI wanna create a filter version of the ErrorList, and I want the ErrorList add or delete Item it wilHelp with LinqThe image should be enough to explain what I want to achieveAddDistributedTokenCaches and custom IDistributedCacheAnybody with experience of AddDistributedTokenCaches ? I would like to use a custom provider insteadRazor component. The child content element 'ChildContent' of component 'EditForm' uses the same paraIf I understood correctly, I think the problem is with ***Model*** attribute, though the name is notHow to migrate table?I have database with table: Quote, so i tried to migrate new table Author on database using package What is MVC?I've seen a some quick explanations of model viewer controller but without knowing what the actual cWriteAsync doesn't always send data to all connected clientsI am making an application that has a TCP client that listens for messages from a TCP server, then Why can't I converter theIEnumable to a list ?```cs public IEnumerable GetErrors(string propertyName) { return _properNeed Help for MSGraph [Answered]Hi, I'm new here in the discord, and found out that the community here is nice. Lots of things. So Load local db file to memoryHow do I load local db file to memory?miniMax algorithmhey guys i need to implement minimax algorithm to my basic console xox game.any help appreciated. hDeserialize wrapper object in routeHello. Does anyone know if it is possible to map deserilize a custom wrapper object in a route? IDsVS JB Mono font not looking like Rider JB Mono font [Answered]I have to use vs 2022 due to rider not supporting xaml hot reload and am trying to make it look simiJsonSerializer.DeserializeAsync() randomly hangs indefinitely [Answered]```cs private static async Task<T> TryReadDataFromFile<T>(string basePath) where T : class { vardotnet6 project failing on static abstracts with preview features set to true``` C:\Users\Kellen James\source\Repos\ClemBot\ClemBot.Api\ClemBot.Api.Core\Features\Tags\Edit.cs(51I need to serialize a bunch of non-primitive types. How would I do this?I'm using JsonSerializer.serializeToUtf8Bytes I need to serialize a class that contains a bitmap, anWhen to use ValueTask?Despite having used C# for a while I'm... not *that* familiar with async. I know people sometimes sa