• 利用Spring.NET实现WCF的AOP编程


        在WCF中,有时我们希望实现AOP编程,用于权限验证、日志记录等,而Spring.NET就是一种很好的实现工具。

        1、引用Spring.NET相关DLL

        下载Spring.NET包,并在WCF项目中引用相关DLL:

                                  

        2、添加WCF服务

        在WCF项目中添加相关WCF服务:

        契约代码:

    using System.ServiceModel;
    
    namespace MyWcfTest
    {
        [ServiceContract]
        public interface IWcfContract
        {
            [OperationContract]
            string GetData(string value);
        }
    }

        服务实现:

    namespace MyWcfTest
    {
        public class ImplementService : IWcfContract
        {
            public string GetData(string value)
            {
                return string.Format("你输入的是:{0}", value);
            }
        }
    }

        添加WcfServer.svc文件:

    <%@ ServiceHost Language="C#" Debug="true" Service="WcfServer" Factory="Spring.ServiceModel.Activation.ServiceHostFactory"  %>

        注意,这里添加了Spring.NET的相关Factory,同时Service变成了我们自定义的名字,它在稍后的配置文件中定义。

        3、配置Spring.NET

        添加Spring.NET配置文件MyObjects.xml,内容如下:

    <?xml version="1.0" encoding="utf-8" ?>
    <objects xmlns="http://www.springframework.net"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.net
            http://www.springframework.net/xsd/spring-objects.xsd">
    
      <object type="Spring.Aop.Framework.AutoProxy.TypeNameAutoProxyCreator, Spring.Aop">
        <property name="TypeNames">
          <list>
            <value>*Service*</value>
          </list>
        </property>
        <property name="InterceptorNames">
          <list>
            <value>aroundAdvice</value>
          </list>
        </property>
      </object>
    
      <object id="aroundAdvisor" type="Spring.Aop.Support.NameMatchMethodPointcutAdvisor, Spring.Aop">
        <property name="Advice" ref="aroundAdvice"/>
        <property name="MappedNames">
          <list>
            <value>Find*</value>
          </list>
        </property>
      </object>
    
      <object id="aroundAdvice" type="MyWcfTest.MyAroundAdvice, MyWcfTest"/>
      
      <object id="WcfServer" type="MyWcfTest.ImplementService, MyWcfTest" />
      
      <object id="htService1" type="MyWcfTest.Service1, MyWcfTest" />
    </objects>

        在WCF应用程序的配置文件Web.config中添加下列配置内容:

      <configSections>
        <sectionGroup name="spring">
          <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/>
          <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core"/>
        </sectionGroup>
      </configSections>
      <spring>
        <context>
          <resource uri="~/MyObjects.xml" />
          <resource uri="config://spring/objects" />
        </context>
        <objects xmlns="http://www.springframework.net" />
      </spring>

       4、注册Spring.NET上下文

        新增应用程序全局设置文件Global.asax,在其中的Application_Start方法中添加下面的代码,用以注册Spring.NET上下文。

            protected void Application_Start(object sender, EventArgs e)
            {
                ContextRegistry.GetContext();
            }

        由此配置完成,即可实现针对WCF的AOP编程。

  • 相关阅读:
    用Python打造一款文件搜索工具,所有功能自己定义!
    Python+Excel+Word一秒制作百份合同
    只需6行代码,Python将PPT转为Word!
    老板让我从几百个Excel中查找数据,我用Python一分钟搞定!
    爬虫遇到头疼的验证码?Python实战讲解弹窗处理和验证码识别
    SoftEther服务端配置
    SoftEther服务端安装
    nginx学习
    zookeeper安装
    prometheus监控之自动发现
  • 原文地址:https://www.cnblogs.com/huatao/p/4848275.html
Copyright © 2020-2023  润新知