【发布时间】:2016-02-22 13:56:18
【问题描述】:
我有一个实现洋葱架构的新应用程序,现在我想在 owin 的启动类中注入一个服务。 ioc 进程在启动类之前启动,因为它位于首先使用 webactivator 运行的引导程序项目中。是否可以在启动类中注入服务?
public class Startup
{
public void Configuration(IAppBuilder app)
{
HttpConfiguration config = new HttpConfiguration();
// Web API routes
config.MapHttpAttributeRoutes();
ConfigureOAuth(app);
app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
app.UseWebApi(config);
}
public void ConfigureOAuth(IAppBuilder app)
{
// i want to use an injected service inside a provider :my problem :(
var providers = Providers(service);
OAuthAuthorizationServerOptions OAuthServerOptions = new OAuthAuthorizationServerOptions()
{
//For Dev enviroment only (on production should be AllowInsecureHttp = false)
AllowInsecureHttp = true,
TokenEndpointPath = new PathString("/generateToken"),
AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(30),
Provider = providers,
AccessTokenFormat = new CustomJwtFormat("http://localhost:18292/")
};
// OAuth 2.0 Bearer Access Token Generation
app.UseOAuthAuthorizationServer(OAuthServerOptions);
}
}
【问题讨论】:
-
为什么不将提供者和/或服务传递给
ConfigureOAuth方法?那就是反转控制。您需要一个设置 IOC 容器的条目,因此它也可以在StartUp中。顺便说一句,您不必遵循StartUp约定。如果您愿意,可以手动启动一个 owin 应用程序。 -
谢谢您的评论,但如果我在 configureOAuth 中传递服务和/或提供者,我应该首先在配置方法中传递它,因为它是调用者..问题是我不能做配置( IAppbuilder, service )因为它抛出异常( Startup 没有预期的签名 'void Configuration(IAppBuilder)' ..)
-
是的,你是。我会告诉你我是怎么做的。
标签: c# dependency-injection owin onion-architecture