Using windsor lock with interceptors and asp.net

I am trying to add logging with an aspect-oriented programming using windsor lock in plain asp.net, i.e. not MVC

I added a class that implements the IInterceptor interface and an attribute that inherits from the attribute.

public class LogAttribute : Attribute
{
    public Level LogLevel { get; set; }

    public LogAttribute(Level level)
    {
        LogLevel = level;
    }
}

public class LoggingInterceptor : IInterceptor
{

    public void Intercept(IInvocation invocation)
    {

        MethodInfo mi = invocation.Method;
        LogAttribute[] atts = (LogAttribute[])mi.GetCustomAttributes(typeof(LogAttribute), true);
        // if method not marked with InternalUseRestricted attribute, then pass on call
        if (atts.Length == 0)
        {
            invocation.Proceed();
        }
        else
        {
            ISeiLogger log = LoggerFactory.GetLogger(mi.DeclaringType.ToString());
            //assume only one logging attribute
            //log on entry
            log.LogEnter(atts[0].LogLevel);
            //allow code to continue
            invocation.Proceed();
            //log on exit
            log.LogExit(atts[0].LogLevel);
        }
    }
}

Now in global.asax.cs I have added the following:

public partial class Global : System.Web.HttpApplication, IoCProvider
{

    private void InitializeIoC()
    {
        container = new WindsorContainer();
        container.Install(new Sei.Aspect.AspectInstaller());
    }

    public IWindsorContainer Container
    {
        get { return container; }
    }

    private static Sei.Logging.ISeiLogger log;
    private IWindsorContainer container;

    public override void Init()
    {
        base.Init();

        InitializeIoC();
    }

and I created an installer class:

public class AspectInstaller : IWindsorInstaller
{
    public void Install(IWindsorContainer container, IConfigurationStore store)
    {
        //container.Register(AllTypes.FromAssembly(Assembly.GetExecutingAssembly()).BasedOn<IInterceptor>().Configure(component => component.LifeStyle.PerWebRequest));
        container.Register(Component.For<IInterceptor>().ImplementedBy<LoggingInterceptor>().LifeStyle.PerWebRequest);
        container.Register(Component.For<IInterceptor>().ImplementedBy<InternalUseRestrictedInterceptor>().LifeStyle.PerWebRequest);
        container.Register(Component.For<IInterceptor>().ImplementedBy<CachingInterceptor>().LifeStyle.PerWebRequest);
    }
}

Now I want to add an attribute to some arbitrary page code behind the class and some arbial virtual method, as in

[Log(Level.Info)]
    protected string Login(string username, string password)
    {
        DoSomething();
    }

, , . ( ) ? ? , , , .

+3
1

: .

: - , ASP.NET Web Forms, . , PageHandlerFactory IoC, , , .

, - runtime, DynamicProxy LinFu, . , PostSharp.

, , Windsor.

+1

Source: https://habr.com/ru/post/1787473/


All Articles