OwinStartup时如何使用DI容器 [英] How to use DI container when OwinStartup
问题描述
这是一个 Web API 2 项目.
It's a Web API 2 project.
当我使用 Ninject 实现 DI 时,我收到一条错误消息
When I implement DI using Ninject, I got an error message
尝试创建类型为TokenController"的控制器时出错.确保控制器具有无参数的公共构造函数.
An error occurred when trying to create a controller of type 'TokenController'. Make sure that the controller has a parameterless public constructor.
[assembly: OwinStartup(typeof(Web.Startup))]
namespace Web
{
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
ConfigureAuth(app);
ConfigureWebApi(app);
}
}
}
public class TokenController : ApiController
{
private IUserService _userService;
public TokenController(IUserService userService)
{
this._userService = userService;
}
[Route("api/Token")]
public HttpResponseMessage PostToken(UserViewModel model)
{
if (_userService.ValidateUser(model.Account, model.Password))
{
ClaimsIdentity identity = new ClaimsIdentity(Startup.OAuthBearerOptions.AuthenticationType);
identity.AddClaim(new Claim(ClaimTypes.Name, model.Account));
AuthenticationTicket ticket = new AuthenticationTicket(identity, new AuthenticationProperties());
var currentUtc = new SystemClock().UtcNow;
ticket.Properties.IssuedUtc = currentUtc;
ticket.Properties.ExpiresUtc = currentUtc.Add(TimeSpan.FromMinutes(30));
return new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new ObjectContent<object>(new
{
UserName = model.Account,
AccessToken = Startup.OAuthBearerOptions.AccessTokenFormat.Protect(ticket)
}, Configuration.Formatters.JsonFormatter)
};
}
return new HttpResponseMessage(HttpStatusCode.BadRequest);
}
}
当我将 <add key="owin:AutomaticAppStartup" value="false"/>
添加到 web.config 时
When I add <add key="owin:AutomaticAppStartup" value="false" />
to web.config
Ninject 工作正常,但 Startup.OAuthBearerOptions.AccessTokenFormat 变为 null
Ninject works fine, but Startup.OAuthBearerOptions.AccessTokenFormat becomes to null
如何在 OWIN 中使用 DI 容器?
How to use DI container with OWIN?
更新
实现 IDependencyResolver 并使用 WebAPI Dependency Resolver 如下
Implement IDependencyResolver and use the WebAPI Dependency Resolver as below
public void ConfigureWebApi(IAppBuilder app)
{
HttpConfiguration config = new HttpConfiguration();
config.DependencyResolver = new NinjectDependencyResolver(NinjectWebCommon.CreateKernel());
app.UseWebApi(config);
}
在简单的注射器案例中
public void ConfigureWebApi(IAppBuilder app)
{
HttpConfiguration config = new HttpConfiguration();
var container = new Container();
container.Register<IUserService, UserService>();
config.DependencyResolver = new SimpleInjectorWebApiDependencyResolver(container);
app.UseWebApi(config);
}
SimpleInjectorWebApiDependencyResolver
推荐答案
你可能想看看 这篇博文.
它正在使用 Unity,但最终应该是一样的.
It's using Unity but it should end-up being the same.
基本上,使用 WebAPI Dependency Resolver.确保所有内容都正确映射并且应该没问题.
Basically, use the WebAPI Dependency Resolver. Make sure that everything is mapped properly and it should be fine.
如果在设置 DI 后您的 OAuth 令牌仍有问题,请告诉我.
If after setting up your DI you still have problem with your OAuth token, let me know.
干杯
这篇关于OwinStartup时如何使用DI容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!