-
-
Notifications
You must be signed in to change notification settings - Fork 242
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Could not find any registered services for type #207
Comments
namespace DecoratorDemo
{
internal class Program
{
static void Main(string[] args)
{
var container = new Container();
var serviceProvider = new ServiceCollection()
.AddSimpleInjector(container)
.AddServices()
.BuildServiceProvider()
.UseSimpleInjector(container);
var command = serviceProvider.GetRequiredService<ICommandHandler<FooCommand>>();
command.Handle(new FooCommand());
}
}
public static class ServiceCollectionExtensions
{
public static IServiceCollection AddServices(this IServiceCollection services)
{
// *********** Can NOT Decorate ***********
return services
.AddTransient(typeof(ICommandHandler<>), typeof(CommandHandler<>))
.Decorate(typeof(ICommandHandler<>), typeof(CommandHandlerDecorator<>));
// *********** Can Decorate ***********
//return services
// .AddTransient<ICommandHandler<FooCommand>, CommandHandler<FooCommand>>()
// .Decorate(typeof(ICommandHandler<>), typeof(CommandHandlerDecorator<>));
}
}
} |
Hello @NeoElitech! 👋🏻 Sorry for the long reply time. The issue with open generic decoration is that Scrutor needs to know which concrete services are registered ahead of time. It does this by inspecting the concrete/closed services already registered in the container. If you replace the first services.Scan(scan => scan
.FromAssemblyOf(typeof(ICommandHandler<>))
.AddClasses(classes => classes.AssignableTo(typeof(ICommandHandler<>)))
.AsImplementedInterfaces()
.WithTransientLifetime()); Scrutor will can the specified assembly and add all closed generic services that implements When you then call |
I'm running into a similar problem, and the suggested solution doesn't provide any help.
And this throws the error:
|
Hi @khellang,
I'm using Scrutor to decorate an open generic type but I receive the following exception.
Could not find any registered services for type 'DecoratorDemo.ICommandHandler<TCommand>
I found a similar issue. But it's fixed already.
Here is the link to the repository
I'm using .NET.4.8 and Scrutor.4.2.2
The text was updated successfully, but these errors were encountered: