使用autofac时无参数默认构造函数错误

时间:2017-05-26 17:48:55

标签: c# asp.net-web-api dependency-injection asp.net-web-api2 autofac

我不知道为什么这不起作用。 我已经从其他项目中复制(如果可能)我的 Startup.cs ,但是由于某种原因,这不起作用。

Startup.cs 如下所示:

// Readonly properties
private readonly ICategoryProvider _provider;

/// <summary>
/// Default constructor
/// </summary>
public CategoriesController(ICategoryProvider provider)
{
    _provider = provider;
}

但是当我向任何控制器发出任何请求时,我收到此错误:

  

尝试创建“CategoriesController”类型的控制器时发生错误。确保控制器具有无参数的公共构造函数。

CategoriesController 只有一个依赖项:

// Fields
private readonly IUnitOfWork _unitOfWork;
private readonly ICategoryService _service;

/// <summary>
/// Default constructor
/// </summary>
public CategoryProvider(IUnitOfWork unitOfWork, ICategoryService service)
{

    // Map our properties
    _unitOfWork = unitOfWork;
    _service = service;
}

CategoryProvider 有这个构造函数:

public class AutofacModule : Module
{
    protected override void Load(ContainerBuilder builder)
    {

        // Create our Singletons
        builder.RegisterType<PiiiCKConfig>().As<PiiiCKConfig>().SingleInstance();
        builder.RegisterType<EncryptionProvider>().As<IEncryptionProvider>().SingleInstance();
        builder.RegisterType<OAuthProvider>().As<OAuthProvider>().SingleInstance();
        builder.RegisterType<RefreshTokenProvider>().As<IAuthenticationTokenProvider>().SingleInstance();
        builder.RegisterType<StripeProvider>().As<IStripeProvider>().SingleInstance();

        // InstancePerLifetimeScope
        builder.RegisterType<DatabaseContext>().As<DbContext>().InstancePerLifetimeScope();
        builder.RegisterType<UnitOfWork>().As<IUnitOfWork>().InstancePerLifetimeScope();
        builder.RegisterType<UserProvider>().As<IUserProvider>().InstancePerLifetimeScope();

        // Create our services (per dependancy)
        builder.RegisterType<AnswerService>().As<IAnswerService>().InstancePerDependency();
        builder.RegisterType<AttributeService>().As<IAttributeService>().InstancePerDependency();
        builder.RegisterType<CategoryService>().As<ICategoryService>().InstancePerDependency();
        builder.RegisterType<ClaimService>().As<IClaimService>().InstancePerDependency();
        builder.RegisterType<ClientService>().As<IClientService>().InstancePerDependency();
        builder.RegisterType<CriteriaService>().As<ICriteriaService>().InstancePerDependency();
        builder.RegisterType<FeedService>().As<IFeedService>().InstancePerDependency();
        builder.RegisterType<FormulaService>().As<IFormulaService>().InstancePerDependency();
        builder.RegisterType<GroupService>().As<IGroupService>().InstancePerDependency();
        builder.RegisterType<QuestionService>().As<IQuestionService>().InstancePerDependency();
        builder.RegisterType<QuoteService>().As<IQuoteService>().InstancePerDependency();
        builder.RegisterType<RefreshTokenService>().As<IRefreshTokenService>().InstancePerDependency();

        // Create our providers
        builder.RegisterType<AnswerProvider>().As<IAnswerProvider>().InstancePerRequest();
        builder.RegisterType<AttributeProvider>().As<IAttributeProvider>().InstancePerRequest();
        builder.RegisterType<CategoryProvider>().As<ICategoryProvider>().InstancePerRequest();
        builder.RegisterType<ClaimProvider>().As<ClaimProvider>().InstancePerRequest();
        builder.RegisterType<ClientProvider>().As<IClientProvider>().InstancePerRequest();
        builder.RegisterType<CriteriaProvider>().As<ICriteriaProvider>().InstancePerRequest();
        builder.RegisterType<FeedProvider>().As<IFeedProvider>().InstancePerRequest();
        builder.RegisterType<FormulaProvider>().As<IFormulaProvider>().InstancePerRequest();
        builder.RegisterType<GroupProvider>().As<IGroupProvider>().InstancePerRequest();
        builder.RegisterType<QuestionProvider>().As<IQuestionProvider>().InstancePerRequest();

        builder.RegisterType<ApiAiProvider>().As<IApiAiProvider>().InstancePerRequest();
        builder.RegisterType<PiiikProvider>().As<IPiiikProvider>().InstancePerRequest();
        builder.RegisterType<ProductProvider>().As<IProductProvider>().InstancePerRequest();
        builder.RegisterType<SettingProvider>().As<ISettingProvider>().InstancePerRequest();
        builder.RegisterType<TrackingProvider>().As<ITrackingProvider>().InstancePerRequest();

        // Users
        builder.Register(c => new UserStore<User>(c.Resolve<DbContext>())).As<IUserStore<User>>().InstancePerDependency();
        builder.Register(c => new IdentityFactoryOptions<UserProvider>() { DataProtectionProvider = new DpapiDataProtectionProvider(c.Resolve<PiiiCKConfig>().Issuer) }).InstancePerDependency();
    }
}

我的模块看起来像这样:

random.sample(range(1,100), 6)

有谁知道我做错了什么?

0 个答案:

没有答案