2016-12-28 8 views
0

私のプロジェクトでは、Web API 2、autofacを使用しています。私はそれを注入するコントローラーで、Owinをプロジェクトにインストールしてからプロジェクトを実行してナビゲートするまで "タイプ 'AccountController'のコントローラを作成しようとしたときにエラーが発生しました。コントローラにパラメータのないpublicコンストラクタがあることを確認してください。"登録するには私をサポートしてくださいOwinミドルウェアはAutofacに登録できませんでした

​​

AccountController.cs:

[RoutePrefix("api/Account")] 
public class AccountController : ApiControllerBase 
{ 
    private readonly IAccountService _accountService; 

    public AccountController(IAccountService accountService, 
     IEntityBaseRepository<Error> errorsRepository) 
     : base(errorsRepository) 
    { 
     _accountService = accountService; 
    } 

    // POST api/Account/Register 
    [AllowAnonymous] 
    [Route("Register")] 
    public async Task<IHttpActionResult> Register(UserModel userModel) 
    { 
     if (!ModelState.IsValid) 
     { 
      return BadRequest(ModelState); 
     } 

     IdentityResult result = await _repo.RegisterUser(userModel); 

     IHttpActionResult errorResult = GetErrorResult(result); 

     if (errorResult != null) 
     { 
      return errorResult; 
     } 

     return Ok(); 
    } 
} 

Startup.cs:

public class Startup 
{ 
    public void Configuration(IAppBuilder app) 
    { 
     ConfigureOAuth(app); 
     HttpConfiguration config = new HttpConfiguration(); 
     WebApiConfig.Register(config); 
     app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll); 
     app.UseWebApi(config); 
    } 

    public void ConfigureOAuth(IAppBuilder app) 
    { 
     OAuthAuthorizationServerOptions OAuthServerOptions = new OAuthAuthorizationServerOptions() 
     { 
      AllowInsecureHttp = true, 
      TokenEndpointPath = new PathString("/token"), 
      AccessTokenExpireTimeSpan = TimeSpan.FromDays(1), 
      Provider = new SimpleAuthorizationServerProvider() 
     }; 

     // Token Generation 
     app.UseOAuthAuthorizationServer(OAuthServerOptions); 
     app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions()); 

    } 

} 

AutofacWebApiConfig.csここ は私のコードですまたはAutofacでOWINを設定します。

ありがとうございました。

答えて

0

以下NuGetから

  1. 参考Autofac.Owinパッケージステップに従ってください。
  2. Autofacコンテナを構築します。
  3. OWINでAutofacミドルウェアを登録し、コンテナに渡します。

文書が利用可能です。here

関連する問題