Web API 및 MVC 5.1을 사용하여 AutoFAC 사용 WebAPI에서 작동하지 않음

StackOverflow https://stackoverflow.com//questions/24048698

  •  21-12-2019
  •  | 
  •  

문제

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