use*_*995 5 castle-windsor castle xml-configuration
我目前正在使用Castle Windsor构建示例应用程序。座右铭是使用xml / app.config来打开/关闭方法拦截。我之前使用过Fluent API,它很吸引人。下一步,我尝试将fluent API替换为xml。
代码的主要内容如下:一个名为RandomOperations的类,带有两个虚拟方法。一个实现IInterceptor的LoggingAspect类。一个MyInterceptorsSelector类,该类实现IModelInterceptorsSelector A Program.cs,该Program.cs以前具有流畅的api语法,现在仅用于对RandomOperations类的方法进行调用。一个名为app.config的部分,其中包含注册组件的xml语法。
使用流畅的api时,我可以截获方法调用,但无法使用xml / app.config注册来完成。有人可以告诉我们错过了什么吗?
这些类如下:
RandomOperations.cs
public class RandomOperations
{
public virtual int MyRandomMethod(int x)
{
return x * x;
}
public virtual void Writer(string x)
{
Console.WriteLine(x);
}
}
Run Code Online (Sandbox Code Playgroud)
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);
}
}
Run Code Online (Sandbox Code Playgroud)
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();
}
}
Run Code Online (Sandbox Code Playgroud)
主要在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");
}
Run Code Online (Sandbox Code Playgroud)
删除注释掉的流利的api语法可使应用程序正常工作。
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>
Run Code Online (Sandbox Code Playgroud)
提前致谢。
您需要将一个传递IConfigurationInterpreter给您的Windsor构造函数。更改:
var container = new WindsorContainer();
Run Code Online (Sandbox Code Playgroud)
至:
var container = new WindsorContainer(new XmlInterpreter());
Run Code Online (Sandbox Code Playgroud)
的XmlInterpreter(不带参数)会选择您的app.config / web.config配置。
有关使用的更多选项IConfigurationInterpreter,请参阅文档。