How to access registered components from startup task of NServiceBus 6 custom feature implementation

113 views
Skip to first unread message

Eugen Gorgan

unread,
Sep 25, 2017, 4:32:34 PM9/25/17
to Particular Software
Helpful information to include
Product name: NServiceBus
Version:6
Stacktrace:

I have a custom NServiceBus feature implementation which is using feature startup task.

Here is the Setup method of the feature

    protected override void Setup(FeatureConfigurationContext context)
        {
            context.Container.ConfigureComponent<GridFsDataBus>(DependencyLifecycle.SingleInstance);
            context.Container.ConfigureComponent<GridFsDataBusCleaner>(DependencyLifecycle.InstancePerCall);
            context.RegisterStartupTask(() => new MongoDbDataBusCleaner());
        }

I cannot find a way to access any registered components(not only registered by mine) from within the task itself since the signature of task registration in NServiceBus 6 has changed and is forcing you to instantiate the instance of the task yourself.

Here is snippet of the task

     class MongoDbDataBusCleaner : FeatureStartupTask, IDisposable
        {
            static readonly ILog Logger = LogManager.GetLogger(typeof(MongoDbDataBusCleaner));

            public IMongoDatabase Database { get; set; }
            public GridFsDataBusCleaner DataBusCleaner { get; set; }
            public ReadOnlySettings Settings { get; set; }
            
            protected override Task OnStart(IMessageSession session)
            {
             // all the class properties are null
             // how to get components registered in container

The only thing that i can get to while in OnStart method is IMessageSession which lacks any container access. 

The default property injection does not work since i had to instantiate the start up task on my own

Any help would be appreciated !

Eugen Gorgan

unread,
Sep 25, 2017, 4:32:34 PM9/25/17
to Particular Software
Helpful information to include
Product name:NServiceBus 
Version:6
Stacktrace:

Szymon Pobiega

unread,
Sep 26, 2017, 2:52:15 AM9/26/17
to particula...@googlegroups.com
Hi

The RegisterStartupTask method has an overload where you can provide a Func that gives you IBuilder and you return the startup task. 


The IBuilder can be used within the Func to resolve components you registered (or are registered by NServiceBus). What would be even better is if you didn't registered the components at all. You can instantiate them right in the Feature's Setup method and pass them immediately to a newly created startup task instance via constructor. You don't need to go through the container.

Hope it helps,
Szymon

--
You received this message because you are subscribed to the Google Groups "Particular Software" group.
To unsubscribe from this group and stop receiving emails from it, send an email to particularsoftware+unsub...@googlegroups.com.
To post to this group, send email to particularsoftware@googlegroups.com.
Visit this group at https://groups.google.com/group/particularsoftware.
To view this discussion on the web visit https://groups.google.com/d/msgid/particularsoftware/f77527e3-aa42-438a-9663-c720b306c574%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages