当前位置:   article > 正文

.Net6获取Autofac注入的服务_autofac 获取service

autofac 获取service

Program.cs 配置:

  1. using Autofac;
  2. using Autofac.Extensions.DependencyInjection;
  3. using System.Reflection;
  4. using WebApplication3;
  5. var builder = WebApplication.CreateBuilder(args);
  6. // 1.将默认ServiceProviderFactory指定为AutofacServiceProviderFactory
  7. builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory());
  8. builder.Host.ConfigureContainer<ContainerBuilder>(builder =>
  9. {
  10. // 2.自动注册
  11. Assembly assembly = Assembly.Load("Jasper.Application.Base");
  12. builder.RegisterAssemblyTypes(assembly).AsImplementedInterfaces().InstancePerDependency();
  13. });
  14. // Add services to the container.
  15. builder.Services.AddControllers();
  16. // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
  17. builder.Services.AddEndpointsApiExplorer();
  18. builder.Services.AddSwaggerGen();
  19. var app = builder.Build();
  20. // 3.获取Autofac注入的实例
  21. IocManager.Instance.Container = app.UseHostFiltering().ApplicationServices.GetAutofacRoot();
  22. // Configure the HTTP request pipeline.
  23. if (app.Environment.IsDevelopment())
  24. {
  25. app.UseSwagger();
  26. app.UseSwaggerUI();
  27. }
  28. app.UseAuthorization();
  29. app.MapControllers();
  30. app.Run();

Autofac帮助类:

  1. using Autofac;
  2. using Autofac.Core;
  3. namespace WebApplication3
  4. {
  5. /// <summary>
  6. /// Autofac帮助类
  7. /// </summary>
  8. public class IocManager
  9. {
  10. public static IocManager Instance = new IocManager();
  11. public ILifetimeScope Container { get; set; }
  12. public T GetService<T>()
  13. {
  14. return ResolutionExtensions.Resolve<T>((IComponentContext)(object)this.Container);
  15. }
  16. public T GetService<T>(string serviceKey)
  17. {
  18. return ResolutionExtensions.ResolveKeyed<T>((IComponentContext)(object)this.Container, (object)serviceKey);
  19. }
  20. public T GetService<T>(string serviceKey, params Parameter[] parameters)
  21. {
  22. return ResolutionExtensions.ResolveKeyed<T>((IComponentContext)(object)this.Container, (object)serviceKey, parameters);
  23. }
  24. public object GetService(Type serviceType)
  25. {
  26. return ResolutionExtensions.Resolve((IComponentContext)(object)this.Container, serviceType);
  27. }
  28. public object GetService(string serviceKey, Type serviceType)
  29. {
  30. return ResolutionExtensions.ResolveKeyed((IComponentContext)(object)this.Container, (object)serviceKey, serviceType);
  31. }
  32. public bool IsRegistered<T>()
  33. {
  34. return ResolutionExtensions.IsRegistered<T>((IComponentContext)(object)this.Container);
  35. }
  36. public bool IsRegistered<T>(string serviceKey)
  37. {
  38. return ResolutionExtensions.IsRegisteredWithKey<T>((IComponentContext)(object)this.Container, (object)serviceKey);
  39. }
  40. public bool IsRegistered(Type serviceType)
  41. {
  42. return ResolutionExtensions.IsRegistered((IComponentContext)(object)this.Container, serviceType);
  43. }
  44. public bool IsRegisteredWithKey(string serviceKey, Type serviceType)
  45. {
  46. return ResolutionExtensions.IsRegisteredWithKey((IComponentContext)(object)this.Container, (object)serviceKey, serviceType);
  47. }
  48. }
  49. }

使用:

var testIoc = IocManager.Instance.GetService<T>();

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/141294
推荐阅读
相关标签
  

闽ICP备14008679号