使用xml/app.config配置Castle Windsor [英] Configuring Castle Windsor using xml/app.config
问题描述
我目前正在使用Castle Windsor构建示例应用程序.座右铭是使用xml/app.config来打开/关闭方法拦截.我之前使用过Fluent API,它很吸引人.下一步,我尝试用我的xml替换fluent API.
I am currently building a sample application using Castle Windsor. The motto is to use xml/app.config to switch method interception on/off. I had used the Fluent API earlier and it worked as a charm. As the next step, I am trying to replace the fluent API with my xml.
代码要点如下: 一个名为RandomOperations的类,带有两个虚拟方法. 一个实现IInterceptor的LoggingAspect类. 一个实现IModelInterceptorsSelector的MyInterceptorsSelector类 一个Program.cs,它以前具有流畅的api语法,现在仅用于调用RandomOperations类的方法. 带有名为的部分的app.config,具有注册组件的xml语法.
The gist of the code is as follows: A class called RandomOperations with two virtual methods. A LoggingAspect class which implements IInterceptor. A MyInterceptorsSelector class which implements IModelInterceptorsSelector A Program.cs which had the fluent api syntax earlier and is now uses to only make calls to methods of RandomOperations class. An app.config with a section called which has the xml syntax of registering components.
当我使用流利的api时,我可以截获方法调用,但无法使用xml/app.config注册来完成.有人可以告诉我们遗漏了什么吗?
When I use the fluent api, I am able to intercept the method calls but I am unable to do it using the xml/app.config registration. Could someone please throw some light on what is being missed?
这些类如下:
RandomOperations.cs
RandomOperations.cs
public class RandomOperations
{
public virtual int MyRandomMethod(int x)
{
return x * x;
}
public virtual void Writer(string x)
{
Console.WriteLine(x);
}
}
LoggingAspect.cs
LoggingAspect.cs
public class LoggingAspect : IInterceptor
{
public void Intercept(IInvocation invocation)
{
Console.WriteLine("Intercepted the call to " + invocation.Method.Name);
invocation.Proceed();
Console.WriteLine("After the method call, the return value is " + invocation.ReturnValue);
}
}
MyInterceptorsSelector.cs
MyInterceptorsSelector.cs
public class MyInterceptorsSelector : IModelInterceptorsSelector
{
public bool HasInterceptors(ComponentModel model)
{
return typeof(LoggingAspect) != model.Implementation &&
model.Implementation.Namespace.StartsWith("ConsoleApplication1") ;
}
public InterceptorReference[] SelectInterceptors(ComponentModel model, Castle.Core.InterceptorReference[] obj)
{
var interceptors = new List<InterceptorReference>(model.Interceptors.Count + 1);
foreach (InterceptorReference inter in model.Interceptors)
{
interceptors.Add(inter);
}
return interceptors.ToArray();
}
}
Program.cs中的主要内容
Main in Program.cs
static void Main(string[] args)
{
var container = new WindsorContainer();
//container.Register(Component.For<RandomOperations>().Interceptors(typeof(LoggingAspect)));
//container.Register(Component.For<LoggingAspect>());
//container.Kernel.ProxyFactory.AddInterceptorSelector(new MyInterceptorsSelector());
var service = container.Resolve<RandomOperations>();
service.MyRandomMethod(4);
service.Writer("Hello, World");
}
删除注释掉的流利的api语法可使应用程序正常工作.
Removing the commented out fluent api syntax makes the application work correctly.
App.config
App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="castle" type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, Castle.Windsor" />
</configSections>
<castle>
<components>
<component id="MyInterceptorsSelector" type="MyInterceptorsSelector"/>
<component
id="LoggingAspect"
type="ConsoleApplication1.LoggingAspect, ConsoleApplication1">
</component>
<component
type="ConsoleApplication1.RandomOperations, ConsoleApplication1">
<interceptors selector="${MyInterceptorsSelector}">
<interceptor>${LoggingAspect}</interceptor>
</interceptors>
</component>
</components>
</castle>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
谢谢.
推荐答案
您需要将IConfigurationInterpreter
传递给Windsor构造函数.更改:
You need to pass an IConfigurationInterpreter
to your Windsor constructor. Change:
var container = new WindsorContainer();
收件人:
var container = new WindsorContainer(new XmlInterpreter());
XmlInterpreter
(无参数)将从您的app.config/web.config中提取配置.
The XmlInterpreter
(with no parameters) will pull configuration from your app.config/web.config.
有关使用IConfigurationInterpreter
的更多选项,请参见 docs .
For more options on using IConfigurationInterpreter
, see the docs.
这篇关于使用xml/app.config配置Castle Windsor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!