如何使用依赖于 HttpRequestMessage 值的 Castle Windsor 解析实例
How to resolve instances with Castle Windsor that rely on values from the HttpRequestMessage
我正在使用 Web Api 和 OWIN 管道。
Startup.cs
public class Startup {
public void Configuration(IAppBuilder app) {
var container = new WindsorContainer().Install(FromAssembly.This());
var config = new HttpConfiguration();
config.MapHttpAttributeRoutes();
// extension method to resolve controllers with Windsor.
app.UseWindsorApi(config, container);
}
}
MyClassInstaller.cs (IWindsorInstaller)
public void Install(IWindsorContainer container, IConfigurationStore store)
{
container.Register(
Component.For<IMyClass>().ImplementedBy<MyClass>()
.LifeStyle.PerWebRequest()
.DependsOn(new {
configSetting = ConfigurationManager.AppSettings["key"],
otherSetting = ???
}));
}
MyClass.cs & 我MyClass.cs
public class MyClass : IMyClass {
private readonly string configSetting;
private readonly string otherSetting;
public MyClass(string configSetting, string otherSetting) {
this.configSetting = configSetting;
this.otherSetting = otherSetting;
}
public void DoSomething() {
// method that uses the settings that were set in the constructor.
}
}
public interface IMyClass {
void DoSomething();
}
MyController.cs
public class MyController : ApiController {
private readonly IMyClass myClass;
public MyController(IMyClass myClass) {
this.myClass = myClass;
}
[HttpGet]
[Route("")]
public async Task<IHttpActionResult> GetAsync() {
// uses this.myClass
}
}
这就是我卡住的地方。每当解析 MyClass 的实例时,都需要分配 otherSetting 的值。
otherSetting 的值由两件事决定。
1) client_id 从请求中声明价值。
2) 异步调用以 client_id 作为参数且 returns 为字符串值的方法。这是 otherSetting
中设置的内容
public async Task<string> GetOtherSetting(string client_id) {
return "value";
}
我什至不确定从哪里开始让 Castle 注入基于这两个标准的值...
更新:
我已经更新了 patotopeelings 的答案,做了一些小改动,它似乎工作正常。
.DynamicParameters(async (k, p) =>
{
var fundNameProvider = k.Resolve<IFundNameValueProvider>();
p["otherSetting"] = await fundNameProvider.GetFundNameAsync();
k.ReleaseComponent(fundNameProvider);
}))
我将其更改为异步 lambda,以便我可以等待该方法。
我还调用了 ReleaseComponent,因为我的印象是,您手动使用 Castle 解析的对象也需要手动释放。
使用UsingFactoryMethod
和DynamicParamters
首先,注入当前声明
...
Component.For<ClaimsIdentity>().UsingFactoryMethod(() => HttpContext.Current.User.Identity as ClaimsIdentity).LifestylePerWebRequest()
...
进入一个服务(IOtherValueProvider
- PerWebRequest),该服务有一个 GetOtherSetting
方法来等待异步调用(即将异步调用转换为同步调用)以获取 otherSetting 从 client_id 中提取,注入 ClaimsIdentity
然后用DynamicParameters
得到值
... register your class ...
.DynamicParameters((kernel, parameters) =>
{
parameters["otherSetting"] = kernel.Resolve<IOtherValueProvider>().GetOtherSetting();
}))
我正在使用 Web Api 和 OWIN 管道。
Startup.cs
public class Startup {
public void Configuration(IAppBuilder app) {
var container = new WindsorContainer().Install(FromAssembly.This());
var config = new HttpConfiguration();
config.MapHttpAttributeRoutes();
// extension method to resolve controllers with Windsor.
app.UseWindsorApi(config, container);
}
}
MyClassInstaller.cs (IWindsorInstaller)
public void Install(IWindsorContainer container, IConfigurationStore store)
{
container.Register(
Component.For<IMyClass>().ImplementedBy<MyClass>()
.LifeStyle.PerWebRequest()
.DependsOn(new {
configSetting = ConfigurationManager.AppSettings["key"],
otherSetting = ???
}));
}
MyClass.cs & 我MyClass.cs
public class MyClass : IMyClass {
private readonly string configSetting;
private readonly string otherSetting;
public MyClass(string configSetting, string otherSetting) {
this.configSetting = configSetting;
this.otherSetting = otherSetting;
}
public void DoSomething() {
// method that uses the settings that were set in the constructor.
}
}
public interface IMyClass {
void DoSomething();
}
MyController.cs
public class MyController : ApiController {
private readonly IMyClass myClass;
public MyController(IMyClass myClass) {
this.myClass = myClass;
}
[HttpGet]
[Route("")]
public async Task<IHttpActionResult> GetAsync() {
// uses this.myClass
}
}
这就是我卡住的地方。每当解析 MyClass 的实例时,都需要分配 otherSetting 的值。
otherSetting 的值由两件事决定。
1) client_id 从请求中声明价值。
2) 异步调用以 client_id 作为参数且 returns 为字符串值的方法。这是 otherSetting
public async Task<string> GetOtherSetting(string client_id) {
return "value";
}
我什至不确定从哪里开始让 Castle 注入基于这两个标准的值...
更新: 我已经更新了 patotopeelings 的答案,做了一些小改动,它似乎工作正常。
.DynamicParameters(async (k, p) =>
{
var fundNameProvider = k.Resolve<IFundNameValueProvider>();
p["otherSetting"] = await fundNameProvider.GetFundNameAsync();
k.ReleaseComponent(fundNameProvider);
}))
我将其更改为异步 lambda,以便我可以等待该方法。 我还调用了 ReleaseComponent,因为我的印象是,您手动使用 Castle 解析的对象也需要手动释放。
使用UsingFactoryMethod
和DynamicParamters
首先,注入当前声明
...
Component.For<ClaimsIdentity>().UsingFactoryMethod(() => HttpContext.Current.User.Identity as ClaimsIdentity).LifestylePerWebRequest()
...
进入一个服务(IOtherValueProvider
- PerWebRequest),该服务有一个 GetOtherSetting
方法来等待异步调用(即将异步调用转换为同步调用)以获取 otherSetting 从 client_id 中提取,注入 ClaimsIdentity
然后用DynamicParameters
得到值
... register your class ...
.DynamicParameters((kernel, parameters) =>
{
parameters["otherSetting"] = kernel.Resolve<IOtherValueProvider>().GetOtherSetting();
}))