Skip to content

全局注册没有响应 #78

@QQ897878763

Description

@QQ897878763

Nuget下载的版本为v0.2.4

源码如下:

using AspectCore.Configuration;
using AspectCore.DynamicProxy;
using AspectCore.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AspectCoreDemo1
{
    class Program
    {
        static void Main(string[] args)
        {
            RegisterAop();
            TestAop test = new TestAop();
            test.CallValue("今天是周三");
            Console.ReadLine();         
        }

        private static void RegisterAop()
        {
            IServiceCollection services = new ServiceCollection();        
            services.AddDynamicProxy(config =>
            {
                config.Interceptors.AddServiced<ExceptionLogAttribute>(Predicates.ForNameSpace("AspectCoreDemo1.*")); //拦截所有AspectCoreDemo及其子命名空间下面的接口或类                
            });
        }
    }

    /// <summary>
    /// 记录异常日志
    /// </summary>
    public class ExceptionLogAttribute : AbstractInterceptorAttribute
    {
        public override Task Invoke(AspectContext context, AspectDelegate next)
        {
            try
            {
                Console.WriteLine("Before service call");
                return next(context);
            }
            catch (Exception ea)
            {
                Console.WriteLine($"出现异常!异常信息:{ea.Message}");
                throw ea;
            }
            finally
            {
                Console.WriteLine("After service call");
            }
        }
    }

    [ExceptionLog]
    public class TestAop
    {
        public virtual void CallValue(string value)
        {
            Console.WriteLine($"我就是CallValue({nameof(value)})执行的,value:{value}");
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions