NancyFx / Nancy.Bootstrappers.Autofac

Nancy bootstrapper for the Autofac container
MIT License
35 stars 36 forks source link

Autofac 4.0 #25

Open janus007 opened 9 years ago

janus007 commented 9 years ago

Hello

I was trying to get AutofacNancyBootstrapper to work with Autofac 4.0 because I wanted to use .NET 4.5.2 in Visual Studio 2015, but with no luck.

It seems that AutofacNancyBootstrapper utilizes ILifetimeScope and that appears not to work with Autofac 4.0

So my question... when will I be able to use Autofac 4.0?

Thank you for a great product.

khellang commented 9 years ago

Uuuh... ILifetimeScope is kinda the core interface in Autofac. It would be weird if they dropped it in 4.0. I even think I've seen the interface provide scoping to the ASP.NET 5 dependency injection stuff.

I'll check and come back to you, but we probably wouldn't update anything to require 4.0 until it's out of beta, at least. Maybe @tillig or @alexmg could provide some insight here?

BTW, why can't you use .NET 4.5.2 with an older version? :stuck_out_tongue_winking_eye:

janus007 commented 9 years ago

Hi khellang That was quick reply :) They didn't drop it, I can see that my previous post could indicate that.

The case is that I'm trying to use ASP.NET 4.5.2 and AutofacRegistration.Populate or builder.Populate, either way this extension method exists in Autofac 4.* and as soon I reference that, the AutofacNancyBootstrapper complains about the Autofac version.

Normally I would just do an assembly-binding, but that is not possible in vNext, it is being said that assembly-version is carried out by Roslyn, but apparently this is not working. Don't now why, but I guess it has something to do with the target framework that Nancy is built in, at the moment 4.0.

In my desperation for a temporary solution, I cloned Nancy.Bootstrappers.Autofac and changed Autofac to version Autofac 4.0.0-alpha2 , but I gave up :)

Install-Package : Failed to add reference to 'System.Runtime'. Please make sure that it is in the Global Assembly Cache. At line:1 char:16

khellang commented 9 years ago

Right, so this has nothing to do with .NET 4.5.2, but rather that you're trying to use this in a DNX project? We don't support DNX yet. We have a branch going in the main repo to get Nancy running on it. In fact I think it runs on DNX451, but not on DNXCORE50, right now. Before we ship that, we'll make sure to have a version of the different bootstrappers that works with it.

janus007 commented 9 years ago

Amazing... I will look forward to compatibility with 451.

radams0x commented 9 years ago

Hello, I'm having the same issue trying to use dnx451. I'll keep an eye out for your updated bootstrapper. Thanks!

khellang commented 9 years ago

If you really want to use Nancy with Autofac on DNX451, you can simply copy the bootstrapper over as source, temporarily. I've created a sample repo over at https://github.com/khellang/Nancy.Autofac.Dnx where I did this. The sample runs nicely using Nancy on DNX451 with Autofac in harmony with IServiceCollection and co :grin:

I've even demonstrated it by using DNX's logging infrastructure inside a Nancy module. Here's the output:

$ dnx . web
> info    : [Microsoft.Net.Http.Server.WebListener] Start
> info    : [Microsoft.Net.Http.Server.WebListener] Listening on prefix: http://localhost:5000/
> Started
> info    : [WebApplication1.HelloWorldModule] Hello World!
> info    : [Microsoft.Net.Http.Server.WebListener] Dispose
> info    : [Microsoft.Net.Http.Server.WebListener] Stop listening on prefix: http://localhost:5000/
radams0x commented 9 years ago

Wow, thank you so much for this! I was actually just in the process of doing that, but but this helped me as I couldn't get it to compile and hadn't noticed the need for the ComponentContextExtensions.cs. I'll give it a whirl and post the results. Thanks!!

khellang commented 9 years ago

No problem :+1: BTW, you don't need Autofac 4.0 to run on DNX. You only need it if you want to use Autofac.Framework.DependencyInjection, which contains the Populate method to populate the ContainerBuilder using an IServiceCollection. If you don't need services from DNX, you can get by with simply using the Nancy.Bootstrappers.Autofac package.

radams0x commented 9 years ago

HI khellang,

I got it to work as you describe, use the code you posted. at https://github.com/khellang/Nancy.Autofac.Dnx

I am using Autofac.Framework.DependencyInjection, which contains the Populate method to populate the ContainerBuilder using an IServiceCollection.

Thank you so much for everything!!

janus007 commented 9 years ago

Haven't tested it, but I believe radams0x is right on.

khellang, I thank you very much for this :)

khellang commented 9 years ago

Glad to hear :+1: I'll leave this issue open until we get a permanent solution :smile:

khellang commented 8 years ago

@norgie What are you talking about? You shouldn't have to implement anything. All those methods you mention are implemented by the AutofacNancyBootstrapper.

khellang commented 8 years ago

Heh. What version of R# are you using? Some EAP?