2016-06-14 81 views
1

我得到一個500錯誤,當我嘗試訪問我的ConfigureServices方法使用services.AddSingleton<>()注入的對象Startup.cs的服務:無法解析型「XYZ」

無法解析類型的服務方法Configure的參數authProviderproj.Auth.IAuthProvider,類型proj.Startup

據我可以看這應該是工作的 「工作」 的代碼示例告訴...

Startup.cs:

public void ConfigureServices(IServiceCollection services) 
{ 
    services.AddOptions(); 
    services.Configure<AppSettings>(Configuration); 
    services.AddSingleton<IAuthProvider, TranslatorAuthProvider>(); 
    services.AddMvc(); 
} 

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IAuthProvider authProvider) 
{ 
    ... 

    app.Run(async context => 
    { 
     await authProvider.GetToken(); 
    }); 

    app.UseMvc(routes => 
    { 
     routes.MapRoute(
      name: "default", 
      template: "{controller=Home}/{action=Index}/{id?}"); 
    }); 
} 

TranslatorAuthProvider.cs:

namespace proj.Auth 
{ 
    public class TranslatorAuthProvider : IAuthProvider 
    { 
     public TranslatorAuthProvider(IHttpContextAccessor httpContextAccessor, IOptions<AppSettings> settings) 
     { 
      ... 
     } 

     public async Task<OAuthToken> GetToken() 
     { 
      ... 
     } 
    } 
} 

IAuthProvider.cs:

namespace proj.Auth 
{ 
    public interface IAuthProvider 
    { 
     Task<OAuthToken> GetToken(); 
    } 
} 

回答

2

是的,這是預期的。它未能解決IHttpContextAccessor,因爲IHttpContextAccessor不是默認,因爲最近註冊(見#190

您應該手動ConfigureServices()像這樣

services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>(); 
註冊它
相關問題