使用 xml/app.config 配置温莎城堡

2024-02-04

我目前正在使用温莎城堡构建一个示例应用程序。座右铭是使用 xml/app.config 来打开/关闭方法拦截。我之前使用过 Fluent API,它很有魅力。下一步,我尝试用我的 xml 替换 Fluent API。

代码要点如下: 一个名为 RandomOperations 的类,具有两个虚拟方法。 实现 IInterceptor 的 LoggingAspect 类。 实现 IModelInterceptorsSelector 的 MyInterceptorsSelector 类 Program.cs 之前具有流畅的 api 语法,现在仅用于调用 RandomOperations 类的方法。 app.config 中有一个名为 的部分,其中具有注册组件的 xml 语法。

当我使用 Fluent api 时,我能够拦截方法调用,但无法使用 xml/app.config 注册来完成此操作。有人可以解释一下所遗漏的内容吗?

课程如下:

随机操作.cs

public class RandomOperations 
    {
        public virtual int MyRandomMethod(int x)
        {
            return x * x;
        }

        public virtual void Writer(string x)
        {
            Console.WriteLine(x);
        }
    }

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

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中

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");
        }

删除注释掉的 Fluent api 语法可以使应用程序正常工作。

应用程序配置

<?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 构造函数。改变:

var container = new WindsorContainer();

To:

var container = new WindsorContainer(new XmlInterpreter());

The XmlInterpreter(不带参数)将从 app.config/web.config 中提取配置。

有关使用的更多选项IConfigurationInterpreter,参见docs https://github.com/castleproject/Windsor/blob/master/docs/xml-registration-reference.md.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 xml/app.config 配置温莎城堡 的相关文章

随机推荐