unity container - Problems integrating NServiceBus with ServiceStack IRequiresRequestContext -


i looking integrate nservicebus existing servicestack web host. servicestack using built in funq ioc container. nservicebus has been configured (elsewhere in system) use unity ioc.

servicestack has feature whereby irequestcontext automatically injected when finds irequiresrequestcontext interface on class. nservicebus has similar feature message mutators, via imutateoutgoingtransportmessages interface.

the application multi-tenant application. single application, via api key, passes account code through nservicebus handler (indirectly via provider called on construction of handler's constructor injection using unity).

my problem arises in servicestack. using request filter drag api key out of request headers, look-up in database, , write ihttprequest.items collection:

apphost.requestfilters.add((req, res, requestdto) => {     var tenant = tenantrepository.getbyapikey(     req.items.add("accountcode", tenant.accountcode); } 

i have nservicebus transport message mutator, implements irequiresrequestcontext interface, , class located in same assembly servicestack services registered in apphost:

public class messageheadermutator : imutateoutgoingtransportmessages, ineedinitialization, irequiresrequestcontext {     #region irequiresrequestcontext members      public irequestcontext requestcontext { get; set; }      #endregion      #region imutateoutgoingtransportmessages members      public void mutateoutgoing(object[] messages, nservicebus.transportmessage transportmessage)     {         transportmessage.headers.add("accountcode", requestcontext.get<ihttprequest>().items["accountcode"].tostring());     }      #endregion      #region ineedinitialization members      public void init()     {         configure.instance.configurer.configurecomponent<messageheadermutator>(dependencylifecycle.instancepercall);     }      #endregion } 

however, requestcontext never injected, , null. theory 2 interface injections, injected via 2 separate frameworks, somehow clashing.

i have workaround, use servicestack hostcontext.items instead, per this discussion, concerned hostcontext not per request collection, might end writing data wrong tenant. workaround is:

// app host apphost.requestfilters.add((req, res, requestdto) => {     var accountcode = tenantrepository.getbyapikey(     hostcontext.instance.items.add("accountcode", client.accountcode); }   // message mutator public class messageheadermutator : imutateoutgoingtransportmessages, ineedinitialization {     #region imutateoutgoingtransportmessages members      public void mutateoutgoing(object[] messages, nservicebus.transportmessage transportmessage)     {         var accountcode = hostcontext.instance.items["accountcode"].tostring();         transportmessage.headers.add("accountcode", accountcode);     }      #endregion      #region ineedinitialization members      public void init()     {         configure.instance.configurer.configurecomponent<messageheadermutator>(dependencylifecycle.instancepercall);     }      #endregion } 

my question therefore twofold:

  1. the first is, why irequiresrequestcontext not correctly inject requestcontext message mutator, or there way inject requestcontext manually?
  2. is use of hostcontext safe assume per request?
  3. bonus question: use of 2 separate ioc containers in same project (unity in nservicebus , funq in servicestack) bad idea? smarter servicestack use same unity ioc container nservicebus?

p.s. nservicebus 4 (beta @ time of writing).

it precisely because you're using 2 different containers di doesn't work out of box objects registered in different containers.

you don't need standardize on single container (though save dealing these sorts of issues time).

what can keep working both containers tell nservicebus container how resolve irequiresrequestcontext this:

public class requestcontextbootstrapper : ineedinitialization {    public void init()    {       configure.component<irequiresrequestcontext>( /* object servicestack */ );    } } 

Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -