我可以在 Asp.net Web Api 项目的 WebApiConfig 类中使用 Ninject 注入类的实例吗?这是我的尝试
public static class WebApiConfig
{
private static IAccountsService _accountsService;
[Inject]
public static IAccountsService AccountsService
{
get { return _accountsService; }
set { _accountsService = value; }
}
public static void Register(HttpConfiguration config)
{
var authentication = CreateAuthenticationConfiguration();
config.MessageHandlers.Add(new AuthenticationHandler(authentication));
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.EnableSystemDiagnosticsTracing();
}
private static AuthenticationConfiguration CreateAuthenticationConfiguration()
{
var authentication = new AuthenticationConfiguration
{
RequireSsl = false,
EnableSessionToken = true
};
// Basic Authentication
authentication.AddBasicAuthentication(AccountsService.ValidateAccountUsingSiteCredential);
return authentication;
}
}
但是,我正在Object reference not set to an instance of an object
这条线上authentication.AddBasicAuthentication(AccountsService.Validate);