Reputation: 654
We are trying to migrate our application using ServiceStack to .NET endpoints. We have some services dependent on ServiceStack functionality. However, the registration fails because the ServiceStack services are not yet registered when we are adding our own service in the ConfigureServices
method. For example we have auth providers that were previously resolved from the container and and passed through the constructor when adding the providers AuthenticationFeature
. But also just regular services that get all their dependencies injected automatically.
I reproduced this in a project created with x new web
.
using ServiceStack.Auth;
[assembly: HostingStartup(typeof(AppHost))]
namespace test;
public class AppHost() : AppHostBase("test"), IHostingStartup
{
public void Configure(IWebHostBuilder builder) => builder
.ConfigureServices(services =>
{
services.AddScoped<Something>();
});
public override void Configure()
{
// Configure ServiceStack, Run custom logic after ASP.NET Core Startup
SetConfig(new HostConfig {
});
}
}
public class Something(IPasswordHasher hasher);
Producing this error on startup:
Unhandled exception. System.AggregateException: Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: test.Something Lifetime: Scoped ImplementationType: test.Something': Unable to resolve service for type 'ServiceStack.Auth.IPasswordHasher' while attempting to activate 'test.Something'.)
---> System.InvalidOperationException: Error while validating the service descriptor 'ServiceType: test.Something Lifetime: Scoped ImplementationType: test.Something': Unable to resolve service for type 'ServiceStack.Auth.IPasswordHasher' while attempting to activate 'test.Something'.
---> System.InvalidOperationException: Unable to resolve service for type 'ServiceStack.Auth.IPasswordHasher' while attempting to activate 'test.Something'.
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateArgumentCallSites(ServiceIdentifier serviceIdentifier, Type implementationType, CallSiteChain callSiteChain, ParameterInfo[] parameters, Boolean throwIfCallSiteNotFound)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateConstructorCallSite(ResultCache lifetime, ServiceIdentifier serviceIdentifier, Type implementationType, CallSiteChain callSiteChain)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.TryCreateExact(ServiceDescriptor descriptor, ServiceIdentifier serviceIdentifier, CallSiteChain callSiteChain, Int32 slot)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.GetCallSite(ServiceDescriptor serviceDescriptor, CallSiteChain callSiteChain)
at Microsoft.Extensions.DependencyInjection.ServiceProvider.ValidateService(ServiceDescriptor descriptor)
--- End of inner exception stack trace ---
at Microsoft.Extensions.DependencyInjection.ServiceProvider.ValidateService(ServiceDescriptor descriptor)
at Microsoft.Extensions.DependencyInjection.ServiceProvider..ctor(ICollection`1 serviceDescriptors, ServiceProviderOptions options)
--- End of inner exception stack trace ---
at Microsoft.Extensions.DependencyInjection.ServiceProvider..ctor(ICollection`1 serviceDescriptors, ServiceProviderOptions options)
at Microsoft.Extensions.DependencyInjection.ServiceCollectionContainerBuilderExtensions.BuildServiceProvider(IServiceCollection services, ServiceProviderOptions options)
at Microsoft.Extensions.Hosting.HostApplicationBuilder.Build()
at Microsoft.AspNetCore.Builder.WebApplicationBuilder.Build()
at Program.<Main>$(String[] args) in C:\development\test\test\test\Program.cs:line 8
How is this supposed to be done?
Upvotes: 3
Views: 39
Reputation: 143369
ServiceStack Services are registered when AddServiceStack()
is called:
services.AddServiceStack(typeof(MyServices).Assembly);
But that shouldn't make a difference since you're unable to resolve any dependencies from ASP .NET Core IOC until the app is built:
var app = builder.Build();
Which happens after all ConfigureServices()
are run.
The error message doesn't have anything to do with ServiceStack Services, it's failing because there's no IPasswordHasher
dependency registered, which there isn't because it's not used in ASP .NET Identity Auth.
You can register it yourself with:
services.AddSingleton<IPasswordHasher, PasswordHasher>();
Upvotes: 1