ASP.Net Core 3.1 With Autofac ConfigureServices returning an System.IServiceProvider isn't s
2021-01-19 21:14
标签:page string value file The names adt nta environ ASP.Net Core 3.1 With Autofac ConfigureServices returning an System.IServiceProvider isn‘t supported. Autofac在ASP.Net Core3.0以后,集成方式有所调整。在ASP.Net Core2中我们一般是把 完整代码可以参考我之前的开源项目:LJD.APP:https://github.com/jellydong/LJDAPP CustomAutofacModule是把Autofac的配置单独写,不然都放在Startup中有点臃肿。 我们把这段代码Copy到ASP.Net Core3.1的项目中启动: 异常: 就是说,我们不能再这样修改返回值类型, 官网解释:您不能再 那我们按照官方文档的方法修改一下。 效果图 创建完成后我们先创建文件夹 在 然后创建两个 创建 我们先用 当然,需要安装Autofac的包: 前序工作都完成了,我们来改最重要的,首先修改我们的 然后我们修改 1、增加属性 2、增加方法 3、给属性赋值(可选) 我们修改下 运行,断点看下结果是否正确 修改下实现类,也是正确的~ 其实很多官方文档都讲述了,因为自己近期打算把原来的项目LJD.APP:https://github.com/jellydong/LJDAPP升级到.Net Core 3.1上,当然是一个简化版本https://github.com/jellydong/Jelly.Simple。 所以不要怕英文,翻译插件那么好用,基本都能看懂的 参考文档:https://autofaccn.readthedocs.io/en/latest/integration/aspnetcore.html#asp-net-core-3-0-and-generic-hosting 当然 ASP.Net Core 3.1 With Autofac ConfigureServices returning an System.IServiceProvider isn't supported. 标签:page string value file The names adt nta environ 原文地址:https://www.cnblogs.com/jellydong/p/13331450.html前言
Startup
的ConfigureServices
方法返回值类型改为IServiceProvider
。我们可以先看一下部分代码: public IServiceProvider ConfigureServices(IServiceCollection services)
{
services.AddMvc();
//xxxxxx你的其他代码 省略...........
//用Autofac来替换IOC容器 返回值由 void 修改为 IServiceProvider
var containerBuilder = new ContainerBuilder();
containerBuilder.RegisterModule
System.NotSupportedException:“ConfigureServices returning an System.IServiceProvider isn‘t supported.”
IServiceProvider
从中返回ConfigureServices
,也不能将服务提供者工厂添加到服务集合中。建立ASP.Net Core3.1 Web项目
Services
Services
,用来测试使用。Services
文件夹下创建Iservice
namespace DotNetCore3AutoFac.Services
{
public interface IService
{
string SayHi();
}
}
Iservice
的实现类OneService
、TwoService
namespace DotNetCore3AutoFac.Services
{
public class OneService:IService
{
public string SayHi()
{
return "One Say Hi";
}
}
}
namespace DotNetCore3AutoFac.Services
{
public class TwoService:IService
{
public string SayHi()
{
return "Two Say Hi";
}
}
}
Autofac
Autofac
文件夹,在该文件夹下创建CustomAutofacModule
using Autofac;
using DotNetCore3AutoFac.Services;
namespace DotNetCore3AutoFac.Autofac
{
public class CustomAutofacModule : Module
{
///
OneService
去实现,当然,正常这里会有很多很多的代码~修改
Startup
和Program
Autofac
&Autofac.Extensions.DependencyInjection
Program
,增加UseServiceProviderFactory(new AutofacServiceProviderFactory())
namespace DotNetCore3AutoFac
{
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
//改用Autofac来实现依赖注入
.UseServiceProviderFactory(new AutofacServiceProviderFactory())
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup
Startup
AutofacContainer
ConfigureContainer
它在ConfigureServices
之后运行using Autofac;
using Autofac.Extensions.DependencyInjection;
using DotNetCore3AutoFac.Autofac;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace DotNetCore3AutoFac
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
//autofac 新增
public ILifetimeScope AutofacContainer { get; private set; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
}
//autofac 新增
public void ConfigureContainer(ContainerBuilder builder)
{
// 直接用Autofac注册我们自定义的
builder.RegisterModule(new CustomAutofacModule());
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
//autofac 新增 可选
this.AutofacContainer = app.ApplicationServices.GetAutofacRoot();
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
}
}
}
验证
HomeController
总结
Autofac
的应用远不止这些,还有一个比原来好的就是可以实现AOP~
下一篇:前端开发常用css动画代码
文章标题:ASP.Net Core 3.1 With Autofac ConfigureServices returning an System.IServiceProvider isn't s
文章链接:http://soscw.com/index.php/essay/44238.html