質問

MVCとWebAPIの両方を使用してプロジェクトを持っています。 それはメンバーシップの再起動アプリケーションであるので、私は単一のアプリケーションプロジェクトの例を実行し、それをわずかに修正してスーツする。

コントローラのためのDIはOKであるただし、WebAPIコントローラを呼び出しようとしている場合はエラーが発生し続けます

コントローラには、パラメータのないパブリックコンストラクタがあることを確認してください。

WebAPIでAutoFacを使用するために必要なものはありますか?

これは私のstartup.cs

からのコードです。
        public void Configuration(IAppBuilder app)
    {
        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = "External", 
            AuthenticationMode = Microsoft.Owin.Security.AuthenticationMode.Passive
        });
        ConfigureMembershipReboot(app);
    }

    private static void ConfigureMembershipReboot(IAppBuilder app)
    {
        System.Data.Entity.Database.SetInitializer(new System.Data.Entity.MigrateDatabaseToLatestVersion<DefaultMembershipRebootDatabase, BrockAllen.MembershipReboot.Ef.Migrations.Configuration>());
        //System.Data.Entity.Database.SetInitializer(new System.Data.Entity.CreateDatabaseIfNotExists<DefaultMembershipRebootDatabase>());
        var cookieOptions = new CookieAuthenticationOptions
        {
            AuthenticationType = MembershipRebootOwinConstants.AuthenticationType
        };
        BuildAutofacContainer(app, cookieOptions.AuthenticationType);
        app.UseMembershipReboot(cookieOptions);
    }

    private static void BuildAutofacContainer(IAppBuilder app, string authType)
    {
        var builder = new ContainerBuilder();

        var config = CreateMembershipRebootConfiguration(app);

        builder.RegisterInstance(config).As<MembershipRebootConfiguration>();
        builder.RegisterType<DefaultUserAccountRepository>()
            .As<IUserAccountRepository>()
            .As<IUserAccountQuery>()
            .InstancePerLifetimeScope();
        builder.RegisterType<UserAccountService>().OnActivating(e =>
        {
            var owin = e.Context.Resolve<IOwinContext>();
            var debugging = false;
         #if DEBUG
            debugging = true;
         #endif
            e.Instance.ConfigureTwoFactorAuthenticationCookies(owin.Environment, debugging);
        })
        .AsSelf()
        .InstancePerLifetimeScope();
        builder.Register(ctx =>
        {
            var owin = ctx.Resolve<IOwinContext>();
            return new OwinAuthenticationService(authType, ctx.Resolve<UserAccountService>(), owin.Environment);
        })
        .As<AuthenticationService>()
        .InstancePerLifetimeScope();

        builder.Register(ctx=>HttpContext.Current.GetOwinContext()).As<IOwinContext>();
        builder.RegisterControllers(typeof(Startup).Assembly);

        builder.RegisterApiControllers(Assembly.GetExecutingAssembly()); 

        var container = builder.Build();
        System.Web.Mvc.DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
    }
.

役に立ちましたか?

解決

それは1ライナーでした:)

GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container);
.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top