Configure StructureMap with latest version of NServiceBus

165 views
Skip to first unread message

Riccardo Di Nuzzo

unread,
Aug 26, 2015, 7:10:41 AM8/26/15
to Particular Software
Helpful information to include
Product name: StructureMap 3.1.4 - NServiceBus 5.2.3 - NServiceBus.StructureMap 5.0.1

Hello,

I'm trying to configure dependency injection in a web application Asp.Net MVC 5. In a bootstra method we map all dependencies using the sintax...
For<ISomething>>().Use<Something>();

How I can configure NServiceBus di in order to inject the bus in my Controllers?

For example, how can I convert the following syntax that was working with NSB ver. 4?

ForSingletonOf<IBus>().Use(
                NServiceBus.Configure.With(AllAssemblies.Except("DevExpress.XtraGauges.v14.1.Presets.dll"))
                    .DefaultBuilder()
                    .DefiningEventsAs(
                        t =>
                            t.Namespace != null && t.Namespace.StartsWith("Mynamespace") && t.Namespace.EndsWith("Events"))
                    .UseNHibernateGatewayPersister()
                    .UseNHibernateGatewayDeduplication()
                    .UseNHibernateSubscriptionPersister()
                    .UseNHibernateTimeoutPersister()
                    .UseNHibernateSagaPersister()
                    .UseTransport<SqlServer>()
                    .UnicastBus()
                    .CreateBus()
                    .Start()
                );

Thanks
Riccardo

Andreas Öhlund

unread,
Aug 26, 2015, 7:39:33 AM8/26/15
to particula...@googlegroups.com
Hi!

You should be able to modify this sample for AutoFac


and pass in an explicit StructureMap container like described here:


Let me know if this helps!

Cheers,

Andreas



--
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 particularsoftw...@googlegroups.com.
To post to this group, send email to particula...@googlegroups.com.
Visit this group at http://groups.google.com/group/particularsoftware.
To view this discussion on the web visit https://groups.google.com/d/msgid/particularsoftware/b6eabe68-e869-4396-b4e0-b491f5b165d4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Riccardo Di Nuzzo

unread,
Aug 26, 2015, 8:14:12 AM8/26/15
to Particular Software
Hi,

I end up using the following configuration. 
Not sure if I need to set .DisableCallbackReceiver() as I found this in an example on internet blog
I'm not sure if the ForSingletonOf<IBus> is required and/or is a good practice for a web app.

var configuration = new BusConfiguration();
            configuration.UseContainer<StructureMapBuilder>(b => b.ExistingContainer(ObjectFactory.Container));
            configuration.UseTransport<SqlServerTransport>(); //.DisableCallbackReceiver();
            configuration.UsePersistence<NHibernatePersistence>();
            var listOfAssemblies = AllAssemblies.Except("DevExpress.");
            configuration.AssembliesToScan(listOfAssemblies);
            configuration.RijndaelEncryptionService();
            configuration.Conventions()
                .DefiningCommandsAs(t => t.Namespace != null && t.Namespace.StartsWith("yourns") && t.Namespace.EndsWith("Commands"))
                .DefiningEventsAs(t => t.Namespace != null && t.Namespace.StartsWith("yourns") && t.Namespace.EndsWith("Events"))
                .DefiningMessagesAs(t => t.Namespace != null && t.Namespace.StartsWith("yourns") && t.Namespace.EndsWith("RequestResponse"))
                .DefiningEncryptedPropertiesAs(p => p.Name.StartsWith("Encrypted"));

            ForSingletonOf<IBus>().Use(Bus.Create(configuration));

Cheers
Riccardo

Andreas Öhlund

unread,
Aug 26, 2015, 9:48:32 AM8/26/15
to particula...@googlegroups.com
>ForSingletonOf<IBus>().Use(Bus.Create(configuration));

NServiceBus will automatically register `IBus` into the container so you should be able to skip that line? (like shown here http://docs.particular.net/samples/web/asp-mvc-injecting-bus/#injection-into-the-controller)


>Not sure if I need to set .DisableCallbackReceiver()

You can safely call this to avoid an extra "queue" to be created as long as you don't make use of callbacks


Cheers,

Andreas

--
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 particularsoftw...@googlegroups.com.
To post to this group, send email to particula...@googlegroups.com.
Visit this group at http://groups.google.com/group/particularsoftware.
Message has been deleted

Andreas Öhlund

unread,
Aug 27, 2015, 6:19:11 AM8/27/15
to particula...@googlegroups.com
Hi Riccardo!

Really sorry for your poor experience, we're contantly trying to improve both our api and our doco. Please accept my appologies.

Would you be able to upload your entire project somewhere so that we can have a look at it?

>Plus, each participant in the message flow potentially have to use different settings and all these settings follow a syntax that changed a lot between different NSB releases

This is unfortunate but since we're constantly trying to improve the api we have to change it. We're trying to mitigate the negative effects with better obsolete messages, we actually leaves obsoletes in to better guide users upgrading, and also better upgrade guides.


I know we've been far from good enough in this area in the past and any ideas on how we can improve is very welcome!

Thanks for your honest feedback, much appreciated!

Andreas

On Wed, Aug 26, 2015 at 5:21 PM, Riccardo Di Nuzzo <ricc...@dinuzzo.it> wrote:
>>ForSingletonOf<IBus>().Use(Bus.Create(configuration));
NServiceBus will automatically register `IBus` into the container so you should be able to skip that line?

No, it doesn't work without that line. I have no idea why.

As a general consideration...
I wasted 2 days looking for some documentation but I can't find anything that show this way. I already read all these documents from your official guides but they show a simple configuration in global asax. We set DI in a separate file and we use For<ISomething>().Use<Something>(); 
Probably there are other ways but the fact is that the configuration of NSB is not a simple task. Plus, each participant in the message flow potentially have to use different settings and all these settings follow a syntax that changed a lot between different NSB releases. If you try to find some example, you can see 98% of results using the version 4 of NSB. 
Is it going to be simpler with next version?

Cheers
Riccardo
Reply all
Reply to author
Forward
0 new messages