web api 2中autofac的无参数构造函数错误

Meh*_*adi 5 asp.net-mvc dependency-injection autofac neo4jclient asp.net-web-api2

嘿伙计我正在使用Autofac作为IOC,这是我的结构:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AdTudent.Repo
{
    public interface IRepository
    {
        IAdvertisementRepository Advertisements { get; set; }
        IProfileRepository Profiles { get; set; }
    }
}
Run Code Online (Sandbox Code Playgroud)

我的存储库类是:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AdTudent.Repo
{
    public class Repositories : IRepository
    {
        public Repositories(IAdvertisementRepository advertisementRepository,
                            IProfileRepository profileRepository)
        {
            Advertisements = advertisementRepository;
            Profiles = profileRepository;
        }
        public IAdvertisementRepository Advertisements { get; set; }
        public IProfileRepository Profiles { get; set; }
    }
}
Run Code Online (Sandbox Code Playgroud)

我的启动课是:

public partial class Startup
{
    public void Configuration(IAppBuilder app)
    {
         var builder = new ContainerBuilder();

        builder.RegisterType<Repositories>().As<IRepository>();
        builder.Register<IGraphClient>(context =>
        {
            var graphClient = new GraphClient(new Uri("http://localhost:7474/db/data"));
            graphClient.Connect();
            return graphClient;
        }).SingleInstance();
        // STANDARD WEB API SETUP:

        // Get your HttpConfiguration. In OWIN, you'll create one
        // rather than using GlobalConfiguration.
        var config = new HttpConfiguration();

        // Register your Web API controllers.
        //builder.RegisterApiControllers(Assembly.GetExecutingAssembly());

        // Run other optional steps, like registering filters,
        // per-controller-type services, etc., then set the dependency resolver
        // to be Autofac.

        var container = builder.Build();
        config.DependencyResolver = new AutofacWebApiDependencyResolver(container);

        app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);

        // OWIN WEB API SETUP:

        // Register the Autofac middleware FIRST, then the Autofac Web API middleware,
        // and finally the standard Web API middleware.
        app.UseAutofacMiddleware(container);
        app.UseAutofacWebApi(config);
        app.UseWebApi(config);

        ConfigureAuth(app);
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的帐户管理员:

public class AccountController : ApiController
{
    private  IRepository _repository;
    private const string LocalLoginProvider = "Local";
    private ApplicationUserManager _userManager;
    private IGraphClient _graphClient;


    public ISecureDataFormat<AuthenticationTicket> AccessTokenFormat { get; private set; }       

    public AccountController(IRepository repository, IGraphClient graphClient,
         ISecureDataFormat<AuthenticationTicket> accessTokenFormat)
    {

        AccessTokenFormat = accessTokenFormat;
        _repository = repository;
        _graphClient = graphClient;

    }
}
Run Code Online (Sandbox Code Playgroud)

但我总是这个问题

"消息":"发生错误.","ExceptionMessage":"尝试创建'AccountController'类型的控制器时发生错误.确保控制器具有无参数的公共构造函数.","ExceptionType":" System.Web.Http.Controllers.HttpControllerDescriptor.CreateController(HttpRequestMessage)中的System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage请求,HttpControllerDescriptor controllerDescriptor,类型controllerType)\n \n的System.InvalidOperationException","StackTrace":"请求)\ r \n在System.Web.Http.Dispatcher.HttpControllerDispatcher.d__1.MoveNext()

我在互联网上搜索,但我找不到任何可以帮助任何人指导我的东西?

Nig*_*888 7

根据WebApi OWIN文档:

OWIN集成中的常见错误是使用GlobalConfiguration.Configuration.在OWIN中,您可以从头开始创建配置.GlobalConfiguration.Configuration使用OWIN集成时,您不应该在任何地方引用.

看起来你有几个问题:

  1. HttpConfiguration使用OWIN时newed了需求.
  2. 您缺少UseAutofacWebApi虚拟方法调用.

您还需要根据文档使用Autofac WebApi OWIN包.

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        var builder = new ContainerBuilder();

        // STANDARD WEB API SETUP:

        // Get your HttpConfiguration. In OWIN, you'll create one
        // rather than using GlobalConfiguration.
        var config = new HttpConfiguration();

        // Register your Web API controllers.
        builder.RegisterApiControllers(Assembly.GetExecutingAssembly());

        // Run other optional steps, like registering filters,
        // per-controller-type services, etc., then set the dependency resolver
        // to be Autofac.
        var container = builder.Build();
        config.DependencyResolver = new AutofacWebApiDependencyResolver(container);

        app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);

        // OWIN WEB API SETUP:

        // Register the Autofac middleware FIRST, then the Autofac Web API middleware,
        // and finally the standard Web API middleware.
        app.UseAutofacMiddleware(container);
        app.UseAutofacWebApi(config);
        app.UseWebApi(config);
    }
}
Run Code Online (Sandbox Code Playgroud)