C# 如何在Startup.cs中实例化此对象
我在基于web api的项目中使用基于OAuth令牌的身份验证C# 如何在Startup.cs中实例化此对象,c#,asp.net-web-api,dependency-injection,castle-windsor,C#,Asp.net Web Api,Dependency Injection,Castle Windsor,我在基于web api的项目中使用基于OAuth令牌的身份验证 public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) { var allowedOrigin = context.OwinContext.Get<string>("as:clientAllowedOrigin"); if
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
var allowedOrigin = context.OwinContext.Get<string>("as:clientAllowedOrigin");
if (allowedOrigin == null) allowedOrigin = "*";
context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { allowedOrigin });
//At this line, I am getting Error Object reference not set.
if (_membershipService.ValidateUser(context.UserName.Trim(), context.Password.Trim()))
{
var user = _membershipService.GetUser(context.UserName);
/* db based authenication*/
var identity = new ClaimsIdentity(context.Options.AuthenticationType);
identity.AddClaim(new Claim(ClaimTypes.Name, context.UserName));
identity.AddClaim(new Claim(ClaimTypes.Role, "user"));
identity.AddClaim(new Claim("sub", context.UserName));
var props = new AuthenticationProperties(new Dictionary<string, string>
{
{
"as:client_id", (context.ClientId == null) ? string.Empty : context.ClientId
},
{
"userName", user.Email
},
{
"role", (user.Role).ToString()
}
});
var ticket = new AuthenticationTicket(identity, props);
context.Validated(ticket);
}
else
{
context.Rejected();
}
}
调用方法时,我传递引用
Startup.cs
public void ConfigureOAuth(IAppBuilder app)
{
OAuthBearerOptions = new OAuthBearerAuthenticationOptions();
OAuthAuthorizationServerOptions OAuthServerOptions = new OAuthAuthorizationServerOptions()
{
//passing the membership object.
Provider = new SimpleAuthorizationServerProvider(_membershipService),
};
// Token Generation
app.UseOAuthAuthorizationServer(OAuthServerOptions);
//Token Consumption
app.UseOAuthBearerAuthentication(OAuthBearerOptions);
}
public Startup()
{
GlobalConfiguration.Configure(WebApiConfig.Register);
RegisterIOC();
}
private void RegisterIOC()
{
container = new WindsorContainer();
container.Register(Component.For<SimpleAuthorizationServerProvider>().LifestyleTransient());
//rest of the stuff
}
对象引用未设置为对象的实例
因此,我尝试重载Startup.cs的构造函数,如下所示:
public Startup(IMembershipservice membership)
{
_membership = membership;
}
但这会抛出下面的运行时错误
没有为此对象定义无参数构造函数
Startup.cs
public void ConfigureOAuth(IAppBuilder app)
{
OAuthBearerOptions = new OAuthBearerAuthenticationOptions();
OAuthAuthorizationServerOptions OAuthServerOptions = new OAuthAuthorizationServerOptions()
{
//passing the membership object.
Provider = new SimpleAuthorizationServerProvider(_membershipService),
};
// Token Generation
app.UseOAuthAuthorizationServer(OAuthServerOptions);
//Token Consumption
app.UseOAuthBearerAuthentication(OAuthBearerOptions);
}
public Startup()
{
GlobalConfiguration.Configure(WebApiConfig.Register);
RegisterIOC();
}
private void RegisterIOC()
{
container = new WindsorContainer();
container.Register(Component.For<SimpleAuthorizationServerProvider>().LifestyleTransient());
//rest of the stuff
}
公共启动()
{
GlobalConfiguration.Configure(WebApiConfig.Register);
注册表项();
}
私有无效注册表()
{
容器=新WindsorContainer();
container.Register(Component.For().LifestyleTransient());
//其他的东西
}
如何实例化成员类
(请注意,我使用的是Castle Windsor DI集装箱)
非常感谢您的帮助/建议。
谢谢。您说您正在使用Castle Windsor DI容器,但Startup.cs类中没有初始化容器的代码。 如果使用OWIN(Startup.cs类),则不需要在Global.asax application\u Start方法中初始化应用程序。 您应该将容器初始化移动到Startup.cs类,并在初始化后解析IMembershipservice实例。 大概是这样的:
public void Configuration(IAppBuilder app)
{
var container = new WindsorContainer().Install(new WindsorInstaller());
container.Register(Component.For<IAppBuilder>().Instance(app));
var httpDependencyResolver = new WindsorHttpDependencyResolver(container);
HttpConfiguration config = new HttpConfiguration();
config.MapHttpAttributeRoutes();
config.DependencyResolver = httpDependencyResolver;
GlobalConfiguration.Configuration.Services.Replace(typeof(IHttpControllerActivator), new WindsorControllerActivator(container));
app.UseWebApi(config);
}
public void配置(IAppBuilder应用程序)
{
var container=new WindsorContainer().Install(new WindsorInstaller());
container.Register(Component.For().Instance(app));
var httpDependencyResolver=新WindsorHttpDependencyResolver(容器);
HttpConfiguration config=新的HttpConfiguration();
config.maphttpAttribute路由();
config.dependencysolver=httpdependencysolver;
全球配置.配置.服务.更换(类型为(IHTTP控制器激活器),新Windsorc控制器激活器(容器));
app.UseWebApi(配置);
}
然后,您可以使用容器解析类的实例。DI stuff位于单独的位置&我重新检查了,所有映射都已就位。但是错误仍然存在“DI的东西在不同的地方”,但是为什么只有这个类会出错呢。所有的控制器和服务都得到了正确的实例化我正在调用RegisterIOC()(一个私有方法),因为Startup.cs类现在是访问点。正在Global.asax中启动应用程序之前执行Startup.cs配置方法。因此,Startup.cs无法由在应用程序\u start中配置的DI解析。答案就在这里