Je tente d'implémenter le modèle CQRS dans mon application. J'ai donc trouvé comment enregistrer tous les gestionnaires de commande de l'assemblage ici: Autofac resolve dependency in CQRS CommandDispatcherAutofac.Core.Registration.ComponentNotRegisteredException
Mais ça ne marche pas bien pour moi. Voici le code:
containerBuilder.RegisterAssemblyTypes(assembly)
.AsClosedTypesOf(typeof(ICommandHandler<>));
containerBuilder.RegisterAssemblyTypes(assembly)
.AsClosedTypesOf(typeof(IQueryHandler<,>));
Handlers usine
public class CqrsHandlerFactory : ICqrsHandlerFactory
{
private readonly IContainer container;
public CqrsHandlerFactory(IContainer container)
{
this.container = container;
}
public ICommandHandler<TCommand> GetCommandHandler<TCommand>(TCommand command) where TCommand : class, ICommand
{
return container.Resolve<ICommandHandler<TCommand>>();
}
public IQueryHandler<TQuery, object> GetQueryHandler<TQuery>(TQuery query) where TQuery : class, IQuery
{
return container.Resolve<IQueryHandler<TQuery, object>>();
}
}
Bus
public class CqrsBus : ICqrsBus
{
private readonly ICqrsHandlerFactory cqrsHandlerFactory;
public CqrsBus(ICqrsHandlerFactory cqrsHandlerFactory)
{
this.cqrsHandlerFactory = cqrsHandlerFactory;
}
public void ExecuteCommand(ICommand command)
{
var handler = cqrsHandlerFactory.GetCommandHandler(command);
if (handler == null)
throw new NotImplementedHandlerException(string.Format("Cannot find handler for {0}", command.GetType()));
handler.Handle(command);
}
public TResult RunQuery<TResult>(IQuery query)
{
var handler = cqrsHandlerFactory.GetQueryHandler(query);
if (handler == null)
throw new NotImplementedHandlerException(string.Format("Cannot find handler for {0}", query.GetType()));
return (TResult)handler.Handle(query);
}
}
Exception
Une exception de type 'Autofac.Core.Registration.ComponentNotRegisteredException' a eu lieu dans Autofac.dll mais n'a pas été manipulé dans le code utilisateur Informations supplémentaires: Le service demandé 'PromocjeWsieciowkach.Messaging.Core.ICommandHandler`1 [[PromocjeWsieciowkach.Messaging.Core.ICommand, PromocjeWsieciowkach.Messaging.Core, Version = 1.0.0.0, Culture = neutre, PublicKeyToken = null] ] 'n'a pas été enregistré. Pour éviter cette exception, enregistrez un composant pour fournir le service, recherchez l'enregistrement du service à l'aide de IsRegistered() ou utilisez la méthode ResolveOptional() pour résoudre une dépendance facultative.
Stacktrace
à Autofac.ResolutionExtensions.ResolveService (contexte IComponentContext, Service de service, IEnumerable
1 parameters) at Autofac.ResolutionExtensions.Resolve[TService](IComponentContext context, IEnumerable
1 paramètres) à PromocjeWsieciowkach.Messaging.Factories.CqrsHandlerFactory.GetCommandHandler [TCommand] (commande TCommand) dans C: \ Utilisateurs \ Daniel \ Bureau \ PromocjeWsieciowkach \ src \ PromocjeWsieciowkach.Messaging \ Factories \ CqrsHandlersFactory.cs: ligne 17 à PromocjeWsieciowkach.Messaging.Bus.CqrsBus.ExecuteCommand (commande ICommand) dans C: \ Users \ Daniel \ Desktop \ PromocjeWsieciowkach \ src \ PromocjeWsieciowkach.Messaging \ Bus \ CqrsBus.cs: ligne 17 à PromocjeWsieciowkach.Controllers.PostController.Index() dans C: \ Users \ Daniel \ Desktop \ PromocjeWsieciowkach \ src \ PromocjeWsieciowkach \ Controllers \ PostController.cs: ligne 20 à lambda_method (fermeture, l'objet, l'objet []) à Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d__28.MoveNext()
Alors qu'est-ce que je fais mal?
Veuillez remplacer l'ima ges avec le code actuel et le texte du message d'exception (et toujours inclure des traces de pile). – Steven
Veuillez inclure le code qui appelle cette méthode 'GetCommandHandler'. –
Steven
J'ai modifié mon message. – TjDillashaw