C# 未加载带有.NetCore API控制器的Autofac 4

C# 未加载带有.NetCore API控制器的Autofac 4,c#,dependency-injection,asp.net-core,autofac,C#,Dependency Injection,Asp.net Core,Autofac,我有一个实例,当我使用Autofac时,我的控制器从未被实例化。我想我的配置有问题,但我无法解决。我的解决方案中有两个项目。1) API 2)核心所有模型、存储库和服务都位于核心中。API中只有控制器 如果导航到默认值或值控制器,它们工作正常。如果我从我的MemberController中删除构造函数,它将工作,但我会在服务上获得NULL引用。如果我重新添加构造函数,则永远不会命中MemberController从不加载(构造函数和get方法中的断点) 该服务在实例化时需要一个数据模型。在下面的

我有一个实例,当我使用
Autofac
时,我的控制器从未被实例化。我想我的配置有问题,但我无法解决。我的解决方案中有两个项目。1) API 2)核心所有模型、存储库和服务都位于核心中。API中只有控制器

如果导航到默认值或值控制器,它们工作正常。如果我从我的
MemberController
中删除构造函数,它将工作,但我会在服务上获得
NULL
引用。如果我重新添加构造函数,则永远不会命中
MemberController
从不加载(构造函数和get方法中的断点)

该服务在实例化时需要一个数据模型。在下面的实例中,
MemberController
MemberService
作为
IService
。 我相信我已经在我的
AutofacModule
中注册了所有内容,但它似乎不起作用,因为在
MemberController
中从未遇到构造函数

如有任何想法/帮助,将不胜感激

Startup.cs

public class Startup
{
    public Startup(IHostingEnvironment env)
    {
        var builder = new ConfigurationBuilder()
            .SetBasePath(env.ContentRootPath)
            .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
            .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
            .AddEnvironmentVariables();
        Configuration = builder.Build();
    }

    public IContainer ApplicationContainer { get; private set; }
    public IConfigurationRoot Configuration { get; private set; }


    // This method gets called by the runtime. Use this method to add services to the container.
    public IServiceProvider ConfigureServices(IServiceCollection services)
    {

        // Add service and create Policy with options
        services.AddCors(o => o.AddPolicy("CorsPolicy", p =>
        {
            p.AllowAnyOrigin()
             .AllowAnyMethod()
             .AllowAnyHeader()
             .AllowCredentials();
        }));    

        // Add framework services.
        services.AddMvc();

        var builder = new ContainerBuilder();
        var connectionString = Configuration.GetValue<string>("DBConnection:ConnectionString");

        builder.RegisterModule(new AutofacModule(connectionString));

        builder.Populate(services);
        ApplicationContainer = builder.Build();
        return new AutofacServiceProvider(ApplicationContainer);

    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IApplicationLifetime appLifetime)
    {
        loggerFactory.AddConsole(Configuration.GetSection("Logging"));
        loggerFactory.AddDebug();
        app.UseCors("CorsPolicy");
        app.UseMvc();

        appLifetime.ApplicationStopped.Register(() => this.ApplicationContainer.Dispose());
    }
}
成员控制器

 [Route("api/[controller]")]
public class MemberController : Controller
{

    private readonly IService<MemberDM> _memberService;

    public MemberController(IService<MemberDM> service)
    {
        _memberService = service;
    }

    // GET: api/values
    [HttpGet]
    public IEnumerable<string> Get()
    {
        return new string[] { "value1", "value2" };
    }

    // GET api/values/5
    [HttpGet("{id}")]
    public async Task<IActionResult> Get(int id)
    {
        var result = await _memberService.Get(id);
        return View(result);
    }}
[路由(“api/[控制器]”)]
公共类成员控制器:控制器
{
私人只读服务器服务;
公共成员控制器(iSeries服务)
{
_会员服务=服务;
}
//获取:api/值
[HttpGet]
公共IEnumerable Get()
{
返回新字符串[]{“value1”,“value2”};
}
//获取api/values/5
[HttpGet(“{id}”)]
公共异步任务Get(int-id)
{
var result=await\u memberService.Get(id);
返回视图(结果);
}}
假设如下:

  • IService
    IDataModel
    实现了
    IServiceAssembly
  • 核心项目中以“DM”或“Service”结尾的所有接口都有相应的实现
那么,在API项目中有一个DI注册语句就足够了

// Register DataModel as IDataModel 
// Register Service Class as IService
builder.RegisterAssemblyTypes(typeof(IServiceAssembly).GetTypeInfo().Assembly)
    .Where(t => t.Name.EndsWith("DM") || t.Name.EndsWith("Service"))
    .AsImplementedInterfaces();
假设:

  • IService
    IDataModel
    实现了
    IServiceAssembly
  • 核心项目中以“DM”或“Service”结尾的所有接口都有相应的实现
那么,在API项目中有一个DI注册语句就足够了

// Register DataModel as IDataModel 
// Register Service Class as IService
builder.RegisterAssemblyTypes(typeof(IServiceAssembly).GetTypeInfo().Assembly)
    .Where(t => t.Name.EndsWith("DM") || t.Name.EndsWith("Service"))
    .AsImplementedInterfaces();

这不是因为调用了
.Except()
吗?如果您的
MemberDM
实现了
IDataModel
接口,那么就我所见,它不会被注册。我试图删除它,但它仍然不起作用。您的服务是泛型的吗
.Name
对于泛型返回
TypeName`n
,其中
n
是泛型参数的数量。i、 e.
MyService
将返回
MyService`1
作为名称,而不是
MyService
不是因为
。除了()
调用吗?如果您的
MemberDM
实现了
IDataModel
接口,那么就我所见,它不会被注册。我试图删除它,但它仍然不起作用。您的服务是泛型的吗
.Name
对于泛型返回
TypeName`n
,其中
n
是泛型参数的数量。i、 e.
MyService
将返回
MyService`1
作为名称,而不是
MyService
Thanx。我喜欢你把它们结合在一起的方式。我认为问题在于我试图使用实际的接口,而不是为此使用.AsImplementedInterfaces().Thanx。我喜欢你把它们结合在一起的方式。我认为问题在于我试图使用实际的接口,而不是使用.AsImplementedInterfaces()。
// Register DataModel as IDataModel 
// Register Service Class as IService
builder.RegisterAssemblyTypes(typeof(IServiceAssembly).GetTypeInfo().Assembly)
    .Where(t => t.Name.EndsWith("DM") || t.Name.EndsWith("Service"))
    .AsImplementedInterfaces();