NServiceBus依赖注入 [英] NServiceBus Dependency Injection

查看:62
本文介绍了NServiceBus依赖注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对此一直有点麻烦.

AndreasÖhlund在此处上回答了一个问题,但我一直无法获得它根据他的建议进行工作.

Andreas Öhlund answered a question on it here, but I've been unable to get it to work using the advice he gave.

这是我的设置:

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将保存所有内容.

The idea is specific command handlers override the OnProcess method and do their thing, then the DomainRepository will save everything.

这是我注册组件的方式:

Here is how I've registered the components:

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).

Those are all the objects down the chain that are used by the DomainRepository; however, when I receive a command, the DomainRepository is null. If I comment out the lines to register the objects that DomainRepository needs, I'll actually get an error saying it failed to create it (Autofac DependencyResolutionException).

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

It should be noted that all the other objects use constructor injection (they're taken from a previously existing project). I tried changing them to use public property injection, but it didn't make any difference.

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

It would be much appreciated if somebody could point out what I'm doing wrong here!

推荐答案

将init方法中的代码移动到实现INeedInitialization的其他类中.在该处,使用Configure.Instance而不是Configure.With(),也不要使用Configure.Instance.DefaultBuilder().

Move the code in your init method into a different class which implements INeedInitialization. In there, use Configure.Instance instead of Configure.With() and also instead of Configure.Instance.DefaultBuilder().

这篇关于NServiceBus依赖注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆