Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trouble configuring Autofac in .NET Core 2.0

I started of with my first .NET CORE 2.0 project which would be a webapi. So far so good until I ran into the issue trying to configure Autofac. I followed the instructions described here. But unfortunately I get build errors in my StartUp. Apparently ContainerBuilder does not contain the definition of Populate(). Also the type AutofacServiceProvider could not be found. I've search the net for some time, trying to find the correct documentation for 2.0. It's scattered and not always clear if the source is targeting 1.0 or 2.0. Unfortunately all options end up with build error so I thought I'd stick with the implementation provided by the official documentation. Is it possible that Autofac does not support this form of initiation in .NET Core 2.0.

FYI:

using Autofac;
using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace TEST.API
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();

            var containerBuilder = new ContainerBuilder();
            containerBuilder.RegisterModule<ServiceModule>();
            containerBuilder.Populate(services);
            var container = containerBuilder.Build();
            return new AutofacServiceProvider(container);
        }

        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseMvc();
        }
    }
}


<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFramework>netcoreapp2.0</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <Folder Include="wwwroot\" />
  </ItemGroup>
  <ItemGroup>
    <ProjectReference Include="..\Model\Recipy.Model.csproj" />
    <ProjectReference Include="..\Service\Recipy.Service.csproj" />  
  </ItemGroup>
  <ItemGroup>
    <PackageReference Include="Autofac" Version="4.6.2" />
    <PackageReference Include="Autofac.Extensions.DependencyInjection" Version="4.2.0" />
    <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
  </ItemGroup>
  <ItemGroup>
    <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.0" />
  </ItemGroup>
</Project>
like image 567
Nieksa Avatar asked Dec 05 '17 17:12

Nieksa


1 Answers

Fairly easy solution to this, both the Populate method and the AutofacServiceProvider are in a namespace you arent using at the moment.

You also need "Autofac.Extensions.DependencyInjection"

and then those two issues should resolve

like image 61
Gibbon Avatar answered Oct 26 '22 08:10

Gibbon