4

我使用Autofac作为国际奥委会和这里嗨是我的结构:在网页API autofac参数的构造函数错误2

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; } 
    } 
} 

,我的仓库类:

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; } 
    } 
} 

和我的启动类是:

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); 
    } 
} 

,这里是我的帐户控制:

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; 

    } 
} 

但我总是这个问题

“消息”:“发生了错误。” “ExceptionMessage”:“试图创建类型的控制装置‘的AccountController’时发生错误。 “, ”ExceptionType“:”System.InvalidOperationException“, ”StackTrace“:”at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request,HttpControllerDescriptor controllerDescriptor,Type controllerType个)\ r \ n在System.Web.Http.Controllers.HttpControllerDescriptor.CreateController(HttpRequestMessage请求个)\ r \ n在System.Web.Http.Dispatcher.HttpControllerDispatcher.d__1.MoveNext()

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

回答

5

WebApi OWIN documentation

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

它看起来像你有几个问题:

  1. HttpConfiguration需要使用OWIN时newed起来。
  2. 您错过了UseAutofacWebApi虚拟方法调用。

您还需要Autofac WebApi OWIN package根据文档。

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); 
    } 
} 
+0

我不需要MVC控制器我只是需要web API控制器,因为我上面提到,你有什么想法吗? –

+0

您还没有按照正确的顺序注册您的全局过滤器。在调用builder.Build();'之前,您需要调用'builder.RegisterWebApiFilterProvider(GlobalConfiguration.Configuration);'。看我上面的例子。 – NightOwl888

+0

我按照你所说的编辑了代码,但结果和以前一样,你有什么想法吗? –

0

您还没有告诉Autofac如何创建ISecureDataFormat<AuthenticationTicket>实例。

在您拨打builder.Build()之前,在您的启动课程中添加此代码。

builder.RegisterType<ISecureDataFormat<AuthenticationTicket>>().As<TicketDataFormat>(); 
+0

感谢您的帮助,但问题不在于您提到的问题我尝试过,但无法正常工作 –

+0

尝试发布一个可以真正运行的完整示例。否则,任何人的猜测。 –

0

检查您的Global.asax.cs。你应该删除这条线,如果你有它:

GlobalConfiguration.Configure(WebApiConfig.Register); 

您可以将其移动到启动。CS这样的配置:

WebApiConfig.Register(config);