Monday 15 April 2013

c# - Using Ninject With EasyNetQ/RabbitMQ Message Handlers -



c# - Using Ninject With EasyNetQ/RabbitMQ Message Handlers -

i'm attempting utilize easynetq ninject log messages.

i've managed setup ninject easynetq di (i think), when message comes handler without parameterless constructor (e.g. need repository bound in there), doesn't resolve. or atleast believe that's issue pretty generic error on console.

i tell easynetq utilize ninject :

rabbithutch.setcontainerfactory(() => new ninjectadapter(container));

i think need set up. ninject adapter 1 easynetq.

my handler looks next :

public class profiledeactivatedupdatehandler : iconsume<profiledeactivatedupdate> { private readonly iprofilerepository _profilerepository; public profiledeactivatedupdatehandler(iprofilerepository profilerepository) { _profilerepository = profilerepository; } public void consume(profiledeactivatedupdate message) { //do stuff. } }

if add together parameterless constructor, , instead setup ninject available through servicelocator (ugh), works. handler called fine, , can find repository via servicelocator, know atleast ninject knows repository.

the error pops when tries handle message is.

system.aggregateexception: 1 or more errors occurred. ---> system.exception: e xception of type 'system.exception' thrown. @ easynetq.reflectionhelpers.defaultfactories`1.get() @ easynetq.reflectionhelpers.createinstance[t]() @ easynetq.autosubscribe.defaultautosubscribermessagedispatcher.dispatch[tme ssage,tconsumer](tmessage message) @ easynetq.rabbitbus.<>c__displayclass6`1.<subscribe>b__5(t msg) --- end of inner exception stack trace --- ---> (inner exception #0) system.exception: exception of type 'system.exception' thrown. @ easynetq.reflectionhelpers.defaultfactories`1.get() @ easynetq.reflectionhelpers.createinstance[t]() @ easynetq.autosubscribe.defaultautosubscribermessagedispatcher.dispatch[tme ssage,tconsumer](tmessage message) @ easynetq.rabbitbus.<>c__displayclass6`1.<subscribe>b__5(t msg)<---

so have resolved this. apparently when easynetq instantiates handler, doesn't utilize specified di framework (boo!). have specify "messagedispatcher" implementation separately. awesome! oh... but... there implementation autofac, not ninject (boo! x2).

so, pull request here code implement event dispatcher ninject here : https://github.com/mikehadlow/easynetq/pull/309

for now, can re-create , paste class project. in ninjectmodule or wherever setting bindings, can next :

//bind message dispatcher ninject event message dispatcher ninjectmessagedispatcher messagedispatcher = new ninjectmessagedispatcher(kernel); bind<iautosubscribermessagedispatcher>().toconstant(messagedispatcher);

then wherever setting subscribers can next (note auto subscribing using iconsume interface of easynetq.

var subscriber = new autosubscriber(_servicebus, "profileservices"); subscriber.autosubscribermessagedispatcher = _dispatcher; subscriber.subscribe(assembly.getexecutingassembly());

the of import part setting messagedispatcher manually instance of ninject dispatcher. how want accomplish you.

i think in future, easynetq needs automatically. if set mill utilize ninject, chances handler want using ninject too.

oh! , can modify above code utilize di of choice. needs done di in utilize easynetq (i think), not ninject.

c# .net rabbitmq ninject easynetq

No comments:

Post a Comment