【问题标题】:Apply Aspect Oriented Programming to Web Service将面向方面的编程应用于 Web 服务
【发布时间】:2013-04-12 17:59:43
【问题描述】:

我正在寻找一种将 AOP 概念应用于 Web 服务的方法。创建一个普通的类很​​容易做到这一点。

MyClass 示例

通常你可以通过这种方式创建一个类的新实例

MyClass NewClass =  new MyClass();

要使用 AOP 创建这个类的实例及其周围的切面,您通常需要一个库来指定类类型和要应用的切面。

MyClass 示例

MyClass NewClass = MyProxyClassManager.CreateNewAOPClass(typeof(MyClass));

但是,当尝试为附加到您的 Web 服务的类时,这被证明是具有挑战性的。 我需要一种方法,而不是返回附加到 Web 服务的普通类,我可以返回基于 Web 服务类的代理类。

顺便说一句:我正在使用城堡项目应用 AOP。 http://www.castleproject.org/projects/dynamicproxy/

【问题讨论】:

    标签: c# .net web-services aop


    【解决方案1】:

    我不确定您的完整技术堆栈,但使用 WebAPI,您可以创建自定义 DelegatingHandler 来环绕服务调用。详情请见the official website

    示例处理程序:

    public class MessageHandler1 : DelegatingHandler
    {
        protected async override Task<HttpResponseMessage> SendAsync(
            HttpRequestMessage request, CancellationToken cancellationToken)
        {
            Debug.WriteLine("Process request");
            // Call the inner handler.
            var response = await base.SendAsync(request, cancellationToken);
            Debug.WriteLine("Process response");
            return response;
        }
    }
    

    处理程序注册

    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            config.MessageHandlers.Add(new MessageHandler1());
            // Other code not shown...
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2010-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-24
      • 2010-09-18
      • 2016-04-05
      • 2013-09-21
      • 1970-01-01
      相关资源
      最近更新 更多