Nancy.Bootstrappers.Autofac icon indicating copy to clipboard operation
Nancy.Bootstrappers.Autofac copied to clipboard

Autofac 4.0

Open janus007 opened this issue 9 years ago • 13 comments

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.

janus007 avatar Aug 05 '15 21:08 janus007

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:

khellang avatar Aug 06 '15 08:08 khellang

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

  • Install-Package <<<< Autofac -Version 4.0.0-alpha2 -Pre
    • CategoryInfo : NotSpecified: (:) [Install-Package], Exception
    • FullyQualifiedErrorId : NuGetCmdletUnhandledException,NuGet.PackageManagement.PowerShellCmdlets.InstallPackageCommand

janus007 avatar Aug 06 '15 08:08 janus007

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.

khellang avatar Aug 06 '15 08:08 khellang

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

janus007 avatar Aug 06 '15 17:08 janus007

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

radams0x avatar Sep 02 '15 14:09 radams0x

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/

khellang avatar Sep 02 '15 14:09 khellang

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!!

radams0x avatar Sep 02 '15 14:09 radams0x

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.

khellang avatar Sep 02 '15 14:09 khellang

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!!

radams0x avatar Sep 02 '15 17:09 radams0x

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

khellang, I thank you very much for this :)

janus007 avatar Sep 02 '15 17:09 janus007

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

khellang avatar Sep 02 '15 17:09 khellang

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

khellang avatar Mar 21 '16 10:03 khellang

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

khellang avatar Mar 21 '16 10:03 khellang