elliotritchie / NES

.NET Event Sourcing
http://getnes.net
Other
129 stars 32 forks source link

NES and WebApi SelfHosted in NServiceBus Endpoint #24

Closed brunobertechini closed 10 years ago

brunobertechini commented 10 years ago

Hi there,

I have one requirement to use a WebApi self hosted within NServiceBus. This is working fine. But I cannot manage to work NES inside my "POST" endpoint.

Im sure its something related to DependencyInjection of NServiceBus components that IRepository needs to use.

How can I apply the SAME DI from NSB to my self hosted WebApi ? Im using Autofac and setting mycontainer to the Bus. But the .NES() extension also add more instances to NSB container.

Should I get the NSB container and apply it to webapi ?

Bruno

marinkobabic commented 10 years ago

Hi Bruno

If have catched correctly your question it's the following approach:

You would like to configure the webapi as well to use your container instance. Is this package not doing what you want?

https://www.nuget.org/packages/Autofac.WebApi

You need to consider, that NServiceBus creates subcontiners for messages which have a different lifetime.

Thanks Marinko

brunobertechini commented 10 years ago

Hi there,

You got it right...

The thing is: Im already using Autofac.Webapi and the IRepository is being injected just fine in my ApiController.

The problem is: Since in my ApiController I don’t have a Bus context, the UnitOfWork.Current is null and also the CommandContext (CurrentMessageBeingHandled)

I only need to retrieve the Aggregate from the event store and do few checks….

How can I do that ?

Bruno

From: Marinko [mailto:notifications@github.com] Sent: sexta-feira, 21 de março de 2014 13:09 To: elliotritchie/NES Cc: Bruno Bertechini Subject: Re: [NES] NES and WebApi SelfHosted in NServiceBus Endpoint (#24)

Hi Bruno

If have catched correctly your answer it's the following approach:

You would like to configure the webapi as well to use your container instance. Is this package not doing what you want?

https://www.nuget.org/packages/Autofac.WebApi

Thanks Marinko

— Reply to this email directly or view it on GitHub https://github.com/elliotritchie/NES/issues/24#issuecomment-38292239 . https://github.com/notifications/beacon/5166528__eyJzY29wZSI6Ik5ld3NpZXM6QmVhY29uIiwiZXhwaXJlcyI6MTcxMTAzNzM1MywiZGF0YSI6eyJpZCI6MjgyMTI1OTB9fQ==--59be54ad8802f4ce00cae058eb94cf89108f236f.gif

marinkobabic commented 10 years ago

Hi Bruno

You have received a request on you api controller. Based on the request you would like to do some stuff and of course return a response back to the caller. I think you have two options:

Option 1: Create a command based on the message and do a this.Bus.SendLocal(yourcommand). Bus supports request/response, but the response can only be an integer. You could use the message header to return some information more as response. I did it once, but faced some problems http://stackoverflow.com/questions/14793215/web-api-currentmessagecontext-null when working inside of webapi. I have found a workaround, so that it would work (let me know if you are interested in the workaround). In this case the CurrentMessageBeingHandled and the UnitOfWork.Current would be set so all should work properly.

Option 2: You could use the static DI class directly https://github.com/elliotritchie/NES/blob/master/src/NES/DI.cs and resolve the IEventSourceMapper and call Get directly on the class to get your aggregate. You are just going to read some stuff for your checks so I don't see any problems if you are not using UnitOfWork here. There are no modifications you are going to make.

Does this help you?

Thanks, Marinko