作者 NCoreCode
//new interface
public interface ITestClass
{
string Hello();
Task<int> ResultIntAsync();
Task ReturnAsync();
}
//new class inherit ITestClass
[JitInject]
public class TestClass: ITestClass
{
[JitAop]
public string Hello()
{
Console.WriteLine("Hello");
return "Hello";
}
public Task<int> ResultIntAsync()
{
Console.WriteLine("ResultIntAsync");
return Task.FromResult(100);
}
public Task ReturnAsync()
{
Console.WriteLine("ReturnAsync");
return Task.CompletedTask;
}
}
method "Hello" add default aop, but custom, add attribute inherit JitAopAttribute support async method and sync method
[AttributeUsage(AttributeTargets.Method)]
internal class TestJitAttribute : JitAopAttribute
{
public override void Before(MethodReflector method, object instance, params object[] param)
{
Console.WriteLine($"Before Name:{method.Name}");
}
public override void After(MethodReflector method, Exception exception, object instance, params object[] param)
{
Console.WriteLine($"After Name:{method.Name}");
}
public override Task BeforeAsync(MethodReflector method, object instance, params object[] param)
{
Console.WriteLine($"BeforeAsync Name:{method.Name}");
return Task.CompletedTask;
}
public override Task AfterAsync(MethodReflector method, Exception exception, object instance, params object[] param)
{
Console.WriteLine($"AfterAsync Name:{method.Name}");
return Task.CompletedTask;
}
}
[JitInject]
public class TestClass: ITestClass
{
[TestJit]
public string Hello()
{
Console.WriteLine("Hello");
return "Hello";
}
///...
}
public class TestActors : IAopActors
{
public object Execute(AopContext context)
{
throw new NotImplementedException();
}
public Task<TResult> ExecuteAsync<TResult>(AopContext context)
{
throw new NotImplementedException();
}
public Task InvokeAsync(AopContext context)
{
throw new NotImplementedException();
}
}
[AopActors(typeof(TestActors))]
public interface ITestClass
{
string Hello();
Task<int> ResultIntAsync();
Task ReturnAsync();
}
edit Startup.cs
public IServiceProvider ConfigureServices(IServiceCollection services)
{
//...
services.AddSingleton<ITestClass, TestClass>();
return services.BuilderJit();
}
edit Program.cs
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseServiceProviderFactory(new JitServiceProviderFactory()) //new
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
edit Startup.cs add Method
public void ConfigureContainer(JitAopBuilder builder)
{
builder.Add<ITestClass, TestClass>(ServiceLifetime.Singleton);
}