help
Root Question Message
services.TryAddScoped<IInContactRestAPI>
(
serviceProvider => new InContactRestAPI
(
serviceProvider.GetRequiredService<IRestHttpClient>(),
serviceProvider.GetRequiredService<IOptionsMonitor<JsonSerializerOptions>>().Get(InContact),
serviceProvider.GetRequiredService<IOptions<InContactConfig>>(),
serviceProvider.GetRequiredService<ITokenStore>()
)
);
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.IInContactRestAPI
, right?var restApi = serviceProvider.GetService<IInContactRestAPI>();
if (restApi is null)
{
return Result.FromError
(new InvalidOperationError($"Unable to retrieve an instance of {nameof(InContactRestAPI)}"));
}
using var scope = serviceProvider.CreateScope();
var restApi = scope.ServiceProvider.GetService<IInContactRestAPI>();
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)}"));
}