Scrutor
Scrutor copied to clipboard
Some types do not get decorated
I have a very strange symptom. Some classes (CommandHandlers) have been decorated and others with exactly the same structure, located in a different directory (different namespace) are not.
I have no idea where to look for the cause.
The class structure is as follows:
internal static class Create
{
public record Command( ... ) : ICommand<int>;
public class CommandHandler : ICommandHandler<Command, int>
{
...
}
}
Dispatcher looks like this:
public async Task<TResult> SendAsync<TResult>(ICommand<TResult> command, CancellationToken token = default)
{
var handlerType = typeof(ICommandHandler<,>).MakeGenericType(command.GetType(), typeof(TResult));
using var scope = _serviceFactory.CreateScope();
var handler = scope.ServiceProvider.GetRequiredService(handlerType);
...
}
Registration:
builder.Services.Scan(s => s.FromAssemblies(assemblies)
.AddClasses(c =>
{
c.AssignableToAny(
typeof(ICommandHandler<,>),
typeof(ICommandHandler<>),
typeof(IQueryHandler<,>),
typeof(IEventHandler<>))
.WithoutAttribute<DecoratorAttribute>();
}, false)
.AsImplementedInterfaces()
.WithScopedLifetime());
I have absolutely no idea why decorating works that inconsistently. 😢
Hey @dario-l! 👋🏻
I don't know what to tell you; it sounds weird to me too, but until I have a repro, there's not much I can do with the little information I have from this issue 😔
Hi @khellang Thanks for the response. I'm just imbecil but I will write my experience with that.
All handlers have the following generic constraints: class, IMessage
but becouse of my stupidness I slap on my decorators also constraint new()
.
Because of this, decorators only worked on messages that had an empty/default constructor.
From my perspective some works but some other do not. 😄