INeedInitialization or IConfigureThisEndpoint, which one to use?

1.2k Views Asked by At

I know we can implement IWantCustomInitialization to configure NServicebus with other IoC continers lets say Castle.Windsor for instance, but I found this is obsoleted. in new version alternatively I used INeedInitialization, but it also didn't solve my issue because the container was specified before "INeedInitialization.Customize" invocation, So at last I implemented IConfigureThisEndpoint as my final solution.

To sum up my question; when to use "INeedInitialization" and "IConfigureThisEndpoint"?

Best Regards

2

There are 2 best solutions below

1
David Savage On BEST ANSWER

This has been changed a bit over time and v5 of NServiceBus introduce a different API. For context you can read through the following bug comments. What the intent is:

  • IConfigureThisEndpoint - for configuring the Endpoint. Can only have 1 per instance.
  • INeedInitialization - for configuring a component used by the Endpoint. Can have multiple implementations of this per instance.

An example of when to use IConfigureThisEndpoint (see full example)

public class EndpointConfig : IConfigureThisEndpoint, AsA_Server, UsingTransport<MsmqTransport>
    {
        public void Customize(BusConfiguration configuration)
        {
            configuration.UsePersistence<NHibernatePersistence>();               
        }
    }

An example of when to use INeedInitialization (see full example)

class ResponderInstaller : INeedInitialization
    {
        public void Init()
        {
            Configure.Instance.Configurer.ConfigureComponent<CustomHttpResponder>(DependencyLifecycle.InstancePerCall);
        }
    }
0
David Boike On

Both give you access to do basically the same things. The big difference is that IConfigureThisEndpoint is once per endpoint, so you use that to configure truly endpoint-specific things. INeedInitialization can have many implementations, and all of them get executed. So you can package up multiple INeedInitialization behaviors in a Conventions assembly and use them throughout your solution to carry out tasks common to multiple (or all) endpoints.