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>
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With