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