Nie*_*ksa 5 c# autofac asp.net-core-2.0
我开始使用我的第一个.NET CORE 2.0项目,它将是一个webapi.到目前为止一直很好,直到我遇到试图配置Autofac的问题.我按照这里描述的说明操作.但不幸的是,我的StartUp中出现了构建错误.显然ContainerBuilder不包含定义Populate().也AutofacServiceProvider找不到类型.我在网上搜索了一段时间,试图找到2.0的正确文档.如果源的目标是1.0或2.0,它是分散的并且并不总是很清楚.不幸的是,所有选项最终都会出现构建错误,所以我认为我会坚持使用官方文档提供的实现.是否有可能Autofac不支持.NET Core 2.0中的这种启动形式.
供参考:
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>
Run Code Online (Sandbox Code Playgroud)
Gib*_*bon 13
相当简单的解决方案,Populate方法和AutofacServiceProvider都在你目前不使用的命名空间中.
你还需要"Autofac.Extensions.DependencyInjection"
然后这两个问题应该解决