NServiceBus Dependency Injection

此生再无相见时 提交于 2019-12-07 12:12:48

问题


I've been having a bit of trouble with this.

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

Here's my setup:

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

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);
    }
}

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!


回答1:


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



来源:https://stackoverflow.com/questions/14144811/nservicebus-dependency-injection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!