小编Ric*_*rdo的帖子

Autofac异常:无法解析构造函数'Void .ctor的参数

我有以下错误:

ExceptionMessage =可以使用可用的服务和参数调用在'RestAPI.DevelopersController'类型上使用'Autofac.Core.Activators.Reflection.DefaultConstructorFinder'找到的构造函数:无法解析构造函数'Void'的参数'Services.DevelopersService userService'.构造函数(Services.DevelopersService)".

的Global.asax.cs

protected void Application_Start()
    {
        GlobalConfiguration.Configure(WebApiConfig.Register);
        AutoMapperConfig.RegisterMappings();
        var builder = new ContainerBuilder();
        builder.RegisterModule(new ServiceModule());
        builder.RegisterModule(new ORMModule());
        builder.RegisterApiControllers(Assembly.GetExecutingAssembly()).PropertiesAutowired();
        var container = builder.Build();
        var resolver = new AutofacWebApiDependencyResolver(container);
        GlobalConfiguration.Configuration.DependencyResolver = resolver;
    }
Run Code Online (Sandbox Code Playgroud)

ServiceModule.cs

public class ServiceModule : Autofac.Module
{
    protected override void Load(ContainerBuilder builder)
    {
        builder.RegisterAssemblyTypes(Assembly.Load("Services"))
                 .Where(t => t.Name.EndsWith("Service"))
                 .AsImplementedInterfaces()
                 .InstancePerLifetimeScope();
    }
}
Run Code Online (Sandbox Code Playgroud)

ORMModule.cs

public class ORMModule : Autofac.Module
{
    protected override void Load(ContainerBuilder builder)
    {
        builder.RegisterType(typeof(DatabaseContext)).As(typeof(DbContext)).InstancePerLifetimeScope();
    }
}
Run Code Online (Sandbox Code Playgroud)

DevelopersController

public class DevelopersController : ApiController
{
    private DevelopersService …
Run Code Online (Sandbox Code Playgroud)

c# asp.net autofac asp.net-web-api

9
推荐指数
2
解决办法
2万
查看次数

标签 统计

asp.net ×1

asp.net-web-api ×1

autofac ×1

c# ×1