-
-
Notifications
You must be signed in to change notification settings - Fork 274
Open
Description
Description
I am trying to decorate an Open Generic repository using Scrutor, but I receive a DecorationException stating that the service could not be found, even though the service is registered immediately before the Decorate call.
Information about error
i try before posting the issue to ensure that service already register using the following code before decorate extension method
var serviceDescriptor = services
.FirstOrDefault(s => s.ServiceType == typeof(IRepository<>));
if (serviceDescriptor != null)
{
Console.WriteLine($"[DEBUG] Found: {serviceDescriptor.ServiceType.Name}");
Console.WriteLine($"[DEBUG] Implemented by: {serviceDescriptor.ImplementationType?.Name}");
}
else
{
Console.WriteLine("[DEBUG] CRITICAL: IGenericService<> is NOT registered yet!");
}and it's give me this massage
[DEBUG] Found: IRepository`1
[DEBUG] Implemented by: Repository`1
Unhandled exception. Scrutor.DecorationException: Could not find any registered services for type 'Domain.Repositories.IRepository<TEntity>'.
at Microsoft.Extensions.DependencyInjection.ServiceCollectionExtensions.Decorate(IServiceCollection services, DecorationStrategy strategy)
at Microsoft.Extensions.DependencyInjection.ServiceCollectionExtensions.Decorate(IServiceCollection services, Type serviceType, Type decoratorType)
at Infrastructure.Shared.DependencyInjection.AddSharedInfrastructure(IServiceCollection services, IConfiguration configuration) in D:\projects\AssignmentAPI\src\Infrastructure\Infrastructure.Shared\DependencyInjection.cs:line 40
at WebAPI.Program.Main(String[] args) in project\src\Presentation\WebAPI\Program.cs:line 42
Code Snippets
Registration (Persistence Layer):
public static IServiceCollection AddInfrastructurePersistence(this IServiceCollection services, IConfiguration configuration)
{
services.AddScoped(typeof(IRepository<>), typeof(Repository<>));
return services;
}Decorate (Shared Layer):
public static IServiceCollection AddInfrastructurePersistence(this IServiceCollection services, IConfiguration configuration)
{
services.Decorate(typeof(IRepository<>), typeof(CachedRepository<>));
return services;
}Register all project layers (WebAPI Layer):
builder.Services
.AddApplication()
.AddInfrastructurePersistence(builder.Configuration) // => Data Layer
.AddInfrastructureIdentity(builder.Configuration)
.AddSharedInfrastructure(builder.Configuration) // => Shared Layer
.AddWebAPI(builder.Configuration);Classes and Interface used
public interface IRepository<TEntity>
where TEntity : class
{}
public class Repository<TEntity>(AuthDbContext context)
: IRepository<TEntity>
where TEntity : class
{}
public class CachedRepository<TEntity>(IRepository<TEntity> repository,
IDistributedCache distributedCache,
IConnectionMultiplexer redis)
: IRepository<TEntity>
where TEntity : class
{}blackaly and MohamedAYassin
Metadata
Metadata
Assignees
Labels
No labels