RaventBootstrapper configuration in NServicebus

2019-09-10 06:00发布

问题:

I'm trying to make the following NServiceBus Pluralsight training code sample by Andreas Öhlund to work.

public class RavenBootstrapper : INeedInitialization
{
    public void Init()
    {
        Configure.Instance.Configurer.ConfigureComponent<IDocumentStore>(
            () =>
            {
                var store = new DocumentStore
                            {
                                Url = "http://localhost:8080"
                            };

                store.Initialize();
                store.JsonRequestFactory.DisableRequestCompression = true;
                return store;
            }
            , DependencyLifecycle.SingleInstance);

        Configure.Instance.Configurer.ConfigureComponent<IDocumentSession>(
            () =>
            {

                return Configure.Instance.Builder.Build<IDocumentStore>()
                    .OpenSession();
            },
            DependencyLifecycle.InstancePerUnitOfWork);

        Configure.Instance.Configurer.ConfigureComponent<RavenUnitOfWork>(DependencyLifecycle.InstancePerUnitOfWork);

    }
}

There were multiple compile errors about obsolete code and I was able to correct most of them but got stock on Configure.Instance.Builder.Build... Here's what I have so far:

 public class RavenBootstrapper : INeedInitialization
{

        configuration.RegisterComponents(c => c.ConfigureComponent<IDocumentStore>(
           () =>
           {
             var  store = new DocumentStore
                         {
                             Url = "http://localhost:8080"
                         };

               store.Initialize();
               store.JsonRequestFactory.DisableRequestCompression = true;
               return store;
           }
           , DependencyLifecycle.SingleInstance));


        configuration.RegisterComponents(c => 
            c.ConfigureComponent(builder => builder.Build<IDocumentStore>().OpenSession(),DependencyLifecycle.InstancePerUnitOfWork));

        configuration.RegisterComponents(c => c.ConfigureComponent<RavenUnitOfWork>(DependencyLifecycle.InstancePerUnitOfWork));
}
  1. What is a new equivalent of Builder.Build?
  2. Does the rest look good?
  3. Would be nice to know where specifically I could find an answer to this in NserviceBus documentation.

回答1:

There is an overload to Configure.Component that accepts a Func<IBuilder,TComponent>

Using this you can change your code to:

 configuration.RegisterComponents(c => c.ConfigureComponent<IDocumentStore>(
           () =>
           {
               var store = new DocumentStore
                           {
                               Url = "http://localhost:8080"
                           };

               store.Initialize();
               store.JsonRequestFactory.DisableRequestCompression = true;
               return store;
           }
           , DependencyLifecycle.SingleInstance));

        configuration.RegisterComponents(c => c.ConfigureComponent<IDocumentSession>(
            builder =>
            {

                return builder.Build<IDocumentStore>()
                    .OpenSession();
            },
            DependencyLifecycle.InstancePerUnitOfWork));


回答2:

Mauro Servienti from Particular Software just posted a webinar that you may find helpful. https://particular-1.wistia.com/medias/q8tdr6mnzz