mirror of https://github.com/abpframework/abp.git
committed by
GitHub
32 changed files with 496 additions and 134 deletions
@ -0,0 +1,21 @@ |
|||||
|
using System.Threading.Tasks; |
||||
|
using Castle.DynamicProxy; |
||||
|
using Volo.Abp.DynamicProxy; |
||||
|
|
||||
|
namespace Volo.Abp.Castle.DynamicProxy |
||||
|
{ |
||||
|
public class CastleAbpAsyncMethodInvocationAdapter : CastleAbpMethodInvocationAdapterBase, IAbpAsyncMethodInvocation |
||||
|
{ |
||||
|
public CastleAbpAsyncMethodInvocationAdapter(IInvocation invocation) |
||||
|
: base(invocation) |
||||
|
{ |
||||
|
|
||||
|
} |
||||
|
|
||||
|
public Task ProceedAsync() |
||||
|
{ |
||||
|
Invocation.Proceed(); |
||||
|
return (Task)Invocation.ReturnValue; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -1,36 +1,29 @@ |
|||||
using System; |
using System.Threading.Tasks; |
||||
using System.Reflection; |
|
||||
using Castle.DynamicProxy; |
using Castle.DynamicProxy; |
||||
using Volo.Abp.DynamicProxy; |
using Volo.Abp.DynamicProxy; |
||||
|
using Volo.Abp.Threading; |
||||
|
|
||||
namespace Volo.Abp.Castle.DynamicProxy |
namespace Volo.Abp.Castle.DynamicProxy |
||||
{ |
{ |
||||
public class CastleAbpMethodInvocationAdapter : IAbpMethodInvocation |
public class CastleAbpMethodInvocationAdapter : CastleAbpMethodInvocationAdapterBase, IAbpMethodInvocation |
||||
{ |
{ |
||||
public object[] Arguments => _invocation.Arguments; |
|
||||
|
|
||||
public Type[] GenericArguments => _invocation.GenericArguments; |
|
||||
|
|
||||
public object TargetObject => _invocation.InvocationTarget; |
|
||||
|
|
||||
public MethodInfo Method => _invocation.MethodInvocationTarget; |
|
||||
|
|
||||
public object ReturnValue |
|
||||
{ |
|
||||
get => _invocation.ReturnValue; |
|
||||
set => _invocation.ReturnValue = value; |
|
||||
} |
|
||||
|
|
||||
private readonly IInvocation _invocation; |
|
||||
|
|
||||
public CastleAbpMethodInvocationAdapter(IInvocation invocation) |
public CastleAbpMethodInvocationAdapter(IInvocation invocation) |
||||
|
: base(invocation) |
||||
{ |
{ |
||||
_invocation = invocation; |
|
||||
} |
} |
||||
|
|
||||
public void Proceed() |
public void Proceed() |
||||
{ |
{ |
||||
_invocation.Proceed(); |
if (Invocation.Method.IsAsync()) |
||||
|
{ |
||||
|
Invocation.Proceed(); |
||||
|
AsyncHelper.RunSync(() => (Task) Invocation.ReturnValue); |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
Invocation.Proceed(); |
||||
|
} |
||||
} |
} |
||||
} |
} |
||||
} |
} |
||||
@ -0,0 +1,31 @@ |
|||||
|
using System; |
||||
|
using System.Reflection; |
||||
|
using Castle.DynamicProxy; |
||||
|
using Volo.Abp.DynamicProxy; |
||||
|
|
||||
|
namespace Volo.Abp.Castle.DynamicProxy |
||||
|
{ |
||||
|
public abstract class CastleAbpMethodInvocationAdapterBase : IAbpMethodInvocationCore |
||||
|
{ |
||||
|
public object[] Arguments => Invocation.Arguments; |
||||
|
|
||||
|
public Type[] GenericArguments => Invocation.GenericArguments; |
||||
|
|
||||
|
public object TargetObject => Invocation.InvocationTarget; |
||||
|
|
||||
|
public MethodInfo Method => Invocation.MethodInvocationTarget; |
||||
|
|
||||
|
public object ReturnValue |
||||
|
{ |
||||
|
get => Invocation.ReturnValue; |
||||
|
set => Invocation.ReturnValue = value; |
||||
|
} |
||||
|
|
||||
|
protected IInvocation Invocation { get; } |
||||
|
|
||||
|
protected CastleAbpMethodInvocationAdapterBase(IInvocation invocation) |
||||
|
{ |
||||
|
Invocation = invocation; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,9 @@ |
|||||
|
using System.Threading.Tasks; |
||||
|
|
||||
|
namespace Volo.Abp.DynamicProxy |
||||
|
{ |
||||
|
public interface IAbpAsyncInterceptor |
||||
|
{ |
||||
|
Task InterceptAsync(IAbpAsyncMethodInvocation invocation); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,9 @@ |
|||||
|
using System.Threading.Tasks; |
||||
|
|
||||
|
namespace Volo.Abp.DynamicProxy |
||||
|
{ |
||||
|
public interface IAbpAsyncMethodInvocation: IAbpMethodInvocationCore |
||||
|
{ |
||||
|
Task ProceedAsync(); |
||||
|
} |
||||
|
} |
||||
@ -1,20 +1,7 @@ |
|||||
using System; |
|
||||
using System.Reflection; |
|
||||
|
|
||||
namespace Volo.Abp.DynamicProxy |
namespace Volo.Abp.DynamicProxy |
||||
{ |
{ |
||||
public interface IAbpMethodInvocation |
public interface IAbpMethodInvocation: IAbpMethodInvocationCore |
||||
{ |
{ |
||||
object[] Arguments { get; } |
|
||||
|
|
||||
Type[] GenericArguments { get; } |
|
||||
|
|
||||
object TargetObject { get; } |
|
||||
|
|
||||
MethodInfo Method { get; } |
|
||||
|
|
||||
object ReturnValue { get; set; } |
|
||||
|
|
||||
void Proceed(); |
void Proceed(); |
||||
} |
} |
||||
} |
} |
||||
@ -0,0 +1,18 @@ |
|||||
|
using System; |
||||
|
using System.Reflection; |
||||
|
|
||||
|
namespace Volo.Abp.DynamicProxy |
||||
|
{ |
||||
|
public interface IAbpMethodInvocationCore |
||||
|
{ |
||||
|
object[] Arguments { get; } |
||||
|
|
||||
|
Type[] GenericArguments { get; } |
||||
|
|
||||
|
object TargetObject { get; } |
||||
|
|
||||
|
MethodInfo Method { get; } |
||||
|
|
||||
|
object ReturnValue { get; set; } |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,9 @@ |
|||||
|
using System.Collections.Generic; |
||||
|
|
||||
|
namespace Volo.Abp.TestBase.Logging |
||||
|
{ |
||||
|
public interface ICanLogOnObject |
||||
|
{ |
||||
|
List<string> Logs { get; } |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,25 @@ |
|||||
|
<Project Sdk="Microsoft.NET.Sdk"> |
||||
|
|
||||
|
<PropertyGroup> |
||||
|
<TargetFramework>netcoreapp1.1</TargetFramework> |
||||
|
<AssemblyName>Volo.Abp.Autofac.Tests</AssemblyName> |
||||
|
<PackageId>Volo.Abp.Autofac.Tests</PackageId> |
||||
|
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles> |
||||
|
<RuntimeFrameworkVersion>1.1.1</RuntimeFrameworkVersion> |
||||
|
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute> |
||||
|
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute> |
||||
|
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute> |
||||
|
<RootNamespace /> |
||||
|
</PropertyGroup> |
||||
|
|
||||
|
<ItemGroup> |
||||
|
<ProjectReference Include="..\..\src\Volo.Abp.Autofac\Volo.Abp.Autofac.csproj" /> |
||||
|
<ProjectReference Include="..\AbpTestBase\AbpTestBase.csproj" /> |
||||
|
<ProjectReference Include="..\Volo.Abp.Castle.Core.Tests\Volo.Abp.Castle.Core.Tests.csproj" /> |
||||
|
</ItemGroup> |
||||
|
|
||||
|
<ItemGroup> |
||||
|
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" /> |
||||
|
</ItemGroup> |
||||
|
|
||||
|
</Project> |
||||
@ -0,0 +1,16 @@ |
|||||
|
using Microsoft.Extensions.DependencyInjection; |
||||
|
using Volo.Abp.Castle; |
||||
|
using Volo.Abp.Castle.DynamicProxy; |
||||
|
using Volo.Abp.Modularity; |
||||
|
|
||||
|
namespace Volo.Abp.Autofac |
||||
|
{ |
||||
|
[DependsOn(typeof(AbpAutofacModule), typeof(AbpCastleCoreTestModule))] |
||||
|
public class AutofacTestModule : AbpModule |
||||
|
{ |
||||
|
public override void ConfigureServices(IServiceCollection services) |
||||
|
{ |
||||
|
services.AddAssemblyOf<AbpAutofacModule>(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,44 @@ |
|||||
|
using System; |
||||
|
using System.Threading.Tasks; |
||||
|
using Microsoft.Extensions.DependencyInjection; |
||||
|
using Shouldly; |
||||
|
using Volo.Abp.Castle.DynamicProxy; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Volo.Abp.Autofac.Interception |
||||
|
{ |
||||
|
public class Autofac_Interception_Test : CastleInterceptionTestBase<AutofacTestModule> |
||||
|
{ |
||||
|
protected override void SetAbpApplicationCreationOptions(AbpApplicationCreationOptions options) |
||||
|
{ |
||||
|
options.UseAutofac(); |
||||
|
} |
||||
|
|
||||
|
protected override IServiceProvider CreateServiceProvider(IServiceCollection services) |
||||
|
{ |
||||
|
return services.BuildAutofacServiceProvider(); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task Should_Intercept_Async_Methods() |
||||
|
{ |
||||
|
//Arrange
|
||||
|
|
||||
|
var target = ServiceProvider.GetService<SimpleInterceptionTargetClass>(); |
||||
|
|
||||
|
//Act
|
||||
|
|
||||
|
var result = await target.GetValueAsync(); |
||||
|
|
||||
|
//Assert
|
||||
|
|
||||
|
result.ShouldBe(42); |
||||
|
target.Logs.Count.ShouldBe(5); |
||||
|
target.Logs[0].ShouldBe("SimpleInterceptor_BeforeInvocation"); |
||||
|
target.Logs[1].ShouldBe("EnterGetValueAsync"); |
||||
|
target.Logs[2].ShouldBe("MiddleGetValueAsync"); |
||||
|
target.Logs[3].ShouldBe("ExitGetValueAsync"); |
||||
|
target.Logs[4].ShouldBe("SimpleInterceptor_AfterInvocation"); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,24 @@ |
|||||
|
<Project Sdk="Microsoft.NET.Sdk"> |
||||
|
|
||||
|
<PropertyGroup> |
||||
|
<TargetFramework>netcoreapp1.1</TargetFramework> |
||||
|
<AssemblyName>Volo.Abp.Castle.Core.Tests</AssemblyName> |
||||
|
<PackageId>Volo.Abp.Castle.Core.Tests</PackageId> |
||||
|
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles> |
||||
|
<RuntimeFrameworkVersion>1.1.1</RuntimeFrameworkVersion> |
||||
|
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute> |
||||
|
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute> |
||||
|
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute> |
||||
|
<RootNamespace /> |
||||
|
</PropertyGroup> |
||||
|
|
||||
|
<ItemGroup> |
||||
|
<ProjectReference Include="..\..\src\Volo.Abp.Castle.Core\Volo.Abp.Castle.Core.csproj" /> |
||||
|
<ProjectReference Include="..\AbpTestBase\AbpTestBase.csproj" /> |
||||
|
</ItemGroup> |
||||
|
|
||||
|
<ItemGroup> |
||||
|
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" /> |
||||
|
</ItemGroup> |
||||
|
|
||||
|
</Project> |
||||
@ -0,0 +1,29 @@ |
|||||
|
using Microsoft.Extensions.DependencyInjection; |
||||
|
using Volo.Abp.Castle.DynamicProxy; |
||||
|
using Volo.Abp.Modularity; |
||||
|
|
||||
|
namespace Volo.Abp.Castle |
||||
|
{ |
||||
|
[DependsOn(typeof(AbpCastleCoreModule))] |
||||
|
public class AbpCastleCoreTestModule : AbpModule |
||||
|
{ |
||||
|
public override void PreConfigureServices(IServiceCollection services) |
||||
|
{ |
||||
|
services.OnServiceRegistred(RegisterTestInterceptors); |
||||
|
} |
||||
|
|
||||
|
public override void ConfigureServices(IServiceCollection services) |
||||
|
{ |
||||
|
services.AddAssemblyOf<AbpCastleCoreTestModule>(); |
||||
|
} |
||||
|
|
||||
|
private static void RegisterTestInterceptors(IOnServiceRegistredArgs registration) |
||||
|
{ |
||||
|
//TODO: Create an attribute to add interceptors!
|
||||
|
if (typeof(SimpleInterceptionTargetClass) == registration.ImplementationType) |
||||
|
{ |
||||
|
registration.Interceptors.Add<SimpleInterceptor>(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,34 @@ |
|||||
|
using System.Threading.Tasks; |
||||
|
using Microsoft.Extensions.DependencyInjection; |
||||
|
using Shouldly; |
||||
|
using Volo.Abp.Modularity; |
||||
|
using Volo.Abp.TestBase; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Volo.Abp.Castle.DynamicProxy |
||||
|
{ |
||||
|
//TODO: There is no Castle Dependency here.. We can move this base class directly to Volo.Abp.Tests
|
||||
|
public abstract class CastleInterceptionTestBase<TStartupModule> : AbpIntegratedTest<TStartupModule> |
||||
|
where TStartupModule : IAbpModule |
||||
|
{ |
||||
|
[Fact] |
||||
|
public void Should_Intercept_Sync_Methods() |
||||
|
{ |
||||
|
//Arrange
|
||||
|
|
||||
|
var target = ServiceProvider.GetService<SimpleInterceptionTargetClass>(); |
||||
|
|
||||
|
//Act
|
||||
|
|
||||
|
var result = target.GetValue(); |
||||
|
|
||||
|
//Assert
|
||||
|
|
||||
|
result.ShouldBe(42); |
||||
|
target.Logs.Count.ShouldBe(3); |
||||
|
target.Logs[0].ShouldBe("SimpleInterceptor_BeforeInvocation"); |
||||
|
target.Logs[1].ShouldBe("ExecutingGetValue"); |
||||
|
target.Logs[2].ShouldBe("SimpleInterceptor_AfterInvocation"); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,28 @@ |
|||||
|
using System.Collections.Generic; |
||||
|
using System.Threading.Tasks; |
||||
|
using Volo.Abp.TestBase.Logging; |
||||
|
using Volo.DependencyInjection; |
||||
|
|
||||
|
namespace Volo.Abp.Castle.DynamicProxy |
||||
|
{ |
||||
|
public class SimpleInterceptionTargetClass : ITransientDependency, ICanLogOnObject |
||||
|
{ |
||||
|
public List<string> Logs { get; } = new List<string>(); |
||||
|
|
||||
|
public virtual int GetValue() |
||||
|
{ |
||||
|
Logs.Add("ExecutingGetValue"); |
||||
|
return 42; |
||||
|
} |
||||
|
|
||||
|
public virtual async Task<int> GetValueAsync() |
||||
|
{ |
||||
|
Logs.Add("EnterGetValueAsync"); |
||||
|
await Task.Delay(1); |
||||
|
Logs.Add("MiddleGetValueAsync"); |
||||
|
await Task.Delay(1); |
||||
|
Logs.Add("ExitGetValueAsync"); |
||||
|
return 42; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,24 @@ |
|||||
|
using System.Threading.Tasks; |
||||
|
using Volo.Abp.DynamicProxy; |
||||
|
using Volo.Abp.TestBase.Logging; |
||||
|
using Volo.DependencyInjection; |
||||
|
|
||||
|
namespace Volo.Abp.Castle.DynamicProxy |
||||
|
{ |
||||
|
public class SimpleInterceptor : IAbpInterceptor, /*IAbpAsyncInterceptor,*/ ITransientDependency |
||||
|
{ |
||||
|
public void Intercept(IAbpMethodInvocation invocation) |
||||
|
{ |
||||
|
(invocation.TargetObject as ICanLogOnObject)?.Logs?.Add("SimpleInterceptor_BeforeInvocation"); |
||||
|
invocation.Proceed(); |
||||
|
(invocation.TargetObject as ICanLogOnObject)?.Logs?.Add("SimpleInterceptor_AfterInvocation"); |
||||
|
} |
||||
|
|
||||
|
//public async Task InterceptAsync(IAbpAsyncMethodInvocation invocation)
|
||||
|
//{
|
||||
|
// (invocation.TargetObject as ICanLogOnObject)?.Logs?.Add("SimpleInterceptor_BeforeInvocation");
|
||||
|
// await invocation.ProceedAsync();
|
||||
|
// (invocation.TargetObject as ICanLogOnObject)?.Logs?.Add("SimpleInterceptor_AfterInvocation");
|
||||
|
//}
|
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue