【问题标题】:NServiceBus Dependency InjectionNServiceBus 依赖注入
【发布时间】:2013-01-03 18:19:42
【问题描述】:

我在这方面遇到了一些麻烦。

Andreas Öhlund 回答了一个关于它的问题 here,但我无法使用他给出的建议让它工作。

这是我的设置:

public abstract class CommandHandler<T> : IHandleMessages<T>, IDomainReadRepository where T : Command
{
    public IDomainRepository DomainRepository { get; set; }

    protected abstract void OnProcess(T command);

    public TAggregate GetById<TAggregate>(Guid id) where TAggregate : IEventProvider, new()
    {
        return DomainRepository.GetById<TAggregate>(id);
    }

    public void Handle(T message)
    {
        OnProcess(message);
        // Domain repository will save.
    }
}

这个想法是特定的命令处理程序覆盖 OnProcess 方法并做他们的事情,然后 DomainRepository 将保存所有内容。

这是我注册组件的方式:

public class EndpointConfig : IConfigureThisEndpoint, AsA_Server, IWantCustomInitialization
{
    public void Init()
    {
        Configure.With().DefiningCommandsAs(c => c.Namespace != null && c.Namespace.EndsWith("Commands"));
        Configure.Instance.DefaultBuilder().Configurer.ConfigureComponent<DomainRepository>(DependencyLifecycle.InstancePerCall);
        Configure.Instance.DefaultBuilder().Configurer.ConfigureComponent<EventStore.Sql.EventStore>(DependencyLifecycle.InstancePerCall);
        Configure.Instance.DefaultBuilder().Configurer.ConfigureComponent<MongoDbObjectSecurityDescriptorRepository>(DependencyLifecycle.InstancePerCall);
        Configure.Instance.DefaultBuilder().Configurer.ConfigureComponent<LocalTenantConfig>(DependencyLifecycle.InstancePerCall);
    }
}

这些是 DomainRepository 使用的链下的所有对象;但是,当我收到命令时, DomainRepository 为空。如果我注释掉注册 DomainRepository 需要的对象的行,我实际上会收到一个错误,说它无法创建它(Autofac DependencyResolutionException)。

应该注意,所有其他对象都使用构造函数注入(它们取自先前存在的项目)。我尝试将它们更改为使用公共属性注入,但没有任何区别。

如果有人能指出我在这里做错了什么,将不胜感激!

【问题讨论】:

    标签: dependency-injection nservicebus nservicebus3


    【解决方案1】:

    将 init 方法中的代码移动到实现 INeedInitialization 的不同类中。在那里,使用 Configure.Instance 而不是 Configure.With() 和 Configure.Instance.DefaultBuilder()。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-08
      • 2014-06-12
      相关资源
      最近更新 更多