├── .gitattributes ├── .github └── workflows │ ├── build-ci.yml │ └── build-pr-ci.yml ├── .gitignore ├── AspectCore-Framework.sln ├── LICENSE ├── NuGet.config ├── README.md ├── benchmark ├── AspectCore.Core.Benchmark │ ├── AspectCore.Core.Benchmark.csproj │ ├── Benchmarks │ │ └── GetTaskResultBenchmarks.cs │ └── Program.cs └── AspectCore.Extensions.Reflection.Benchmark │ ├── AspectCore.Extensions.Reflection.Benchmark.csproj │ ├── Benchmarks │ ├── ConstructorReflectorBenchmarks.cs │ ├── CustomAttributeReflectorBenchmarks.cs │ ├── FieldReflectorBenchmarks.cs │ ├── MethodReflectorBenchmarks.cs │ └── PropertyReflectorBenchmarks.cs │ ├── Fakes │ ├── ConstructorFakes.cs │ ├── FieldFakes.cs │ ├── MethodFakes.cs │ └── PropertyFakes.cs │ └── Program.cs ├── build ├── aspectcore.snk ├── common.props ├── sign.props └── version.props ├── docs ├── 0.AOP简单介绍.md ├── 1.使用指南.md ├── injector.md ├── reflection-extensions.md └── reflection.md ├── sample ├── AspectCore.Extensions.AspectScope.Sample │ ├── AspectCore.Extensions.AspectScope.Sample.csproj │ └── Program.cs ├── AspectCore.Extensions.Autofac.Sample │ ├── AspectCore.Extensions.Autofac.Sample.csproj │ ├── MethodExecuteLoggerInterceptor.cs │ └── Program.cs ├── AspectCore.Extensions.DataAnnotations.Sample │ ├── AspectCore.Extensions.DataAnnotations.Sample.csproj │ ├── IAccountService.cs │ ├── Program.cs │ └── RegisterInput.cs └── AspectCore.Extensions.DependencyInjection.ConsoleSample │ ├── AspectCore.Extensions.DependencyInjection.ConsoleSample.csproj │ └── Program.cs ├── src ├── AspectCore.Abstractions │ ├── AspectCore.Abstractions.csproj │ ├── Configuration │ │ ├── AspectPredicate.cs │ │ ├── AspectValidationHandlerCollection.cs │ │ ├── IAspectConfiguration.cs │ │ ├── InterceptorCollection.cs │ │ ├── InterceptorFactory.cs │ │ └── NonAspectPredicateCollection.cs │ ├── DependencyInjection │ │ ├── ConfigurationExtensions.cs │ │ ├── Definitions │ │ │ ├── DelegateServiceDefinition.cs │ │ │ ├── InstanceServiceDefinition.cs │ │ │ ├── ServiceDefinition.cs │ │ │ └── TypeServiceDefinition.cs │ │ ├── FromServiceContextAttribute.cs │ │ ├── ILifetimeServiceContext.cs │ │ ├── IManyEnumerable.cs │ │ ├── IPropertyInjector.cs │ │ ├── IPropertyInjectorFactory.cs │ │ ├── IScopeResolverFactory.cs │ │ ├── IServiceContext.cs │ │ ├── IServiceResolveCallback.cs │ │ ├── IServiceResolver.cs │ │ ├── ITransientServiceAccessor.cs │ │ ├── Lifetime.cs │ │ ├── LifetimeServiceContextExtensions.cs │ │ ├── ServiceContextExtensions.cs │ │ ├── ServiceProviderExtensions.cs │ │ └── ServiceResolverExtensions.cs │ ├── DynamicProxy │ │ ├── AbstractInterceptor.cs │ │ ├── AbstractInterceptorAttribute.cs │ │ ├── AspectActivatorContext.cs │ │ ├── AspectContext.cs │ │ ├── AspectDelegate.cs │ │ ├── AspectInvalidCastException.cs │ │ ├── AspectInvalidOperationException.cs │ │ ├── AspectInvocationException.cs │ │ ├── AspectValidationContext.cs │ │ ├── AspectValidationDelegate.cs │ │ ├── AsyncAspectAttribute.cs │ │ ├── DelegateInterceptor.cs │ │ ├── DynamicallyAttribute.cs │ │ ├── IAdditionalInterceptorSelector.cs │ │ ├── IAspectActivator.cs │ │ ├── IAspectActivatorFactory.cs │ │ ├── IAspectBuilder.cs │ │ ├── IAspectBuilderFactory.cs │ │ ├── IAspectCaching.cs │ │ ├── IAspectCachingProvider.cs │ │ ├── IAspectContextFactory.cs │ │ ├── IAspectValidationHandler.cs │ │ ├── IAspectValidator.cs │ │ ├── IAspectValidatorBuilder.cs │ │ ├── IInterceptor.cs │ │ ├── IInterceptorCollector.cs │ │ ├── IInterceptorSelector.cs │ │ ├── IProxyGenerator.cs │ │ ├── IProxyTypeGenerator.cs │ │ ├── IServiceInstanceAccessor.cs │ │ ├── NonAspectAttribute.cs │ │ ├── Parameters │ │ │ ├── IParameterInterceptor.cs │ │ │ ├── IParameterInterceptorSelector.cs │ │ │ ├── Parameter.cs │ │ │ ├── ParameterAspectContext.cs │ │ │ ├── ParameterAspectDelegate.cs │ │ │ ├── ParameterCollection.cs │ │ │ ├── ParameterExtensions.cs │ │ │ ├── ParameterInterceptorAttribute.cs │ │ │ ├── ReturnParameter.cs │ │ │ └── ReturnParameterInterceptorAttribute.cs │ │ └── ServiceInterceptorAttribute.cs │ └── Properties │ │ └── AssemblyInfo.cs ├── AspectCore.Core │ ├── AspectCore.Core.csproj │ ├── Configuration │ │ ├── AspectConfiguration.cs │ │ ├── Extensions │ │ │ ├── AspectValidationHandlerCollectionExtensions.cs │ │ │ ├── InterceptorCollectionExtensions.cs │ │ │ ├── NonAspectsCollectionExtensions.cs │ │ │ └── StringExtensions.cs │ │ ├── InterceptorFactories │ │ │ ├── DelegateInterceptorFactory.cs │ │ │ ├── ServiceInterceptorFactory.cs │ │ │ └── TypeInterceptorFactory.cs │ │ └── Predicates.cs │ ├── DependencyInjection │ │ ├── ConstructorCallSiteResolver.cs │ │ ├── EnumerableServiceDefintion.cs │ │ ├── Extensions │ │ │ ├── LinkedListExtensions.cs │ │ │ ├── ServiceContainerBuildExtensions.cs │ │ │ └── ServiceDefinitionExtensions.cs │ │ ├── IServiceResolveCallbackProvider.cs │ │ ├── LifetimeServiceContext.cs │ │ ├── ManyEnumerable.cs │ │ ├── PropertyInjector.cs │ │ ├── PropertyInjectorCallback.cs │ │ ├── PropertyInjectorFactory.cs │ │ ├── PropertyResolver.cs │ │ ├── PropertyResolverSelector.cs │ │ ├── ProxyServiceDefinition.cs │ │ ├── ScopeResolverFactory.cs │ │ ├── ServiceCallSiteResolver.cs │ │ ├── ServiceContext.cs │ │ ├── ServiceResolver.cs │ │ ├── ServiceTable.cs │ │ ├── ServiceValidator.cs │ │ └── TransientServiceAccessor.cs │ ├── DynamicProxy │ │ ├── AspectActivator.cs │ │ ├── AspectActivatorFactory.cs │ │ ├── AspectBuilder.cs │ │ ├── AspectBuilderFactory.cs │ │ ├── AspectCaching.cs │ │ ├── AspectCachingProvider.cs │ │ ├── AspectContext.Runtime.cs │ │ ├── AspectContextFactory.cs │ │ ├── AspectValidator.cs │ │ ├── AspectValidatorBuilder.cs │ │ ├── AttributeAdditionalInterceptorSelector.cs │ │ ├── AttributeInterceptorSelector.cs │ │ ├── ConfigureInterceptorSelector.cs │ │ ├── Extensions │ │ │ ├── AspectContextRuntimeExtensions.cs │ │ │ ├── AspectValidatorExtensions.cs │ │ │ └── ProxyGeneratorExtensions.cs │ │ ├── InterceptorCollector.cs │ │ ├── InterceptorSelectorEqualityComparer.cs │ │ ├── Parameters │ │ │ ├── EnableParameterAspectExtensions.cs │ │ │ ├── EnableParameterAspectInterceptor.cs │ │ │ ├── NotNullAttribute.cs │ │ │ ├── ParameterAspectInvoker.cs │ │ │ └── ParameterInterceptorSelector.cs │ │ ├── ProxyGenerator.cs │ │ ├── ProxyGeneratorBuilder.cs │ │ ├── ProxyTypeGenerator.cs │ │ └── ValidationHandlers │ │ │ ├── AttributeAspectValidationHandler.cs │ │ │ ├── CacheAspectValidationHandler.cs │ │ │ ├── ConfigureAspectValidationHandler.cs │ │ │ └── OverwriteAspectValidationHandler.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ └── Utils │ │ ├── ActivatorUtils.cs │ │ ├── ArrayUtils.cs │ │ ├── MethodUtils.cs │ │ ├── NoSyncContextScope.cs │ │ ├── PropertyInjectionUtils.cs │ │ ├── ProxyGeneratorUtils.cs │ │ ├── ReflectionUtils.cs │ │ └── TaskUtils.cs ├── AspectCore.Extensions.AspNetCore │ ├── AspectCore.Extensions.AspNetCore.csproj │ ├── Extensions │ │ ├── AspectContextExtensions.cs │ │ ├── ConfigurationExtensions.cs │ │ └── ServiceCollectionExtensions.cs │ ├── Filters │ │ └── ModelStateAdapterAttribute.cs │ ├── Http │ │ ├── ExecutionContextHelper.cs │ │ └── HttpContextFactory.cs │ └── Interceptors │ │ ├── MethodExecuteLoggerInterceptor.cs │ │ └── ModelBindingAdapterAttribute.cs ├── AspectCore.Extensions.AspectScope │ ├── AspectCore.Extensions.AspectScope.csproj │ ├── IAspectScheduler.cs │ ├── IScopeInterceptor.cs │ ├── SchedulerHelpers.cs │ ├── Scope.cs │ ├── ScopeAspectBuilderFactory.cs │ ├── ScopeAspectContext.cs │ ├── ScopeAspectContextFactory.cs │ ├── ScopeAspectScheduler.cs │ ├── ScopeInterceptorAttribute.cs │ └── ServiceContainerExtensions.cs ├── AspectCore.Extensions.Autofac │ ├── ActivationResolveMiddleware.cs │ ├── AspectCore.Extensions.Autofac.csproj │ ├── AutofacScopeResolverFactory.cs │ ├── AutofacServiceResolver.cs │ ├── ContainerBuilderExtensions.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ └── RegistrationExtensions.cs ├── AspectCore.Extensions.Configuration │ ├── AspectCore.Extensions.Configuration.csproj │ ├── Attributes │ │ ├── ConfigurationBindingAttribute.cs │ │ ├── ConfigurationMetadataAttribute.cs │ │ └── ConfigurationValueAttribute.cs │ ├── ConfigurationBindResolveCallback.cs │ ├── ConfigurationBindType.cs │ ├── IConfigurationMetadataProvider.cs │ └── ServiceContainerExtensions.cs ├── AspectCore.Extensions.DataAnnotations │ ├── AnnotationDataValidator.cs │ ├── AnnotationPropertyValidator.cs │ ├── AspectCore.Extensions.DataAnnotations.csproj │ └── ServiceContainerExtensions.cs ├── AspectCore.Extensions.DataValidation │ ├── AspectContextExtensions.cs │ ├── AspectCore.Extensions.DataValidation.csproj │ ├── DataMetaData.cs │ ├── DataState.cs │ ├── DataStateFactory.cs │ ├── DataValidationContext.cs │ ├── DataValidationError.cs │ ├── DataValidationErrorCollection.cs │ ├── DataValidationInterceptorAttribute.cs │ ├── DataValidationState.cs │ ├── IDataState.cs │ ├── IDataStateFactory.cs │ ├── IDataStateProvider.cs │ ├── IDataValidator.cs │ ├── IPropertyValidator.cs │ ├── PropertyMetaData.cs │ ├── PropertyValidationContext.cs │ └── SkipValidationAttribute.cs ├── AspectCore.Extensions.DependencyInjection │ ├── AspectCore.Extensions.DependencyInjection.csproj │ ├── DynamicProxyServiceProviderFactory.cs │ ├── MsdiScopeResolverFactory.cs │ ├── MsdiServiceResolver.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ ├── ServiceCollectionAddExtensions.cs │ ├── ServiceCollectionBuildExtensions.cs │ ├── ServiceCollectionExtensions.cs │ ├── ServiceCollectionToServiceContextExtensions.cs │ ├── ServiceContextProviderFactory.cs │ ├── ServiceScope.cs │ ├── ServiceScopeFactory.cs │ ├── ServiceValidator.cs │ └── SupportRequiredService.cs ├── AspectCore.Extensions.Hosting │ ├── AspectCore.Extensions.Hosting.csproj │ └── HostBuilderExtensions.cs ├── AspectCore.Extensions.LightInject │ ├── AspectCore.Extensions.LightInject.csproj │ ├── ContainerBuilderExtensions.cs │ ├── LightInjectExtensions.cs │ ├── LightInjectScopeResolverFactory.cs │ └── LightInjectServiceResolver.cs ├── AspectCore.Extensions.Reflection │ ├── AspectCore.Extensions.Reflection.csproj │ ├── CallOptions.cs │ ├── ConstructorReflector.OpenGeneric.cs │ ├── ConstructorReflector.cs │ ├── CustomAttributeReflector.cs │ ├── Emit │ │ ├── ILGeneratorExtensions.cs │ │ └── IndexedLocalBuilder.cs │ ├── Extensions │ │ ├── CustomAttributeExtensions.cs │ │ ├── InternalExtensions.cs │ │ ├── MethodExtensions.cs │ │ ├── ParameterInfoExtensions.cs │ │ ├── ReflectorExtensions.cs │ │ └── TypeExtensions.cs │ ├── Factories │ │ ├── ConstructorReflector.Factory.cs │ │ ├── CustomAttributeReflector.Factory.cs │ │ ├── FieldReflector.Factory.cs │ │ ├── MethodReflector.Factory.cs │ │ ├── ParameterReflector.Factory.cs │ │ ├── PropertyReflector.Factory.cs │ │ └── TypeReflector.Factory.cs │ ├── FieldReflector.Enum.cs │ ├── FieldReflector.OpenGeneric.cs │ ├── FieldReflector.Static.cs │ ├── FieldReflector.cs │ ├── ICustomAttributeReflectorProvider.cs │ ├── IParameterReflectorProvider.cs │ ├── Internals │ │ ├── ConstantsUtils.cs │ │ ├── ReflectorUtils.cs │ │ └── TypeInfoUtils.cs │ ├── MemberReflector.CustomAttribute.cs │ ├── MemberReflector.cs │ ├── MethodReflector.Call.cs │ ├── MethodReflector.DisplayName.cs │ ├── MethodReflector.OpenGeneric.cs │ ├── MethodReflector.Static.cs │ ├── MethodReflector.cs │ ├── MethodSignature.cs │ ├── Pair.cs │ ├── ParameterReflector.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ ├── PropertyReflector.Call.cs │ ├── PropertyReflector.OpenGeneric.cs │ ├── PropertyReflector.Static.cs │ ├── PropertyReflector.cs │ └── TypeReflector.cs └── AspectCore.Extensions.Windsor │ ├── AspectCore.Extensions.Windsor.csproj │ ├── AspectCoreFacility.cs │ ├── CompatibleCollectionResolver.cs │ ├── DynamicProxyInterceptor.cs │ ├── FacilityExtensions.cs │ ├── InterceptUtils.cs │ ├── WindsorAspectBuilderFactory.cs │ └── WindsorServiceResolver.cs └── tests ├── AspectCore.Extensions.Autofac.Test ├── AdditionalInterceptorSelectorTests.cs ├── AspectCore.Extensions.Autofac.Test.csproj ├── Fakes │ ├── CacheInterceptorAttribute.cs │ ├── Controller.cs │ ├── FakeServiceWithOut.cs │ ├── IController.cs │ ├── IService.cs │ ├── Model.cs │ └── Service.cs ├── Issues │ ├── AddDelegateThenUseInterceptorTests.cs │ ├── PropertiesAutowiredTests.cs │ └── PropertyInjectorProxyWithVirtualTests.cs ├── NonAspectTest.cs ├── Properties │ └── AssemblyInfo.cs ├── RegistrationExtensionsTests.cs └── SpecificationTests.cs ├── AspectCore.Extensions.Configuration.Tests ├── AspectCore.Extensions.Configuration.Tests.csproj ├── ConfigurationBindingTest.cs └── ConfigurationValueTest.cs ├── AspectCore.Extensions.DependencyInjection.Test ├── AdditionalInterceptorSelectorTests.cs ├── AspectCore.Extensions.DependencyInjection.Test.csproj ├── Issues │ ├── ConstructorSelectTests.cs │ ├── InterceptorAttributeWithArrayMemberTests.cs │ ├── ParamWithInTests.cs │ └── ValueTupleMorethan7ElementsTests.cs ├── KeyedServiceTests.cs ├── Properties │ └── AssemblyInfo.cs ├── SpecificationTests.cs ├── UseMicrosoftDISpecificationTests.cs └── Use_Built_In_ContainerSpecificationTests.cs ├── AspectCore.Extensions.Hosting.Tests ├── AspectCore.Extensions.Hosting.Tests.csproj ├── FakeClasses.cs └── HostBuilderExtensionsTests.cs ├── AspectCore.Extensions.LightInject.Test ├── AdditionalInterceptorSelectorTests.cs ├── AspectCore.Extensions.LightInject.Test.csproj ├── AsyncIncreamentAttribute.cs ├── AsyncInterceptorTests.cs ├── AwaitBeforeInvokeNextTests.cs ├── DisposeTests.cs ├── ExcuteTimesTests.cs ├── Fakes │ ├── CacheInterceptorAttribute.cs │ ├── Controller.cs │ ├── IController.cs │ ├── IService.cs │ ├── Model.cs │ └── Service.cs ├── NonAspectTest.cs ├── RegistrationExtensionsTests.cs ├── RegistryTests.cs └── ScopeTests.cs ├── AspectCore.Extensions.Reflection.Test ├── AspectCore.Extensions.Reflection.Test.csproj ├── ConstructorReflectorTest.cs ├── CustomAttributeExtensionsTests.cs ├── CustomAttributeReflectorTests.cs ├── FieldReflectorTests.cs ├── MethodReflectorTests.cs ├── MethodSignatureTest.cs ├── Properties │ └── AssemblyInfo.cs ├── PropertyReflectorTests.cs ├── TypeReflectorTests.cs └── VisibleTests.cs ├── AspectCore.Extensions.Windsor.Test ├── AspectCore.Extensions.Windsor.Test.csproj ├── AsyncInterceptorTests.cs ├── AwaitBeforeInvokeNextTests.cs ├── ExcuteTimesTests.cs ├── Fakes │ ├── CacheInterceptorAttribute.cs │ ├── CacheService.cs │ ├── Controller.cs │ ├── ICacheService.cs │ ├── IController.cs │ └── Model.cs ├── RegistrationExtensionsTests.cs └── ScopedServiceTests.cs └── AspectCore.Tests ├── AspectCore.Tests.csproj ├── Classes.cs ├── DependencyInjection └── OptionalArgumentsTests.cs ├── DynamicProxy ├── AdditionalInterceptorSelectorTests.cs ├── AspectInvocationTest.cs ├── AsyncAspectTests.cs ├── ClassProxyTest.cs ├── ConfigureInterceptorSelectorTest.cs ├── DefineAttributesTests.cs ├── DynamicProxyTestBase.cs ├── ExplicitImplementationTest.cs ├── ExplicitMethodTests.cs ├── InheritedTest.cs ├── InterceptorAttributeWithArrayMemberTests.cs ├── InterceptorPropertyInjectionTest.cs ├── NonAspectTest.cs ├── OpenClosedGenericsTests.cs ├── OpenGenericMethodTests.cs ├── OptionalNonNullableMethodParameterTests.cs ├── OptionalNullableMethodParameterTests.cs ├── ParameterInjectionTest.cs ├── PredicatesTests.cs ├── ProxyGeneratorTest.cs ├── RefParameterTests.cs ├── ThrowExceptionDirectlyTests.cs ├── ThrowExceptionTests.cs └── UnwrapAsyncReturnValueTests.cs ├── Injector ├── AsyncBlockTest.cs ├── ConstructorInjectionTest.cs ├── EnumerableTest.cs ├── GenericTest.cs ├── InjectorTestBase.cs ├── InternalServiceTest.cs ├── ManyEnumerableTest.cs ├── OpenClosedGenericsTests.cs ├── PropertyInjectionTest.cs ├── ScopedTest.cs ├── SingletonTest.cs ├── TransientServiceAccessorTest.cs └── TransientTest.cs ├── Integrate ├── Container_Built_In_ProxyTests.cs ├── IntegrateTestBase.cs └── ServiceInterceptorTests.cs └── Issues └── DynamicProxy ├── InterfaceDefaultMethodsShouldBeUsedTests.cs └── NullableEnumWithDefaultValueTests.cs /.github/workflows/build-pr-ci.yml: -------------------------------------------------------------------------------- 1 | name: Build for PR 2 | on: 3 | pull_request: 4 | 5 | jobs: 6 | build-and-test: 7 | runs-on: windows-latest 8 | steps: 9 | - name: Checkout 10 | uses: actions/checkout@v4 11 | with: 12 | fetch-depth: 0 13 | - name: Setup .NET Core 14 | uses: actions/setup-dotnet@v4 15 | with: # NOTE: we don't need to install 6.x, 8.x and 9.x cause they are included in windows-latest(windows-2022) image. 16 | dotnet-version: 7.x 17 | 18 | - name: Build Reason 19 | run: "echo ref: ${{github.ref}} event: ${{github.event_name}}" 20 | 21 | - name: Build 22 | shell: bash 23 | run: | 24 | for project in $(find ./src -name "*.csproj"); do 25 | dotnet build --configuration Release $project 26 | done 27 | 28 | - name: Run Tests 29 | shell: bash 30 | run: | 31 | for project in $(find ./tests -name "*.csproj"); do 32 | dotnet test --configuration Release $project 33 | done 34 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 AspectCore Project 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /NuGet.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /benchmark/AspectCore.Core.Benchmark/AspectCore.Core.Benchmark.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Exe 5 | net9.0;net8.0;net7.0;net6.0 6 | true 7 | true 8 | snupkg 9 | 9.0 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /benchmark/AspectCore.Core.Benchmark/Program.cs: -------------------------------------------------------------------------------- 1 | using AspectCore.Core.Benchmark.Benchmarks; 2 | using BenchmarkDotNet.Running; 3 | 4 | namespace AspectCore.Core.Benchmark 5 | { 6 | public static class Program 7 | { 8 | public static void Main(string[] args) 9 | { 10 | BenchmarkRunner.Run(); 11 | } 12 | } 13 | } -------------------------------------------------------------------------------- /benchmark/AspectCore.Extensions.Reflection.Benchmark/AspectCore.Extensions.Reflection.Benchmark.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Exe 5 | net9.0;net8.0;net7.0;net6.0 6 | true 7 | true 8 | snupkg 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /benchmark/AspectCore.Extensions.Reflection.Benchmark/Benchmarks/ConstructorReflectorBenchmarks.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Reflection; 3 | using AspectCore.Extensions.Reflection.Benchmark.Fakes; 4 | using BenchmarkDotNet.Attributes; 5 | 6 | namespace AspectCore.Extensions.Reflection.Benchmark.Benchmarks 7 | { 8 | [AllStatisticsColumn] 9 | [MemoryDiagnoser] 10 | public class ConstructorReflectorBenchmarks 11 | { 12 | private readonly ConstructorInfo _constructorInfo; 13 | private readonly ConstructorReflector _reflector; 14 | 15 | private readonly static object[] args = new object[0]; 16 | 17 | public ConstructorReflectorBenchmarks() 18 | { 19 | _constructorInfo = typeof(ConstructorFakes).GetTypeInfo().GetConstructor(new Type[0]); 20 | _reflector = _constructorInfo.GetReflector(); 21 | } 22 | 23 | [Benchmark] 24 | public void Reflection() 25 | { 26 | _constructorInfo.Invoke(args); 27 | } 28 | 29 | [Benchmark] 30 | public void Reflector() 31 | { 32 | _reflector.Invoke(); 33 | } 34 | 35 | [Benchmark] 36 | public void New() 37 | { 38 | var result = new ConstructorFakes(); 39 | } 40 | } 41 | } -------------------------------------------------------------------------------- /benchmark/AspectCore.Extensions.Reflection.Benchmark/Fakes/ConstructorFakes.cs: -------------------------------------------------------------------------------- 1 | using System.Runtime.CompilerServices; 2 | 3 | namespace AspectCore.Extensions.Reflection.Benchmark.Fakes 4 | { 5 | public class ConstructorFakes 6 | { 7 | public string Name { get; set; } 8 | 9 | [MethodImpl(MethodImplOptions.NoInlining)] 10 | public ConstructorFakes() 11 | { 12 | //Name = "Nonparametric constructor"; 13 | } 14 | 15 | [MethodImpl(MethodImplOptions.NoInlining)] 16 | public ConstructorFakes(string name) 17 | { 18 | Name = "Parametric constructor. param : " + name; 19 | } 20 | 21 | [MethodImpl(MethodImplOptions.NoInlining)] 22 | public ConstructorFakes(ref string name, ref ConstructorFakes fakes) 23 | { 24 | name = Name = "Parametric constructor with ref param."; 25 | fakes = new ConstructorFakes(); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /benchmark/AspectCore.Extensions.Reflection.Benchmark/Fakes/FieldFakes.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace AspectCore.Extensions.Reflection.Benchmark.Fakes 8 | { 9 | public class FieldFakes 10 | { 11 | public static string StaticFiled; 12 | 13 | public string InstanceField; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /benchmark/AspectCore.Extensions.Reflection.Benchmark/Fakes/MethodFakes.cs: -------------------------------------------------------------------------------- 1 | using System.Runtime.CompilerServices; 2 | 3 | namespace AspectCore.Extensions.Reflection.Benchmark.Fakes 4 | { 5 | public class MethodFakes 6 | { 7 | [MethodImpl(MethodImplOptions.NoInlining)] 8 | public object Call() => null; 9 | [MethodImpl(MethodImplOptions.NoInlining)] 10 | public static object StaticCall() => null; 11 | [MethodImpl(MethodImplOptions.NoInlining)] 12 | public virtual object CallVirt() => null; 13 | } 14 | } -------------------------------------------------------------------------------- /benchmark/AspectCore.Extensions.Reflection.Benchmark/Fakes/PropertyFakes.cs: -------------------------------------------------------------------------------- 1 | using System.Runtime.CompilerServices; 2 | 3 | namespace AspectCore.Extensions.Reflection.Benchmark.Fakes 4 | { 5 | public class PropertyFakes 6 | { 7 | public static string StaticProperty { [MethodImpl(MethodImplOptions.NoInlining)]get; [MethodImpl(MethodImplOptions.NoInlining)]set; } 8 | 9 | public string InstanceProperty { [MethodImpl(MethodImplOptions.NoInlining)]get; [MethodImpl(MethodImplOptions.NoInlining)]set; } 10 | } 11 | } -------------------------------------------------------------------------------- /benchmark/AspectCore.Extensions.Reflection.Benchmark/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using AspectCore.Extensions.Reflection.Benchmark.Benchmarks; 3 | using BenchmarkDotNet.Running; 4 | 5 | namespace AspectCore.Extensions.Reflection.Benchmark 6 | { 7 | static class Program 8 | { 9 | static void Main(string[] args) 10 | { 11 | BenchmarkRunner.Run(); 12 | BenchmarkRunner.Run(); 13 | BenchmarkRunner.Run(); 14 | BenchmarkRunner.Run(); 15 | BenchmarkRunner.Run(); 16 | } 17 | } 18 | } -------------------------------------------------------------------------------- /build/aspectcore.snk: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dotnetcore/AspectCore-Framework/6a37bcc4207badbd1c183641fe5a04673088338f/build/aspectcore.snk -------------------------------------------------------------------------------- /build/common.props: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Lemon 6 | AspectCore Framework 7 | https://avatars1.githubusercontent.com/u/19426425?v=3&s=200 8 | https://github.com/dotnetcore/AspectCore-Framework 9 | https://github.com/dotnetcore/AspectCore-Framework/blob/dev/LICENSE 10 | git 11 | https://github.com/dotnetcore/AspectCore-Framework 12 | false 13 | false 14 | false 15 | False 16 | False 17 | 18 | -------------------------------------------------------------------------------- /build/sign.props: -------------------------------------------------------------------------------- 1 | 2 | 3 | True 4 | $(MSBuildThisFileDirectory)aspectcore.snk 5 | False 6 | 7 | -------------------------------------------------------------------------------- /build/version.props: -------------------------------------------------------------------------------- 1 | 2 | 3 | 2 4 | 4 5 | 0 6 | 7 | $(VersionMajor).$(VersionMinor).$(VersionPatch) 8 | 9 | 10 | -------------------------------------------------------------------------------- /docs/reflection.md: -------------------------------------------------------------------------------- 1 | 在从零实现AOP的过程中,难免会需要大量反射相关的操作,虽然在.net 4.5+/.net core中反射的性能有了大幅的优化,但为了追求极致性能,自己实现了部分反射的替代方案,包括构造器调用、方法调用、字段读写,属性读写和特性读取。在重构时,把反射扩展操作封装到单独的项目中,以此方便自己和大家使用。[获取AspectCore.Extension.Reflection](https://github.com/dotnetcore/AspectCore-Framework/blob/master/docs/reflection-extensions.md) -------------------------------------------------------------------------------- /sample/AspectCore.Extensions.AspectScope.Sample/AspectCore.Extensions.AspectScope.Sample.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp2.0 6 | true 7 | true 8 | snupkg 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /sample/AspectCore.Extensions.Autofac.Sample/AspectCore.Extensions.Autofac.Sample.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Exe 5 | net9.0;net8.0;net7.0;net6.0 6 | true 7 | true 8 | snupkg 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /sample/AspectCore.Extensions.Autofac.Sample/MethodExecuteLoggerInterceptor.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Diagnostics; 3 | using System.Threading.Tasks; 4 | using AspectCore.DynamicProxy; 5 | 6 | namespace AspectCore.Extensions.Autofac.Sample 7 | { 8 | public class MethodExecuteLoggerInterceptor : AbstractInterceptor 9 | { 10 | public override async Task Invoke(AspectContext context, AspectDelegate next) 11 | { 12 | var stopwatch = Stopwatch.StartNew(); 13 | await next(context); 14 | stopwatch.Stop(); 15 | Console.WriteLine("Executed method {0}.{1}.{2} ({3}) in {4}ms", 16 | context.ImplementationMethod.DeclaringType.Namespace, 17 | context.ImplementationMethod.DeclaringType.Name, 18 | context.ImplementationMethod.Name, 19 | context.ImplementationMethod.DeclaringType.Assembly.GetName().Name, 20 | stopwatch.ElapsedMilliseconds 21 | ); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /sample/AspectCore.Extensions.DataAnnotations.Sample/AspectCore.Extensions.DataAnnotations.Sample.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp2.0 6 | DataAnnotations.Sample 7 | true 8 | true 9 | snupkg 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /sample/AspectCore.Extensions.DataAnnotations.Sample/IAccountService.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using AspectCore.Extensions.DataValidation; 5 | 6 | namespace DataAnnotations.Sample 7 | { 8 | public interface IAccountService 9 | { 10 | void Register(RegisterInput input); 11 | string TestString(string a); 12 | } 13 | 14 | public class AccountService : IAccountService 15 | { 16 | public IDataState DataState { get; set; } 17 | public string TestString(string str) 18 | { 19 | return str; 20 | } 21 | public void Register(RegisterInput input) 22 | { 23 | if (DataState.IsValid) 24 | { 25 | //验证通过 26 | Console.WriteLine("register.. name:{0},email:{1}", input.Name, input.Email); 27 | } 28 | 29 | if (!DataState.IsValid) 30 | { 31 | //验证失败 32 | foreach(var error in DataState.Errors) 33 | { 34 | Console.WriteLine("error.. key:{0},message:{1}", error.Key, error.ErrorMessage); 35 | } 36 | } 37 | Console.WriteLine(); 38 | } 39 | } 40 | } -------------------------------------------------------------------------------- /sample/AspectCore.Extensions.DataAnnotations.Sample/RegisterInput.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.ComponentModel.DataAnnotations; 3 | 4 | namespace DataAnnotations.Sample 5 | { 6 | public class RegisterInput 7 | { 8 | [Required] 9 | public string Name { get; set; } 10 | 11 | [Required] 12 | [EmailAddress] 13 | public string Email { get; set; } 14 | 15 | [Required] 16 | [StringLength(18, MinimumLength = 6)] 17 | public string Password { get; set; } 18 | } 19 | } -------------------------------------------------------------------------------- /sample/AspectCore.Extensions.DependencyInjection.ConsoleSample/AspectCore.Extensions.DependencyInjection.ConsoleSample.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | Exe 4 | net9.0;net8.0;net7.0;net6.0 5 | true 6 | true 7 | snupkg 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /src/AspectCore.Abstractions/Configuration/AspectPredicate.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | 3 | namespace AspectCore.Configuration 4 | { 5 | public delegate bool AspectPredicate(MethodInfo method); 6 | } 7 | -------------------------------------------------------------------------------- /src/AspectCore.Abstractions/Configuration/IAspectConfiguration.cs: -------------------------------------------------------------------------------- 1 | using AspectCore.DynamicProxy; 2 | using AspectCore.DependencyInjection; 3 | 4 | namespace AspectCore.Configuration 5 | { 6 | [NonAspect] 7 | public interface IAspectConfiguration 8 | { 9 | AspectValidationHandlerCollection ValidationHandlers { get; } 10 | 11 | InterceptorCollection Interceptors { get; } 12 | 13 | NonAspectPredicateCollection NonAspectPredicates { get; } 14 | 15 | bool ThrowAspectException { get; set; } 16 | } 17 | } -------------------------------------------------------------------------------- /src/AspectCore.Abstractions/Configuration/InterceptorCollection.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | 5 | namespace AspectCore.Configuration 6 | { 7 | public sealed class InterceptorCollection : IEnumerable 8 | { 9 | private readonly ICollection _collection = new List(); 10 | 11 | public InterceptorCollection Add(InterceptorFactory interceptorFactory) 12 | { 13 | if (interceptorFactory == null) 14 | { 15 | throw new ArgumentNullException(nameof(interceptorFactory)); 16 | } 17 | _collection.Add(interceptorFactory); 18 | return this; 19 | } 20 | 21 | public int Count => _collection.Count; 22 | 23 | public IEnumerator GetEnumerator() 24 | { 25 | return _collection.GetEnumerator(); 26 | } 27 | 28 | IEnumerator IEnumerable.GetEnumerator() 29 | { 30 | return GetEnumerator(); 31 | } 32 | } 33 | } -------------------------------------------------------------------------------- /src/AspectCore.Abstractions/Configuration/InterceptorFactory.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Reflection; 3 | using AspectCore.DynamicProxy; 4 | 5 | namespace AspectCore.Configuration 6 | { 7 | public abstract class InterceptorFactory 8 | { 9 | private static readonly AspectPredicate[] EmptyPredicates = new AspectPredicate[0]; 10 | private readonly AspectPredicate[] _predicates; 11 | 12 | public AspectPredicate[] Predicates 13 | { 14 | get 15 | { 16 | return _predicates; 17 | } 18 | } 19 | 20 | public InterceptorFactory(params AspectPredicate[] predicates) 21 | { 22 | _predicates = predicates ?? EmptyPredicates; 23 | } 24 | 25 | public bool CanCreated(MethodInfo method) 26 | { 27 | if (_predicates.Length == 0) 28 | { 29 | return true; 30 | } 31 | foreach (var predicate in _predicates) 32 | { 33 | if (predicate(method)) return true; 34 | } 35 | return false; 36 | } 37 | 38 | public abstract IInterceptor CreateInstance(IServiceProvider serviceProvider); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/AspectCore.Abstractions/Configuration/NonAspectPredicateCollection.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | 5 | namespace AspectCore.Configuration 6 | { 7 | public sealed class NonAspectPredicateCollection : IEnumerable 8 | { 9 | private readonly ICollection _collection = new List(); 10 | 11 | public NonAspectPredicateCollection Add(AspectPredicate interceptorFactory) 12 | { 13 | _collection.Add(interceptorFactory); 14 | return this; 15 | } 16 | 17 | public int Count => _collection.Count; 18 | 19 | public IEnumerator GetEnumerator() 20 | { 21 | return _collection.GetEnumerator(); 22 | } 23 | 24 | IEnumerator IEnumerable.GetEnumerator() 25 | { 26 | return GetEnumerator(); 27 | } 28 | } 29 | } -------------------------------------------------------------------------------- /src/AspectCore.Abstractions/DependencyInjection/ConfigurationExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using AspectCore.Configuration; 3 | 4 | namespace AspectCore.DependencyInjection 5 | { 6 | public static class ConfigurationExtensions 7 | { 8 | public static IServiceContext Configure(this IServiceContext serviceContext, Action configure) 9 | { 10 | if (serviceContext == null) 11 | { 12 | throw new ArgumentNullException(nameof(serviceContext)); 13 | } 14 | configure?.Invoke(serviceContext.Configuration); 15 | return serviceContext; 16 | } 17 | } 18 | } -------------------------------------------------------------------------------- /src/AspectCore.Abstractions/DependencyInjection/Definitions/DelegateServiceDefinition.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace AspectCore.DependencyInjection 4 | { 5 | public class DelegateServiceDefinition : ServiceDefinition 6 | { 7 | public DelegateServiceDefinition(Type serviceType, Func implementationDelegate, Lifetime lifetime) : base(serviceType, lifetime) 8 | { 9 | ImplementationDelegate = implementationDelegate ?? throw new ArgumentNullException(nameof(implementationDelegate)); 10 | } 11 | 12 | public Func ImplementationDelegate { get; } 13 | } 14 | } -------------------------------------------------------------------------------- /src/AspectCore.Abstractions/DependencyInjection/Definitions/InstanceServiceDefinition.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Reflection; 3 | 4 | namespace AspectCore.DependencyInjection 5 | { 6 | public sealed class InstanceServiceDefinition : ServiceDefinition 7 | { 8 | public InstanceServiceDefinition(Type serviceType, object implementationInstance) : base(serviceType, Lifetime.Singleton) 9 | { 10 | ImplementationInstance = implementationInstance ?? throw new ArgumentNullException(nameof(implementationInstance)); 11 | } 12 | 13 | public object ImplementationInstance { get; } 14 | } 15 | } -------------------------------------------------------------------------------- /src/AspectCore.Abstractions/DependencyInjection/Definitions/ServiceDefinition.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using AspectCore.DynamicProxy; 3 | 4 | namespace AspectCore.DependencyInjection 5 | { 6 | [NonAspect] 7 | public abstract class ServiceDefinition 8 | { 9 | public Type ServiceType { get; } 10 | 11 | public Lifetime Lifetime { get; } 12 | 13 | public ServiceDefinition(Type serviceType, Lifetime lifetime) 14 | { 15 | Lifetime = lifetime; 16 | ServiceType = serviceType ?? throw new ArgumentNullException(nameof(serviceType)); 17 | } 18 | } 19 | } -------------------------------------------------------------------------------- /src/AspectCore.Abstractions/DependencyInjection/Definitions/TypeServiceDefinition.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Reflection; 3 | 4 | namespace AspectCore.DependencyInjection 5 | { 6 | public sealed class TypeServiceDefinition : ServiceDefinition 7 | { 8 | public TypeServiceDefinition(Type serviceType, Type implementationType, Lifetime lifetime) : base(serviceType, lifetime) 9 | { 10 | ImplementationType = implementationType ?? throw new ArgumentNullException(nameof(implementationType)); 11 | } 12 | 13 | public Type ImplementationType { get; } 14 | } 15 | } -------------------------------------------------------------------------------- /src/AspectCore.Abstractions/DependencyInjection/FromServiceContextAttribute.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace AspectCore.DependencyInjection 4 | { 5 | [AttributeUsage(AttributeTargets.All, AllowMultiple = false, Inherited = false)] 6 | public class FromServiceContextAttribute : Attribute 7 | { 8 | public FromServiceContextAttribute() 9 | { 10 | } 11 | } 12 | } -------------------------------------------------------------------------------- /src/AspectCore.Abstractions/DependencyInjection/ILifetimeServiceContext.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using AspectCore.DynamicProxy; 4 | 5 | namespace AspectCore.DependencyInjection 6 | { 7 | [NonAspect] 8 | public interface ILifetimeServiceContext : IEnumerable 9 | { 10 | Lifetime Lifetime { get; } 11 | 12 | int Count { get; } 13 | 14 | void Add(ServiceDefinition item); 15 | 16 | bool Contains(Type serviceType); 17 | } 18 | } -------------------------------------------------------------------------------- /src/AspectCore.Abstractions/DependencyInjection/IManyEnumerable.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using AspectCore.DynamicProxy; 4 | 5 | namespace AspectCore.DependencyInjection 6 | { 7 | [NonAspect, NonCallback] 8 | public interface IManyEnumerable : IEnumerable, IEnumerable 9 | { 10 | } 11 | } -------------------------------------------------------------------------------- /src/AspectCore.Abstractions/DependencyInjection/IPropertyInjector.cs: -------------------------------------------------------------------------------- 1 | using AspectCore.DynamicProxy; 2 | 3 | namespace AspectCore.DependencyInjection 4 | { 5 | [NonAspect] 6 | public interface IPropertyInjector 7 | { 8 | void Invoke(object implementation); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /src/AspectCore.Abstractions/DependencyInjection/IPropertyInjectorFactory.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using AspectCore.DynamicProxy; 3 | 4 | namespace AspectCore.DependencyInjection 5 | { 6 | [NonAspect] 7 | public interface IPropertyInjectorFactory 8 | { 9 | IPropertyInjector Create(Type implementationType); 10 | } 11 | } -------------------------------------------------------------------------------- /src/AspectCore.Abstractions/DependencyInjection/IScopeResolverFactory.cs: -------------------------------------------------------------------------------- 1 | using AspectCore.DynamicProxy; 2 | 3 | namespace AspectCore.DependencyInjection 4 | { 5 | [NonAspect] 6 | public interface IScopeResolverFactory 7 | { 8 | IServiceResolver CreateScope(); 9 | } 10 | } -------------------------------------------------------------------------------- /src/AspectCore.Abstractions/DependencyInjection/IServiceContext.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using AspectCore.Configuration; 4 | using AspectCore.DynamicProxy; 5 | 6 | namespace AspectCore.DependencyInjection 7 | { 8 | [NonAspect] 9 | public interface IServiceContext : IEnumerable 10 | { 11 | ILifetimeServiceContext Singletons { get; } 12 | 13 | ILifetimeServiceContext Scopeds { get; } 14 | 15 | ILifetimeServiceContext Transients { get; } 16 | 17 | IAspectConfiguration Configuration { get; } 18 | 19 | int Count { get; } 20 | 21 | void Add(ServiceDefinition item); 22 | 23 | bool Remove(ServiceDefinition item); 24 | 25 | bool Contains(Type serviceType); 26 | } 27 | } -------------------------------------------------------------------------------- /src/AspectCore.Abstractions/DependencyInjection/IServiceResolveCallback.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using AspectCore.DynamicProxy; 3 | 4 | namespace AspectCore.DependencyInjection 5 | { 6 | [NonAspect, NonCallback] 7 | public interface IServiceResolveCallback 8 | { 9 | object Invoke(IServiceResolver resolver, object instance, ServiceDefinition service); 10 | } 11 | 12 | public sealed class NonCallback : Attribute 13 | { 14 | } 15 | } -------------------------------------------------------------------------------- /src/AspectCore.Abstractions/DependencyInjection/IServiceResolver.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using AspectCore.DynamicProxy; 3 | 4 | namespace AspectCore.DependencyInjection 5 | { 6 | [NonAspect] 7 | public interface IServiceResolver : IServiceProvider, IDisposable 8 | #if NET8_0_OR_GREATER 9 | , Microsoft.Extensions.DependencyInjection.IKeyedServiceProvider 10 | #endif 11 | { 12 | object Resolve(Type serviceType); 13 | } 14 | } -------------------------------------------------------------------------------- /src/AspectCore.Abstractions/DependencyInjection/ITransientServiceAccessor.cs: -------------------------------------------------------------------------------- 1 | using AspectCore.DynamicProxy; 2 | 3 | namespace AspectCore.DependencyInjection 4 | { 5 | [NonAspect] 6 | public interface ITransientServiceAccessor where T : class 7 | { 8 | T Value { get; } 9 | } 10 | } -------------------------------------------------------------------------------- /src/AspectCore.Abstractions/DependencyInjection/Lifetime.cs: -------------------------------------------------------------------------------- 1 | namespace AspectCore.DependencyInjection 2 | { 3 | public enum Lifetime 4 | { 5 | /// 6 | /// Specifies that a single instance of the service will be created. 7 | /// 8 | Singleton, 9 | /// 10 | /// Specifies that a new instance of the service will be created for each scope. 11 | /// 12 | /// 13 | /// In ASP.NET Core applications a scope is created around each server request. 14 | /// 15 | Scoped, 16 | /// 17 | /// Specifies that a new instance of the service will be created every time it is requested. 18 | /// 19 | Transient 20 | } 21 | } -------------------------------------------------------------------------------- /src/AspectCore.Abstractions/DynamicProxy/AbstractInterceptor.cs: -------------------------------------------------------------------------------- 1 | using System.Threading.Tasks; 2 | 3 | namespace AspectCore.DynamicProxy 4 | { 5 | [NonAspect] 6 | public abstract class AbstractInterceptor : IInterceptor 7 | { 8 | public virtual bool AllowMultiple { get; } = false; 9 | 10 | public virtual int Order { get; set; } = 0; 11 | 12 | public bool Inherited { get; set; } = false; 13 | 14 | public abstract Task Invoke(AspectContext context, AspectDelegate next); 15 | } 16 | } -------------------------------------------------------------------------------- /src/AspectCore.Abstractions/DynamicProxy/AbstractInterceptorAttribute.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading.Tasks; 3 | 4 | namespace AspectCore.DynamicProxy 5 | { 6 | /// 7 | /// Standard interceptor definition via custom attribute. 8 | /// 9 | [NonAspect] 10 | [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class | AttributeTargets.Interface, Inherited = false)] 11 | public abstract class AbstractInterceptorAttribute : Attribute, IInterceptor 12 | { 13 | public virtual bool AllowMultiple { get; } = false; 14 | 15 | public virtual int Order { get; set; } = 0; 16 | 17 | public bool Inherited { get; set; } = false; 18 | 19 | public abstract Task Invoke(AspectContext context, AspectDelegate next); 20 | } 21 | } -------------------------------------------------------------------------------- /src/AspectCore.Abstractions/DynamicProxy/AspectActivatorContext.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | 3 | namespace AspectCore.DynamicProxy 4 | { 5 | public struct AspectActivatorContext 6 | { 7 | public MethodInfo ServiceMethod { get; } 8 | 9 | public MethodInfo TargetMethod { get; } 10 | 11 | public MethodInfo ProxyMethod { get; } 12 | 13 | public object TargetInstance { get; } 14 | 15 | public object ProxyInstance { get; } 16 | 17 | public object[] Parameters { get; } 18 | 19 | public AspectActivatorContext(MethodInfo serviceMethod, MethodInfo targetMethod, MethodInfo proxyMethod, 20 | object targetInstance, object proxyInstance, object[] parameters) 21 | { 22 | ServiceMethod = serviceMethod; 23 | TargetMethod = targetMethod; 24 | ProxyMethod = proxyMethod; 25 | TargetInstance = targetInstance; 26 | ProxyInstance = proxyInstance; 27 | Parameters = parameters; 28 | } 29 | } 30 | } -------------------------------------------------------------------------------- /src/AspectCore.Abstractions/DynamicProxy/AspectContext.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Reflection; 4 | using System.Threading.Tasks; 5 | 6 | namespace AspectCore.DynamicProxy 7 | { 8 | [NonAspect] 9 | public abstract class AspectContext 10 | { 11 | public abstract IDictionary AdditionalData { get; } 12 | 13 | public abstract object ReturnValue { get; set; } 14 | 15 | public abstract IServiceProvider ServiceProvider { get; } 16 | 17 | public abstract MethodInfo ServiceMethod { get; } 18 | 19 | public abstract object Implementation { get; } 20 | 21 | public abstract MethodInfo ImplementationMethod { get; } 22 | 23 | public abstract object[] Parameters { get; } 24 | 25 | public abstract MethodInfo ProxyMethod { get; } 26 | 27 | public abstract object Proxy { get; } 28 | 29 | public abstract Task Break(); 30 | 31 | public abstract Task Invoke(AspectDelegate next); 32 | 33 | public abstract Task Complete(); 34 | } 35 | } -------------------------------------------------------------------------------- /src/AspectCore.Abstractions/DynamicProxy/AspectDelegate.cs: -------------------------------------------------------------------------------- 1 | using System.Threading.Tasks; 2 | 3 | namespace AspectCore.DynamicProxy 4 | { 5 | public delegate Task AspectDelegate(AspectContext context); 6 | } 7 | -------------------------------------------------------------------------------- /src/AspectCore.Abstractions/DynamicProxy/AspectInvalidCastException.cs: -------------------------------------------------------------------------------- 1 | namespace AspectCore.DynamicProxy 2 | { 3 | public class AspectInvalidCastException : AspectInvocationException 4 | { 5 | public AspectInvalidCastException(AspectContext aspectContext, string message) : base(aspectContext, message) { } 6 | } 7 | } -------------------------------------------------------------------------------- /src/AspectCore.Abstractions/DynamicProxy/AspectInvalidOperationException.cs: -------------------------------------------------------------------------------- 1 | namespace AspectCore.DynamicProxy 2 | { 3 | public class AspectInvalidOperationException : AspectInvocationException 4 | { 5 | public AspectInvalidOperationException(AspectContext aspectContext, string message) : base(aspectContext, message) { } 6 | } 7 | } -------------------------------------------------------------------------------- /src/AspectCore.Abstractions/DynamicProxy/AspectInvocationException.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace AspectCore.DynamicProxy 4 | { 5 | public class AspectInvocationException : Exception 6 | { 7 | public AspectContext AspectContext { get; } 8 | 9 | public AspectInvocationException(AspectContext aspectContext, string message) : this(aspectContext, message, null) { } 10 | 11 | public AspectInvocationException(AspectContext aspectContext, Exception innerException) 12 | : this(aspectContext, $"Exception has been thrown by the aspect of an invocation. ---> {innerException?.Message}.", innerException) { } 13 | 14 | public AspectInvocationException(AspectContext aspectContext, string message, Exception innerException) : base(message, innerException) 15 | { 16 | AspectContext = aspectContext; 17 | } 18 | } 19 | } -------------------------------------------------------------------------------- /src/AspectCore.Abstractions/DynamicProxy/AspectValidationContext.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System; 3 | 4 | namespace AspectCore.DynamicProxy 5 | { 6 | public struct AspectValidationContext : IEquatable 7 | { 8 | public MethodInfo Method { get; set; } 9 | 10 | public bool StrictValidation { get; set; } 11 | 12 | public bool Equals(AspectValidationContext other) 13 | { 14 | return Method == other.Method && StrictValidation == other.StrictValidation; 15 | } 16 | 17 | public override bool Equals(object obj) 18 | { 19 | if (obj == null) 20 | { 21 | return false; 22 | } 23 | if (obj is AspectActivatorContext other) 24 | return Equals(other); 25 | return false; 26 | } 27 | 28 | public override int GetHashCode() 29 | { 30 | var hash_1 = this.Method?.GetHashCode() ?? 0; 31 | var hash_2 = StrictValidation.GetHashCode(); 32 | return (hash_1 << 5) + hash_1 ^ hash_2; 33 | } 34 | } 35 | } -------------------------------------------------------------------------------- /src/AspectCore.Abstractions/DynamicProxy/AspectValidationDelegate.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | 3 | namespace AspectCore.DynamicProxy 4 | { 5 | public delegate bool AspectValidationDelegate(AspectValidationContext context); 6 | } 7 | -------------------------------------------------------------------------------- /src/AspectCore.Abstractions/DynamicProxy/AsyncAspectAttribute.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace AspectCore.DynamicProxy 4 | { 5 | [AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)] 6 | public class AsyncAspectAttribute : Attribute 7 | { 8 | } 9 | } -------------------------------------------------------------------------------- /src/AspectCore.Abstractions/DynamicProxy/DelegateInterceptor.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading.Tasks; 3 | 4 | namespace AspectCore.DynamicProxy 5 | { 6 | [NonAspect] 7 | public class DelegateInterceptor : IInterceptor 8 | { 9 | private readonly Func _aspectDelegate; 10 | 11 | public bool AllowMultiple => true; 12 | 13 | public bool Inherited { get; set; } = false; 14 | 15 | public int Order { get; set; } 16 | 17 | public DelegateInterceptor(Func aspectDelegate, int order = 0) 18 | { 19 | _aspectDelegate = aspectDelegate ?? throw new ArgumentNullException(nameof(aspectDelegate)); 20 | Order = order; 21 | } 22 | 23 | public Task Invoke(AspectContext context, AspectDelegate next) 24 | { 25 | return _aspectDelegate(next)(context); 26 | } 27 | } 28 | } -------------------------------------------------------------------------------- /src/AspectCore.Abstractions/DynamicProxy/DynamicallyAttribute.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace AspectCore.DynamicProxy 4 | { 5 | [AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)] 6 | public sealed class DynamicallyAttribute : Attribute 7 | { 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /src/AspectCore.Abstractions/DynamicProxy/IAdditionalInterceptorSelector.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Reflection; 4 | using System.Text; 5 | 6 | namespace AspectCore.DynamicProxy 7 | { 8 | [NonAspect] 9 | public interface IAdditionalInterceptorSelector 10 | { 11 | IEnumerable Select(MethodInfo serviceMethod, MethodInfo implementationMethod); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/AspectCore.Abstractions/DynamicProxy/IAspectActivator.cs: -------------------------------------------------------------------------------- 1 | using System.Threading.Tasks; 2 | 3 | namespace AspectCore.DynamicProxy 4 | { 5 | [NonAspect] 6 | public interface IAspectActivator 7 | { 8 | TResult Invoke(AspectActivatorContext activatorContext); 9 | 10 | Task InvokeTask(AspectActivatorContext activatorContext); 11 | 12 | ValueTask InvokeValueTask(AspectActivatorContext activatorContext); 13 | } 14 | } -------------------------------------------------------------------------------- /src/AspectCore.Abstractions/DynamicProxy/IAspectActivatorFactory.cs: -------------------------------------------------------------------------------- 1 | namespace AspectCore.DynamicProxy 2 | { 3 | [NonAspect] 4 | public interface IAspectActivatorFactory 5 | { 6 | IAspectActivator Create(); 7 | } 8 | } -------------------------------------------------------------------------------- /src/AspectCore.Abstractions/DynamicProxy/IAspectBuilder.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace AspectCore.DynamicProxy 5 | { 6 | [NonAspect] 7 | public interface IAspectBuilder 8 | { 9 | IEnumerable> Delegates { get; } 10 | 11 | AspectDelegate Build(); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/AspectCore.Abstractions/DynamicProxy/IAspectBuilderFactory.cs: -------------------------------------------------------------------------------- 1 | namespace AspectCore.DynamicProxy 2 | { 3 | [NonAspect] 4 | public interface IAspectBuilderFactory 5 | { 6 | IAspectBuilder Create(AspectContext context); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src/AspectCore.Abstractions/DynamicProxy/IAspectCaching.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace AspectCore.DynamicProxy 4 | { 5 | [NonAspect] 6 | public interface IAspectCaching : IDisposable 7 | { 8 | string Name { get; } 9 | 10 | object Get(object key); 11 | 12 | void Set(object key, object value); 13 | 14 | object GetOrAdd(object key, Func factory); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/AspectCore.Abstractions/DynamicProxy/IAspectCachingProvider.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace AspectCore.DynamicProxy 4 | { 5 | [NonAspect] 6 | public interface IAspectCachingProvider : IDisposable 7 | { 8 | IAspectCaching GetAspectCaching(string name); 9 | } 10 | } -------------------------------------------------------------------------------- /src/AspectCore.Abstractions/DynamicProxy/IAspectContextFactory.cs: -------------------------------------------------------------------------------- 1 | namespace AspectCore.DynamicProxy 2 | { 3 | [NonAspect] 4 | public interface IAspectContextFactory 5 | { 6 | AspectContext CreateContext(AspectActivatorContext activatorContext); 7 | 8 | void ReleaseContext(AspectContext aspectContext); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /src/AspectCore.Abstractions/DynamicProxy/IAspectValidationHandler.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | 3 | namespace AspectCore.DynamicProxy 4 | { 5 | [NonAspect] 6 | public interface IAspectValidationHandler 7 | { 8 | int Order { get; } 9 | 10 | bool Invoke(AspectValidationContext context, AspectValidationDelegate next); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/AspectCore.Abstractions/DynamicProxy/IAspectValidator.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | 3 | namespace AspectCore.DynamicProxy 4 | { 5 | [NonAspect] 6 | public interface IAspectValidator 7 | { 8 | bool Validate(MethodInfo method, bool isStrictValidation); 9 | } 10 | } -------------------------------------------------------------------------------- /src/AspectCore.Abstractions/DynamicProxy/IAspectValidatorBuilder.cs: -------------------------------------------------------------------------------- 1 | namespace AspectCore.DynamicProxy 2 | { 3 | [NonAspect] 4 | public interface IAspectValidatorBuilder 5 | { 6 | IAspectValidator Build(); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src/AspectCore.Abstractions/DynamicProxy/IInterceptor.cs: -------------------------------------------------------------------------------- 1 | using System.Threading.Tasks; 2 | 3 | namespace AspectCore.DynamicProxy 4 | { 5 | [NonAspect] 6 | public interface IInterceptor 7 | { 8 | bool AllowMultiple { get; } 9 | 10 | bool Inherited { get; set; } 11 | 12 | int Order { get; set; } 13 | 14 | Task Invoke(AspectContext context, AspectDelegate next); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/AspectCore.Abstractions/DynamicProxy/IInterceptorCollector.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Reflection; 3 | 4 | namespace AspectCore.DynamicProxy 5 | { 6 | [NonAspect] 7 | public interface IInterceptorCollector 8 | { 9 | IEnumerable Collect(MethodInfo serviceMethod, MethodInfo implementationMethod); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/AspectCore.Abstractions/DynamicProxy/IInterceptorSelector.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Reflection; 3 | 4 | namespace AspectCore.DynamicProxy 5 | { 6 | [NonAspect] 7 | public interface IInterceptorSelector 8 | { 9 | IEnumerable Select(MethodInfo method); 10 | } 11 | } -------------------------------------------------------------------------------- /src/AspectCore.Abstractions/DynamicProxy/IProxyGenerator.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace AspectCore.DynamicProxy 4 | { 5 | [NonAspect] 6 | public interface IProxyGenerator : IDisposable 7 | { 8 | IProxyTypeGenerator TypeGenerator { get; } 9 | 10 | object CreateInterfaceProxy(Type serviceType); 11 | 12 | object CreateInterfaceProxy(Type serviceType, object implementationInstance); 13 | 14 | object CreateClassProxy(Type serviceType, Type implementationType, object[] args); 15 | } 16 | } -------------------------------------------------------------------------------- /src/AspectCore.Abstractions/DynamicProxy/IProxyTypeGenerator.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace AspectCore.DynamicProxy 4 | { 5 | [NonAspect] 6 | public interface IProxyTypeGenerator 7 | { 8 | Type CreateInterfaceProxyType(Type serviceType); 9 | 10 | Type CreateInterfaceProxyType(Type serviceType, Type implementationType); 11 | 12 | Type CreateClassProxyType(Type serviceType, Type implementationType); 13 | } 14 | } -------------------------------------------------------------------------------- /src/AspectCore.Abstractions/DynamicProxy/IServiceInstanceAccessor.cs: -------------------------------------------------------------------------------- 1 | namespace AspectCore.DynamicProxy 2 | { 3 | [NonAspect] 4 | public interface IServiceInstanceAccessor 5 | { 6 | object ServiceInstance { get; } 7 | } 8 | 9 | [NonAspect] 10 | public interface IServiceInstanceAccessor 11 | { 12 | TService ServiceInstance { get; } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/AspectCore.Abstractions/DynamicProxy/NonAspectAttribute.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace AspectCore.DynamicProxy 4 | { 5 | [AttributeUsage(AttributeTargets.All, AllowMultiple = false, Inherited = false)] 6 | public class NonAspectAttribute : Attribute 7 | { 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /src/AspectCore.Abstractions/DynamicProxy/Parameters/IParameterInterceptor.cs: -------------------------------------------------------------------------------- 1 | using System.Threading.Tasks; 2 | 3 | namespace AspectCore.DynamicProxy.Parameters 4 | { 5 | [NonAspect] 6 | public interface IParameterInterceptor 7 | { 8 | Task Invoke(ParameterAspectContext context, ParameterAspectDelegate next); 9 | } 10 | } -------------------------------------------------------------------------------- /src/AspectCore.Abstractions/DynamicProxy/Parameters/IParameterInterceptorSelector.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | 3 | namespace AspectCore.DynamicProxy.Parameters 4 | { 5 | public interface IParameterInterceptorSelector 6 | { 7 | IParameterInterceptor[] Select(ParameterInfo parameter); 8 | } 9 | } -------------------------------------------------------------------------------- /src/AspectCore.Abstractions/DynamicProxy/Parameters/Parameter.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Reflection; 3 | 4 | namespace AspectCore.DynamicProxy.Parameters 5 | { 6 | public class Parameter 7 | { 8 | protected readonly AspectContext _context; 9 | protected readonly int _index; 10 | 11 | public string Name { get; } 12 | 13 | public Type Type { get; } 14 | 15 | public Type RawType 16 | { 17 | get 18 | { 19 | if (IsRef) 20 | { 21 | return Type.GetElementType(); 22 | } 23 | return Type; 24 | } 25 | } 26 | 27 | public bool IsRef { get; } 28 | 29 | public virtual object Value 30 | { 31 | get 32 | { 33 | return _context.Parameters[_index]; 34 | } 35 | set 36 | { 37 | _context.Parameters[_index] = value; 38 | } 39 | } 40 | 41 | public ParameterInfo ParameterInfo { get; } 42 | 43 | internal Parameter(AspectContext context, int index, ParameterInfo parameterInfo) 44 | { 45 | _context = context; 46 | _index = index; 47 | Name = parameterInfo.Name; 48 | Type = parameterInfo.ParameterType; 49 | IsRef = Type.IsByRef; 50 | ParameterInfo = parameterInfo; 51 | } 52 | } 53 | } -------------------------------------------------------------------------------- /src/AspectCore.Abstractions/DynamicProxy/Parameters/ParameterAspectContext.cs: -------------------------------------------------------------------------------- 1 | namespace AspectCore.DynamicProxy.Parameters 2 | { 3 | public struct ParameterAspectContext 4 | { 5 | public Parameter Parameter { get; } 6 | 7 | public AspectContext AspectContext { get; } 8 | 9 | public ParameterAspectContext(AspectContext aspectContext, Parameter parameter) 10 | { 11 | AspectContext = aspectContext; 12 | Parameter = parameter; 13 | } 14 | } 15 | } -------------------------------------------------------------------------------- /src/AspectCore.Abstractions/DynamicProxy/Parameters/ParameterAspectDelegate.cs: -------------------------------------------------------------------------------- 1 | using System.Threading.Tasks; 2 | 3 | namespace AspectCore.DynamicProxy.Parameters 4 | { 5 | public delegate Task ParameterAspectDelegate(ParameterAspectContext context); 6 | } 7 | -------------------------------------------------------------------------------- /src/AspectCore.Abstractions/DynamicProxy/Parameters/ParameterInterceptorAttribute.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading.Tasks; 3 | 4 | namespace AspectCore.DynamicProxy.Parameters 5 | { 6 | [AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = false)] 7 | public abstract class ParameterInterceptorAttribute : Attribute, IParameterInterceptor 8 | { 9 | public abstract Task Invoke(ParameterAspectContext context, ParameterAspectDelegate next); 10 | } 11 | } -------------------------------------------------------------------------------- /src/AspectCore.Abstractions/DynamicProxy/Parameters/ReturnParameter.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | 3 | namespace AspectCore.DynamicProxy.Parameters 4 | { 5 | internal sealed class ReturnParameter : Parameter 6 | { 7 | public override object Value 8 | { 9 | get 10 | { 11 | return _context.ReturnValue; 12 | } 13 | set 14 | { 15 | _context.ReturnValue = value; 16 | } 17 | } 18 | 19 | internal ReturnParameter(AspectContext context, ParameterInfo reflector) 20 | : base(context, -1, reflector) 21 | { 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/AspectCore.Abstractions/DynamicProxy/Parameters/ReturnParameterInterceptorAttribute.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading.Tasks; 3 | 4 | namespace AspectCore.DynamicProxy.Parameters 5 | { 6 | [AttributeUsage(AttributeTargets.ReturnValue, AllowMultiple = false, Inherited = false)] 7 | public abstract class ReturnParameterInterceptorAttribute : Attribute, IParameterInterceptor 8 | { 9 | public abstract Task Invoke(ParameterAspectContext context, ParameterAspectDelegate next); 10 | } 11 | } -------------------------------------------------------------------------------- /src/AspectCore.Abstractions/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyConfiguration("")] 9 | [assembly: AssemblyCompany("")] 10 | [assembly: AssemblyTrademark("")] 11 | 12 | // Setting ComVisible to false makes the types in this assembly not visible 13 | // to COM components. If you need to access a type in this assembly from 14 | // COM, set the ComVisible attribute to true on that type. 15 | [assembly: ComVisible(false)] 16 | 17 | // The following GUID is for the ID of the typelib if this project is exposed to COM 18 | [assembly: Guid("f85d3075-97c2-4ac1-9b36-db752c0edbee")] -------------------------------------------------------------------------------- /src/AspectCore.Core/AspectCore.Core.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | The implementation of the AspectCore framework. 5 | AspectCore.Core 6 | true 7 | AspectCore.Core 8 | AspectCore.Core 9 | DynamicProxy;Aop;Aspect;AspectCore;Interceptor 10 | The implementation of the AspectCore framework. 11 | .net9.0;net8.0;net7.0;net6.0;netstandard2.1;netstandard2.0 12 | true 13 | true 14 | snupkg 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /src/AspectCore.Core/Configuration/AspectConfiguration.cs: -------------------------------------------------------------------------------- 1 | using AspectCore.DependencyInjection; 2 | 3 | namespace AspectCore.Configuration 4 | { 5 | public sealed class AspectConfiguration : IAspectConfiguration 6 | { 7 | public AspectValidationHandlerCollection ValidationHandlers { get; } 8 | 9 | public InterceptorCollection Interceptors { get; } 10 | 11 | public NonAspectPredicateCollection NonAspectPredicates { get; } 12 | 13 | public bool ThrowAspectException { get; set; } 14 | 15 | public AspectConfiguration() 16 | { 17 | ThrowAspectException = false; 18 | ValidationHandlers = new AspectValidationHandlerCollection().AddDefault(this); 19 | Interceptors = new InterceptorCollection(); 20 | NonAspectPredicates = new NonAspectPredicateCollection().AddDefault(); 21 | } 22 | } 23 | } -------------------------------------------------------------------------------- /src/AspectCore.Core/Configuration/Extensions/AspectValidationHandlerCollectionExtensions.cs: -------------------------------------------------------------------------------- 1 | using AspectCore.DynamicProxy; 2 | 3 | namespace AspectCore.Configuration 4 | { 5 | internal static class AspectValidationHandlerCollectionExtensions 6 | { 7 | internal static AspectValidationHandlerCollection AddDefault(this AspectValidationHandlerCollection aspectValidationHandlers, IAspectConfiguration configuration) 8 | { 9 | aspectValidationHandlers.Add(new OverwriteAspectValidationHandler()); 10 | aspectValidationHandlers.Add(new AttributeAspectValidationHandler()); 11 | aspectValidationHandlers.Add(new CacheAspectValidationHandler()); 12 | aspectValidationHandlers.Add(new ConfigureAspectValidationHandler(configuration)); 13 | return aspectValidationHandlers; 14 | } 15 | } 16 | } -------------------------------------------------------------------------------- /src/AspectCore.Core/Configuration/Extensions/StringExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace AspectCore.Configuration 4 | { 5 | public static class StringExtensions 6 | { 7 | public static unsafe bool Matches(this string input, string pattern) 8 | { 9 | if (string.IsNullOrEmpty(input)) 10 | throw new ArgumentNullException(nameof(input)); 11 | 12 | if (string.IsNullOrEmpty(pattern)) 13 | throw new ArgumentNullException(nameof(pattern)); 14 | 15 | bool matched = false; 16 | 17 | fixed (char* p_wild = pattern) 18 | fixed (char* p_str = input) 19 | { 20 | char* wild = p_wild, str = p_str, cp = null, mp = null; 21 | 22 | while ((*str) != 0 && (*wild != '*')) 23 | { 24 | if ((*wild != *str) && (*wild != '?')) return matched; wild++; str++; 25 | } 26 | 27 | while (*str != 0) 28 | { 29 | if (*wild == '*') { if (0 == (*++wild)) return (matched = true); mp = wild; cp = str + 1; } 30 | else if ((*wild == *str) || (*wild == '?')) { wild++; str++; } else { wild = mp; str = cp++; } 31 | } 32 | 33 | while (*wild == '*') wild++; return (*wild) == 0; 34 | } 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/AspectCore.Core/Configuration/InterceptorFactories/DelegateInterceptorFactory.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using AspectCore.DynamicProxy; 3 | 4 | namespace AspectCore.Configuration 5 | { 6 | public sealed class DelegateInterceptorFactory : InterceptorFactory 7 | { 8 | private readonly Func _aspectDelegate; 9 | private readonly int _order; 10 | 11 | public DelegateInterceptorFactory(Func aspectDelegate, int order, params AspectPredicate[] predicates) 12 | : base(predicates) 13 | { 14 | _aspectDelegate = aspectDelegate ?? throw new ArgumentNullException(nameof(aspectDelegate)); 15 | _order = order; 16 | } 17 | 18 | public override IInterceptor CreateInstance(IServiceProvider serviceProvider) 19 | { 20 | return new DelegateInterceptor(_aspectDelegate, _order); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/AspectCore.Core/Configuration/InterceptorFactories/ServiceInterceptorFactory.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Reflection; 3 | using AspectCore.DynamicProxy; 4 | 5 | namespace AspectCore.Configuration 6 | { 7 | public sealed class ServiceInterceptorFactory : InterceptorFactory 8 | { 9 | private readonly Type _interceptorType; 10 | 11 | public ServiceInterceptorFactory(Type interceptorType, params AspectPredicate[] predicates) 12 | : base(predicates) 13 | { 14 | if (interceptorType == null) 15 | { 16 | throw new ArgumentNullException(nameof(interceptorType)); 17 | } 18 | if (!typeof(IInterceptor).GetTypeInfo().IsAssignableFrom(interceptorType.GetTypeInfo())) 19 | { 20 | throw new ArgumentException($"{interceptorType} is not an interceptor type.", nameof(interceptorType)); 21 | } 22 | _interceptorType = interceptorType; 23 | } 24 | 25 | public override IInterceptor CreateInstance(IServiceProvider serviceProvider) 26 | { 27 | return new ServiceInterceptorAttribute(_interceptorType); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/AspectCore.Core/Configuration/InterceptorFactories/TypeInterceptorFactory.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Reflection; 3 | using AspectCore.DynamicProxy; 4 | 5 | namespace AspectCore.Configuration 6 | { 7 | public sealed class TypeInterceptorFactory : InterceptorFactory 8 | { 9 | private readonly static object[] emptyArgs = new object[0]; 10 | private readonly object[] _args; 11 | private readonly Type _interceptorType; 12 | 13 | public TypeInterceptorFactory(Type interceptorType, object[] args, params AspectPredicate[] predicates) 14 | : base(predicates) 15 | { 16 | if (interceptorType == null) 17 | { 18 | throw new ArgumentNullException(nameof(interceptorType)); 19 | } 20 | if (!typeof(IInterceptor).GetTypeInfo().IsAssignableFrom(interceptorType.GetTypeInfo())) 21 | { 22 | throw new ArgumentException($"{interceptorType} is not an interceptor type.", nameof(interceptorType)); 23 | } 24 | _interceptorType = interceptorType; 25 | _args = args ?? emptyArgs; 26 | } 27 | 28 | public override IInterceptor CreateInstance(IServiceProvider serviceProvider) 29 | { 30 | return (IInterceptor)Activator.CreateInstance(_interceptorType, _args); 31 | } 32 | } 33 | } -------------------------------------------------------------------------------- /src/AspectCore.Core/DependencyInjection/EnumerableServiceDefintion.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace AspectCore.DependencyInjection 5 | { 6 | internal class EnumerableServiceDefintion : ServiceDefinition 7 | { 8 | public IEnumerable ServiceDefinitions { get; } 9 | 10 | public Type ElementType { get; } 11 | 12 | public EnumerableServiceDefintion(Type serviceType, Type elementType, IEnumerable serviceDefinitions) : base(serviceType, Lifetime.Transient) 13 | { 14 | ElementType = elementType; 15 | ServiceDefinitions = serviceDefinitions; 16 | } 17 | } 18 | 19 | internal class ManyEnumerableServiceDefintion : EnumerableServiceDefintion 20 | { 21 | public ManyEnumerableServiceDefintion(Type serviceType, Type elementType, IEnumerable serviceDefinitions) : base(serviceType, elementType, serviceDefinitions) 22 | { 23 | } 24 | } 25 | } -------------------------------------------------------------------------------- /src/AspectCore.Core/DependencyInjection/Extensions/LinkedListExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace AspectCore.DependencyInjection 6 | { 7 | internal static class LinkedListExtensions 8 | { 9 | public static LinkedList Add(this LinkedList linkedList, T value) 10 | { 11 | if (linkedList == null) 12 | { 13 | throw new ArgumentNullException(nameof(linkedList)); 14 | } 15 | if (value == null) 16 | { 17 | throw new ArgumentNullException(nameof(value)); 18 | } 19 | if (linkedList.Count == 0) linkedList.AddFirst(value); 20 | else linkedList.AddAfter(linkedList.Last, value); 21 | return linkedList; 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/AspectCore.Core/DependencyInjection/Extensions/ServiceContainerBuildExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace AspectCore.DependencyInjection 4 | { 5 | public static class ServiceContainerBuildExtensions 6 | { 7 | public static IServiceResolver Build(this IServiceContext serviceContext) 8 | { 9 | if (serviceContext == null) 10 | { 11 | throw new ArgumentNullException(nameof(serviceContext)); 12 | } 13 | return new ServiceResolver(serviceContext); 14 | } 15 | } 16 | } -------------------------------------------------------------------------------- /src/AspectCore.Core/DependencyInjection/IServiceResolveCallbackProvider.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace AspectCore.DependencyInjection 4 | { 5 | internal interface IServiceResolveCallbackProvider 6 | { 7 | IServiceResolveCallback[] ServiceResolveCallbacks { get; } 8 | } 9 | } -------------------------------------------------------------------------------- /src/AspectCore.Core/DependencyInjection/LifetimeServiceContext.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | 6 | namespace AspectCore.DependencyInjection 7 | { 8 | public sealed class LifetimeServiceContext : ILifetimeServiceContext 9 | { 10 | private readonly ICollection _internalCollection; 11 | 12 | public Lifetime Lifetime { get; } 13 | 14 | public LifetimeServiceContext(ICollection collection, Lifetime lifetime) 15 | { 16 | _internalCollection = collection; 17 | Lifetime = lifetime; 18 | } 19 | 20 | public int Count => _internalCollection.Count(x => x.Lifetime == Lifetime); 21 | 22 | public void Add(ServiceDefinition item) 23 | { 24 | if (item.Lifetime == Lifetime) 25 | { 26 | _internalCollection.Add(item); 27 | } 28 | } 29 | 30 | public bool Contains(Type serviceType) => _internalCollection.Any(x => x.ServiceType == serviceType && x.Lifetime == Lifetime); 31 | 32 | public IEnumerator GetEnumerator() => _internalCollection.Where(x => x.Lifetime == Lifetime).GetEnumerator(); 33 | 34 | IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); 35 | } 36 | } -------------------------------------------------------------------------------- /src/AspectCore.Core/DependencyInjection/ManyEnumerable.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | 4 | namespace AspectCore.DependencyInjection 5 | { 6 | public sealed class ManyEnumerable : IManyEnumerable 7 | { 8 | private readonly IEnumerable _array; 9 | 10 | public ManyEnumerable(IEnumerable array) 11 | { 12 | _array = array; 13 | } 14 | 15 | public IEnumerator GetEnumerator() 16 | { 17 | return _array.GetEnumerator(); 18 | } 19 | 20 | IEnumerator IEnumerable.GetEnumerator() 21 | { 22 | return _array.GetEnumerator(); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/AspectCore.Core/DependencyInjection/PropertyInjector.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace AspectCore.DependencyInjection 4 | { 5 | internal sealed class PropertyInjector : IPropertyInjector 6 | { 7 | private readonly IServiceProvider _serviceProvider; 8 | private readonly PropertyResolver[] _propertyResolvers; 9 | 10 | public PropertyInjector(IServiceProvider serviceProvider, PropertyResolver[] propertyResolvers) 11 | { 12 | _serviceProvider = serviceProvider; 13 | _propertyResolvers = propertyResolvers; 14 | } 15 | 16 | public void Invoke(object implementation) 17 | { 18 | if (implementation == null) 19 | { 20 | return; 21 | } 22 | var resolverLength = _propertyResolvers.Length; 23 | if (resolverLength == 0) 24 | { 25 | return; 26 | } 27 | for (var i = 0; i < resolverLength; i++) 28 | { 29 | _propertyResolvers[i].Resolve(_serviceProvider, implementation); 30 | } 31 | } 32 | } 33 | } -------------------------------------------------------------------------------- /src/AspectCore.Core/DependencyInjection/PropertyInjectorCallback.cs: -------------------------------------------------------------------------------- 1 | namespace AspectCore.DependencyInjection 2 | { 3 | public class PropertyInjectorCallback : IServiceResolveCallback 4 | { 5 | public object Invoke(IServiceResolver resolver, object instance, ServiceDefinition service) 6 | { 7 | if (instance == null || !service.RequiredPropertyInjection()) 8 | { 9 | return instance; 10 | } 11 | var injectorFactory = resolver.Resolve(); 12 | var injector = injectorFactory.Create(instance.GetType()); 13 | injector.Invoke(instance); 14 | return instance; 15 | } 16 | } 17 | } -------------------------------------------------------------------------------- /src/AspectCore.Core/DependencyInjection/PropertyInjectorFactory.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using AspectCore.DynamicProxy; 3 | 4 | namespace AspectCore.DependencyInjection 5 | { 6 | [NonAspect] 7 | public class PropertyInjectorFactory : IPropertyInjectorFactory 8 | { 9 | private readonly IServiceProvider _servicePorvider; 10 | private readonly PropertyResolverSelector _propertyResolverSelector; 11 | 12 | public PropertyInjectorFactory(IServiceProvider servicePorvider) 13 | { 14 | _servicePorvider = servicePorvider; 15 | _propertyResolverSelector = PropertyResolverSelector.Default; 16 | } 17 | 18 | public IPropertyInjector Create(Type implementationType) 19 | { 20 | return new PropertyInjector(_servicePorvider, _propertyResolverSelector.SelectPropertyResolver(implementationType)); 21 | } 22 | } 23 | } -------------------------------------------------------------------------------- /src/AspectCore.Core/DependencyInjection/PropertyResolver.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using AspectCore.Extensions.Reflection; 3 | 4 | namespace AspectCore.DependencyInjection 5 | { 6 | public sealed class PropertyResolver 7 | { 8 | private readonly Func _propertyFactory; 9 | private readonly PropertyReflector _reflector; 10 | 11 | internal PropertyResolver(Func propertyFactory, PropertyReflector reflector) 12 | { 13 | _propertyFactory = propertyFactory; 14 | _reflector = reflector; 15 | } 16 | 17 | public void Resolve(IServiceProvider provider, object implementation) 18 | { 19 | _reflector.SetValue(implementation, _propertyFactory(provider)); 20 | } 21 | } 22 | } -------------------------------------------------------------------------------- /src/AspectCore.Core/DependencyInjection/ProxyServiceDefinition.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Reflection; 3 | 4 | namespace AspectCore.DependencyInjection 5 | { 6 | internal class ProxyServiceDefinition : ServiceDefinition 7 | { 8 | public ServiceDefinition ServiceDefinition { get; } 9 | public TypeServiceDefinition ClassProxyServiceDefinition { get; } 10 | 11 | public ProxyServiceDefinition(ServiceDefinition serviceDefinition, Type proxyType) : base(serviceDefinition.ServiceType, serviceDefinition.Lifetime) 12 | { 13 | ProxyType = proxyType; 14 | ServiceDefinition = serviceDefinition; 15 | if (serviceDefinition.ServiceType.GetTypeInfo().IsClass) 16 | { 17 | ClassProxyServiceDefinition = new TypeServiceDefinition(serviceDefinition.ServiceType, ProxyType, serviceDefinition.Lifetime); 18 | } 19 | } 20 | 21 | public Type ProxyType { get; } 22 | } 23 | } -------------------------------------------------------------------------------- /src/AspectCore.Core/DependencyInjection/ScopeResolverFactory.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using AspectCore.DynamicProxy; 3 | 4 | namespace AspectCore.DependencyInjection 5 | { 6 | [NonAspect] 7 | internal class ScopeResolverFactory : IScopeResolverFactory 8 | { 9 | private readonly ServiceResolver _serviceResolver; 10 | 11 | public ScopeResolverFactory(IServiceResolver serviceResolver) 12 | { 13 | _serviceResolver = serviceResolver as ServiceResolver; 14 | } 15 | 16 | public IServiceResolver CreateScope() 17 | { 18 | if (_serviceResolver == null) 19 | { 20 | throw new ArgumentNullException("ServiceResolver"); 21 | } 22 | return new ServiceResolver(_serviceResolver._root ?? _serviceResolver); 23 | } 24 | } 25 | } -------------------------------------------------------------------------------- /src/AspectCore.Core/DependencyInjection/TransientServiceAccessor.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace AspectCore.DependencyInjection 4 | { 5 | public sealed class TransientServiceAccessor : ITransientServiceAccessor where T : class 6 | { 7 | private readonly IServiceProvider _serviceProvider; 8 | public T Value => (T)_serviceProvider.GetService(typeof(T)); 9 | 10 | public TransientServiceAccessor(IServiceProvider serviceProvider) 11 | { 12 | _serviceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider)); 13 | } 14 | } 15 | } -------------------------------------------------------------------------------- /src/AspectCore.Core/DynamicProxy/AspectActivatorFactory.cs: -------------------------------------------------------------------------------- 1 | using AspectCore.Configuration; 2 | using System; 3 | 4 | namespace AspectCore.DynamicProxy 5 | { 6 | [NonAspect] 7 | public sealed class AspectActivatorFactory : IAspectActivatorFactory 8 | { 9 | private readonly IAspectContextFactory _aspectContextFactory; 10 | private readonly IAspectBuilderFactory _aspectBuilderFactory; 11 | private readonly IAspectConfiguration _aspectConfiguration; 12 | 13 | public AspectActivatorFactory(IAspectContextFactory aspectContextFactory, IAspectBuilderFactory aspectBuilderFactory, IAspectConfiguration aspectConfiguration) 14 | { 15 | _aspectContextFactory = aspectContextFactory ?? throw new ArgumentNullException(nameof(aspectContextFactory)); 16 | _aspectBuilderFactory = aspectBuilderFactory ?? throw new ArgumentNullException(nameof(aspectBuilderFactory)); 17 | _aspectConfiguration = aspectConfiguration ?? throw new ArgumentNullException(nameof(aspectConfiguration)); 18 | } 19 | 20 | public IAspectActivator Create() 21 | { 22 | return new AspectActivator(_aspectContextFactory, _aspectBuilderFactory, _aspectConfiguration); 23 | } 24 | } 25 | } -------------------------------------------------------------------------------- /src/AspectCore.Core/DynamicProxy/AspectCachingProvider.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Concurrent; 3 | 4 | namespace AspectCore.DynamicProxy 5 | { 6 | [NonAspect] 7 | public sealed class AspectCachingProvider : IAspectCachingProvider 8 | { 9 | private readonly ConcurrentDictionary _cachings; 10 | 11 | public AspectCachingProvider() 12 | { 13 | _cachings = new ConcurrentDictionary(); 14 | } 15 | 16 | public void Dispose() 17 | { 18 | foreach(var caching in _cachings) 19 | { 20 | caching.Value.Dispose(); 21 | } 22 | _cachings.Clear(); 23 | } 24 | 25 | public IAspectCaching GetAspectCaching(string name) 26 | { 27 | if (name == null) 28 | { 29 | throw new ArgumentNullException(nameof(name)); 30 | } 31 | return _cachings.GetOrAdd(name, key => new AspectCaching(key)); 32 | } 33 | } 34 | } -------------------------------------------------------------------------------- /src/AspectCore.Core/DynamicProxy/AspectContextFactory.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace AspectCore.DynamicProxy 4 | { 5 | [NonAspect] 6 | public sealed class AspectContextFactory : IAspectContextFactory 7 | { 8 | private static readonly object[] emptyParameters = new object[0]; 9 | private readonly IServiceProvider _serviceProvider; 10 | 11 | public AspectContextFactory(IServiceProvider serviceProvider) 12 | { 13 | _serviceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider)); 14 | } 15 | 16 | public AspectContext CreateContext(AspectActivatorContext activatorContext) 17 | { 18 | return new RuntimeAspectContext(_serviceProvider, 19 | activatorContext.ServiceMethod, 20 | activatorContext.TargetMethod, 21 | activatorContext.ProxyMethod, 22 | activatorContext.TargetInstance, 23 | activatorContext.ProxyInstance, 24 | activatorContext.Parameters ?? emptyParameters); 25 | } 26 | 27 | public void ReleaseContext(AspectContext aspectContext) 28 | { 29 | (aspectContext as IDisposable)?.Dispose(); 30 | } 31 | } 32 | } -------------------------------------------------------------------------------- /src/AspectCore.Core/DynamicProxy/AspectValidatorBuilder.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using AspectCore.Configuration; 5 | 6 | namespace AspectCore.DynamicProxy 7 | { 8 | [NonAspect] 9 | public sealed class AspectValidatorBuilder : IAspectValidatorBuilder 10 | { 11 | private readonly IList> _collections; 12 | 13 | public AspectValidatorBuilder(IAspectConfiguration aspectConfiguration) 14 | { 15 | _collections = new List>(); 16 | 17 | foreach (var handler in aspectConfiguration.ValidationHandlers.OrderBy(x => x.Order)) 18 | { 19 | _collections.Add(next => method => handler.Invoke(method, next)); 20 | } 21 | } 22 | 23 | public IAspectValidator Build() 24 | { 25 | AspectValidationDelegate invoke = method => false; 26 | 27 | var count = _collections.Count; 28 | 29 | for (var i = count - 1; i > -1; i--) 30 | { 31 | invoke = _collections[i](invoke); 32 | } 33 | 34 | return new AspectValidator(invoke); 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/AspectCore.Core/DynamicProxy/AttributeInterceptorSelector.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Reflection; 3 | using AspectCore.Extensions.Reflection; 4 | 5 | namespace AspectCore.DynamicProxy 6 | { 7 | [NonAspect] 8 | public sealed class AttributeInterceptorSelector : IInterceptorSelector 9 | { 10 | public IEnumerable Select(MethodInfo method) 11 | { 12 | foreach(var attribute in method.DeclaringType.GetTypeInfo().GetReflector().GetCustomAttributes()) 13 | { 14 | if (attribute is IInterceptor interceptor) 15 | yield return interceptor; 16 | } 17 | foreach (var attribute in method.GetReflector().GetCustomAttributes()) 18 | { 19 | if (attribute is IInterceptor interceptor) 20 | yield return interceptor; 21 | } 22 | } 23 | } 24 | } -------------------------------------------------------------------------------- /src/AspectCore.Core/DynamicProxy/InterceptorSelectorEqualityComparer.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace AspectCore.DynamicProxy 4 | { 5 | [NonAspect] 6 | internal class InterceptorSelectorEqualityComparer : IEqualityComparer where T : class 7 | { 8 | public bool Equals(T x, T y) 9 | { 10 | if (x == null || y == null) 11 | { 12 | return false; 13 | } 14 | if (x == y) 15 | { 16 | return true; 17 | } 18 | if (x.GetType() == y.GetType()) 19 | { 20 | return true; 21 | } 22 | return false; 23 | } 24 | 25 | public int GetHashCode(T obj) 26 | { 27 | return obj.GetType().GetHashCode(); 28 | } 29 | } 30 | } -------------------------------------------------------------------------------- /src/AspectCore.Core/DynamicProxy/Parameters/EnableParameterAspectExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using AspectCore.Configuration; 3 | 4 | namespace AspectCore.DynamicProxy.Parameters 5 | { 6 | public static class EnableParameterAspectExtensions 7 | { 8 | public static IAspectConfiguration EnableParameterAspect(this IAspectConfiguration configuration, params AspectPredicate[] predicates) 9 | { 10 | if (configuration == null) 11 | { 12 | throw new ArgumentNullException(nameof(configuration)); 13 | } 14 | configuration.Interceptors.AddTyped(predicates); 15 | return configuration; 16 | } 17 | } 18 | } -------------------------------------------------------------------------------- /src/AspectCore.Core/DynamicProxy/Parameters/NotNullAttribute.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading.Tasks; 3 | 4 | namespace AspectCore.DynamicProxy.Parameters 5 | { 6 | public class NotNullAttribute : ParameterInterceptorAttribute 7 | { 8 | public string Message { get; set; } 9 | 10 | public override Task Invoke(ParameterAspectContext context, ParameterAspectDelegate next) 11 | { 12 | if (context.Parameter.Value == null) 13 | { 14 | throw new ArgumentNullException(context.Parameter.Name, Message); 15 | } 16 | return next(context); 17 | } 18 | } 19 | } -------------------------------------------------------------------------------- /src/AspectCore.Core/DynamicProxy/Parameters/ParameterAspectInvoker.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | using AspectCore.Utils; 6 | 7 | namespace AspectCore.DynamicProxy.Parameters 8 | { 9 | internal class ParameterAspectInvoker 10 | { 11 | private readonly IList> delegates = new List>(); 12 | 13 | public void AddDelegate(Func parameterAspectDelegate) 14 | { 15 | delegates.Add(next => ctx => parameterAspectDelegate(ctx, next)); 16 | } 17 | 18 | private ParameterAspectDelegate Build() 19 | { 20 | ParameterAspectDelegate invoke = ctx => TaskUtils.CompletedTask; 21 | 22 | foreach (var next in delegates.Reverse()) 23 | { 24 | invoke = next(invoke); 25 | } 26 | 27 | return invoke; 28 | } 29 | 30 | public Task Invoke(ParameterAspectContext context) 31 | { 32 | return Build()(context); 33 | } 34 | 35 | public void Reset() 36 | { 37 | delegates.Clear(); 38 | } 39 | } 40 | } -------------------------------------------------------------------------------- /src/AspectCore.Core/DynamicProxy/ProxyGeneratorBuilder.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using AspectCore.Configuration; 4 | using AspectCore.DependencyInjection; 5 | 6 | namespace AspectCore.DynamicProxy 7 | { 8 | [NonAspect] 9 | public sealed class ProxyGeneratorBuilder 10 | { 11 | private readonly IAspectConfiguration _configuration; 12 | private readonly IServiceContext _serviceContext; 13 | 14 | public ProxyGeneratorBuilder() 15 | { 16 | _configuration = new AspectConfiguration(); 17 | _serviceContext = new ServiceContext(_configuration); 18 | } 19 | 20 | public ProxyGeneratorBuilder Configure(Action options = null) 21 | { 22 | options?.Invoke(_configuration); 23 | return this; 24 | } 25 | 26 | public ProxyGeneratorBuilder ConfigureService(Action options = null) 27 | { 28 | options?.Invoke(_serviceContext); 29 | return this; 30 | } 31 | 32 | public IProxyGenerator Build() 33 | { 34 | var serviceResolver = _serviceContext.Build(); 35 | return new DisposedProxyGenerator(serviceResolver); 36 | } 37 | } 38 | } -------------------------------------------------------------------------------- /src/AspectCore.Core/DynamicProxy/ValidationHandlers/AttributeAspectValidationHandler.cs: -------------------------------------------------------------------------------- 1 | using System.Linq; 2 | using System.Reflection; 3 | 4 | namespace AspectCore.DynamicProxy 5 | { 6 | [NonAspect] 7 | public sealed class AttributeAspectValidationHandler : IAspectValidationHandler 8 | { 9 | public int Order { get; } = 13; 10 | 11 | public bool Invoke(AspectValidationContext context, AspectValidationDelegate next) 12 | { 13 | var declaringType = context.Method.DeclaringType.GetTypeInfo(); 14 | 15 | if (IsAttributeAspect(declaringType) || IsAttributeAspect(context.Method)) 16 | { 17 | return true; 18 | } 19 | 20 | return next(context); 21 | } 22 | 23 | private bool IsAttributeAspect(MemberInfo member) 24 | { 25 | return member.CustomAttributes.Any(data => typeof(IInterceptor).GetTypeInfo().IsAssignableFrom(data.AttributeType)); 26 | } 27 | } 28 | } -------------------------------------------------------------------------------- /src/AspectCore.Core/DynamicProxy/ValidationHandlers/CacheAspectValidationHandler.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Concurrent; 3 | using System.Reflection; 4 | 5 | namespace AspectCore.DynamicProxy 6 | { 7 | public sealed class CacheAspectValidationHandler : IAspectValidationHandler 8 | { 9 | private readonly ConcurrentDictionary detectorCache = new ConcurrentDictionary(); 10 | 11 | public int Order { get; } = -101; 12 | 13 | public bool Invoke(AspectValidationContext context, AspectValidationDelegate next) 14 | { 15 | return detectorCache.GetOrAdd(context, tuple => next(context)); 16 | } 17 | } 18 | } -------------------------------------------------------------------------------- /src/AspectCore.Core/DynamicProxy/ValidationHandlers/OverwriteAspectValidationHandler.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using AspectCore.Extensions.Reflection; 3 | 4 | namespace AspectCore.DynamicProxy 5 | { 6 | public sealed class OverwriteAspectValidationHandler : IAspectValidationHandler 7 | { 8 | public int Order { get; } = 1; 9 | 10 | public bool Invoke(AspectValidationContext context, AspectValidationDelegate next) 11 | { 12 | var method = context.Method; 13 | var declaringType = method.DeclaringType.GetTypeInfo(); 14 | if (!declaringType.CanInherited()) 15 | { 16 | return false; 17 | } 18 | if (method.IsNonAspect()) 19 | { 20 | return false; 21 | } 22 | if (!method.IsVisibleAndVirtual()) 23 | { 24 | if (context.StrictValidation) 25 | { 26 | return false; 27 | } 28 | if (!method.Attributes.HasFlag(MethodAttributes.Virtual | MethodAttributes.NewSlot | MethodAttributes.Final)) 29 | return false; 30 | } 31 | 32 | return next(context); 33 | } 34 | } 35 | } -------------------------------------------------------------------------------- /src/AspectCore.Core/Utils/ActivatorUtils.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using AspectCore.DependencyInjection; 3 | 4 | namespace AspectCore.Utils 5 | { 6 | internal static class ActivatorUtils 7 | { 8 | public static object CreateManyEnumerable(Type elementType) 9 | { 10 | return Activator.CreateInstance(typeof(ManyEnumerable<>).MakeGenericType(elementType), Array.CreateInstance(elementType, 0)); 11 | } 12 | 13 | public static object CreateManyEnumerable(Type elementType, Array array) 14 | { 15 | return Activator.CreateInstance(typeof(ManyEnumerable<>).MakeGenericType(elementType), array); 16 | } 17 | } 18 | } -------------------------------------------------------------------------------- /src/AspectCore.Core/Utils/ArrayUtils.cs: -------------------------------------------------------------------------------- 1 | namespace AspectCore.Utils 2 | { 3 | internal static class ArrayUtils 4 | { 5 | public static T[] Empty() 6 | { 7 | return EmptyArray.Value; 8 | } 9 | 10 | private static class EmptyArray 11 | { 12 | public static readonly T[] Value = new T[0]; 13 | } 14 | } 15 | } -------------------------------------------------------------------------------- /src/AspectCore.Core/Utils/NoSyncContextScope.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading; 3 | using System.Threading.Tasks; 4 | 5 | namespace AspectCore.Core.Utils 6 | { 7 | internal static class NoSyncContextScope 8 | { 9 | // See: https://stackoverflow.com/questions/28305968/use-task-run-in-synchronous-method-to-avoid-deadlock-waiting-on-async-method 10 | private static IDisposable Enter() 11 | { 12 | var context = SynchronizationContext.Current; 13 | SynchronizationContext.SetSynchronizationContext(null); 14 | return new Disposable(context); 15 | } 16 | 17 | private struct Disposable : IDisposable 18 | { 19 | private readonly SynchronizationContext _synchronizationContext; 20 | 21 | public Disposable(SynchronizationContext synchronizationContext) 22 | { 23 | _synchronizationContext = synchronizationContext; 24 | } 25 | 26 | public void Dispose() => 27 | SynchronizationContext.SetSynchronizationContext(_synchronizationContext); 28 | } 29 | 30 | public static void Run(Task task) 31 | { 32 | using (Enter()) 33 | { 34 | task.GetAwaiter().GetResult(); 35 | } 36 | } 37 | 38 | public static T Run(Task task) 39 | { 40 | using (Enter()) 41 | { 42 | return task.GetAwaiter().GetResult(); 43 | } 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/AspectCore.Core/Utils/PropertyInjectionUtils.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Concurrent; 3 | using System.Linq; 4 | using System.Reflection; 5 | using AspectCore.Extensions.Reflection; 6 | using AspectCore.DependencyInjection; 7 | 8 | namespace AspectCore.Utils 9 | { 10 | internal static class PropertyInjectionUtils 11 | { 12 | private readonly static ConcurrentDictionary dictionary = new ConcurrentDictionary(); 13 | 14 | public static bool TypeRequired(Type type) 15 | { 16 | if (type == null) 17 | { 18 | throw new ArgumentNullException(nameof(type)); 19 | } 20 | return dictionary.GetOrAdd(type, _ => type.GetTypeInfo().GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) 21 | .Where(x => x.CanWrite).Any(x => x.GetReflector().IsDefined())); 22 | } 23 | 24 | public static bool Required(object instance) 25 | { 26 | if (instance == null) 27 | { 28 | return false; 29 | } 30 | return TypeRequired(instance.GetType()); 31 | } 32 | } 33 | } -------------------------------------------------------------------------------- /src/AspectCore.Core/Utils/TaskUtils.cs: -------------------------------------------------------------------------------- 1 | using System.Threading.Tasks; 2 | 3 | namespace AspectCore.Utils 4 | { 5 | internal static class TaskUtils 6 | { 7 | internal static readonly Task CompletedTask = Task.FromResult(false); 8 | } 9 | 10 | internal static class TaskUtils 11 | { 12 | internal static readonly Task CompletedTask = Task.FromResult(default(T)); 13 | } 14 | } -------------------------------------------------------------------------------- /src/AspectCore.Extensions.AspNetCore/AspectCore.Extensions.AspNetCore.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | net9.0;net8.0;net7.0;net6.0 7 | true 8 | true 9 | snupkg 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /src/AspectCore.Extensions.AspNetCore/Extensions/AspectContextExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Microsoft.AspNetCore.Http; 3 | using Microsoft.Extensions.DependencyInjection; 4 | 5 | namespace AspectCore.DynamicProxy 6 | { 7 | public static class AspectContextExtensions 8 | { 9 | public static HttpContext GetHttpContext(this AspectContext aspectContext) 10 | { 11 | if (aspectContext == null) 12 | { 13 | throw new ArgumentNullException(nameof(aspectContext)); 14 | } 15 | var httpContextAccessor = aspectContext.ServiceProvider.GetService(); 16 | return httpContextAccessor?.HttpContext; 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/AspectCore.Extensions.AspNetCore/Extensions/ConfigurationExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using AspectCore.Configuration; 3 | 4 | namespace AspectCore.Extensions.AspNetCore 5 | { 6 | public static class ConfigurationExtensions 7 | { 8 | public static IAspectConfiguration AddMethodExecuteLogging(this IAspectConfiguration configuration, params AspectPredicate[] predicates) 9 | { 10 | if (configuration == null) 11 | { 12 | throw new ArgumentNullException(nameof(configuration)); 13 | } 14 | configuration.Interceptors.AddTyped(predicates); 15 | return configuration; 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/AspectCore.Extensions.AspNetCore/Filters/ModelStateAdapterAttribute.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.AspNetCore.Mvc.Filters; 2 | 3 | namespace AspectCore.Extensions.AspNetCore.Filters 4 | { 5 | public class ModelStateAdapterAttribute : ActionFilterAttribute 6 | { 7 | public override void OnActionExecuting(ActionExecutingContext context) 8 | { 9 | context.HttpContext.Items["modelstate-aspectcore"] = context.ModelState; 10 | base.OnActionExecuting(context); 11 | } 12 | } 13 | } -------------------------------------------------------------------------------- /src/AspectCore.Extensions.AspNetCore/Http/ExecutionContextHelper.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using System.Threading; 5 | 6 | namespace AspectCore.Extensions.AspNetCore.Http 7 | { 8 | public static class ExecutionContextHelper 9 | { 10 | public static ExecutionContext Current { get; set; } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/AspectCore.Extensions.AspectScope/AspectCore.Extensions.AspectScope.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | ScopedContext extension system for AspectCore Framework. 7 | AspectCore.Extensions.ScopedContext 8 | AspectCore.Extensions.AspectScope 9 | AspectCore.Extensions.AspectScope 10 | AspectCore.Extensions.AspectScope 11 | DynamicProxy;Aop;Aspect;AspectCore;Intercepter 12 | ScopedContext extension system for AspectCore Framework. 13 | net9.0;net8.0;net7.0;net6.0;netstandard2.1;netstandard2.0 14 | true 15 | true 16 | snupkg 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /src/AspectCore.Extensions.AspectScope/IAspectScheduler.cs: -------------------------------------------------------------------------------- 1 | using AspectCore.DynamicProxy; 2 | 3 | namespace AspectCore.Extensions.AspectScope 4 | { 5 | [NonAspect] 6 | public interface IAspectScheduler 7 | { 8 | bool TryEnter(AspectContext context); 9 | 10 | void Release(AspectContext context); 11 | 12 | bool TryRelate(AspectContext context, IInterceptor interceptor); 13 | 14 | AspectContext[] GetCurrentContexts(); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/AspectCore.Extensions.AspectScope/IScopeInterceptor.cs: -------------------------------------------------------------------------------- 1 | using AspectCore.DynamicProxy; 2 | 3 | namespace AspectCore.Extensions.AspectScope 4 | { 5 | [NonAspect] 6 | public interface IScopeInterceptor : IInterceptor 7 | { 8 | Scope Scope { get; set; } 9 | } 10 | } -------------------------------------------------------------------------------- /src/AspectCore.Extensions.AspectScope/SchedulerHelpers.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Diagnostics; 4 | using System.Collections.Concurrent; 5 | using System.Reflection; 6 | using System.Text; 7 | using AspectCore.DynamicProxy; 8 | using System.Linq; 9 | 10 | namespace AspectCore.Extensions.AspectScope 11 | { 12 | //internal static class SchedulerHelpers 13 | //{ 14 | // private static readonly HashSet _invokes = new HashSet { "Invoke", "InvokeTask", "InvokeValueTask" }; 15 | // private static readonly ConcurrentDictionary _aspectMap = new ConcurrentDictionary(); 16 | 17 | // public static bool IsAspectInvoke(MethodInfo method) 18 | // { 19 | // if (method == null) 20 | // { 21 | // return false; 22 | // } 23 | // return _aspectMap.GetOrAdd(method, m => _invokes.Contains(m.Name) && typeof(IAspectActivator).IsAssignableFrom(m.DeclaringType)); 24 | // } 25 | 26 | // public static MethodInfo[] GetInvokeMethods() 27 | // { 28 | // var frames =new StackTrace().GetFrames(); 29 | // return frames.Select(x => x.GetMethod() as MethodInfo).Where(x => IsAspectInvoke(x)).ToArray(); 30 | // } 31 | //} 32 | } -------------------------------------------------------------------------------- /src/AspectCore.Extensions.AspectScope/Scope.cs: -------------------------------------------------------------------------------- 1 | namespace AspectCore.Extensions.AspectScope 2 | { 3 | public enum Scope 4 | { 5 | None, 6 | 7 | Nested, 8 | 9 | Aspect 10 | } 11 | } -------------------------------------------------------------------------------- /src/AspectCore.Extensions.AspectScope/ScopeAspectContextFactory.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using AspectCore.DynamicProxy; 3 | 4 | namespace AspectCore.Extensions.AspectScope 5 | { 6 | [NonAspect] 7 | public sealed class ScopeAspectContextFactory : IAspectContextFactory 8 | { 9 | private readonly IAspectScheduler _aspectScheduler; 10 | private readonly AspectContextFactory _aspectContextFactory; 11 | 12 | public ScopeAspectContextFactory(IServiceProvider serviceProvider, IAspectScheduler aspectContextScheduler) 13 | { 14 | _aspectScheduler = aspectContextScheduler ?? throw new ArgumentNullException(nameof(aspectContextScheduler)); 15 | _aspectContextFactory = new AspectContextFactory(serviceProvider); 16 | } 17 | 18 | public AspectContext CreateContext(AspectActivatorContext activatorContext) 19 | { 20 | var aspectContext = _aspectContextFactory.CreateContext(activatorContext); 21 | if (!_aspectScheduler.TryEnter(aspectContext)) 22 | { 23 | throw new InvalidOperationException("Error occurred in the schedule AspectContext."); 24 | } 25 | return aspectContext; 26 | } 27 | 28 | public void ReleaseContext(AspectContext aspectContext) 29 | { 30 | _aspectContextFactory.ReleaseContext(aspectContext); 31 | _aspectScheduler.Release(aspectContext); 32 | } 33 | } 34 | } -------------------------------------------------------------------------------- /src/AspectCore.Extensions.AspectScope/ScopeInterceptorAttribute.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using AspectCore.DynamicProxy; 3 | 4 | namespace AspectCore.Extensions.AspectScope 5 | { 6 | [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method | AttributeTargets.Interface, AllowMultiple = false, Inherited = false)] 7 | [NonAspect] 8 | public abstract class ScopeInterceptorAttribute : AbstractInterceptorAttribute, IScopeInterceptor 9 | { 10 | public virtual Scope Scope { get; set; } = Scope.None; 11 | } 12 | } -------------------------------------------------------------------------------- /src/AspectCore.Extensions.AspectScope/ServiceContainerExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using AspectCore.DynamicProxy; 3 | using AspectCore.DependencyInjection; 4 | 5 | namespace AspectCore.Extensions.AspectScope 6 | { 7 | public static class ServiceContainerExtensions 8 | { 9 | public static IServiceContext AddAspectScope(this IServiceContext services) 10 | { 11 | if (services == null) 12 | { 13 | throw new ArgumentNullException(nameof(services)); 14 | } 15 | services.AddType(Lifetime.Scoped); 16 | services.AddType(Lifetime.Scoped); 17 | services.AddType(Lifetime.Scoped); 18 | return services; 19 | } 20 | } 21 | } -------------------------------------------------------------------------------- /src/AspectCore.Extensions.Autofac/AspectCore.Extensions.Autofac.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | Interceptor and dynamicProxy support for Autofac via AspectCore Framework. 7 | AspectCore.Extensions.Autofac 8 | AspectCore.Extensions.Autofac 9 | AspectCore.Extensions.Autofac 10 | DynamicProxy;Aop;Autofac;AspectCore 11 | Interceptor and dynamicProxy support for Autofac via AspectCore Framework. 12 | net9.0;net8.0;net7.0;net6.0;netstandard2.1;netstandard2.0 13 | true 14 | true 15 | snupkg 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /src/AspectCore.Extensions.Autofac/AutofacScopeResolverFactory.cs: -------------------------------------------------------------------------------- 1 | using AspectCore.DynamicProxy; 2 | using AspectCore.DependencyInjection; 3 | using Autofac; 4 | 5 | namespace AspectCore.Extensions.Autofac 6 | { 7 | [NonAspect] 8 | internal class AutofacScopeResolverFactory : IScopeResolverFactory 9 | { 10 | private readonly ILifetimeScope _lifetimeScope; 11 | 12 | public AutofacScopeResolverFactory(ILifetimeScope lifetimeScope) 13 | { 14 | this._lifetimeScope = lifetimeScope; 15 | } 16 | 17 | public IServiceResolver CreateScope() 18 | { 19 | return new AutofacServiceResolver(_lifetimeScope.BeginLifetimeScope()); 20 | } 21 | } 22 | } -------------------------------------------------------------------------------- /src/AspectCore.Extensions.Autofac/AutofacServiceResolver.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using AspectCore.DynamicProxy; 3 | using AspectCore.DependencyInjection; 4 | using Autofac; 5 | 6 | namespace AspectCore.Extensions.Autofac 7 | { 8 | [NonAspect] 9 | internal class AutofacServiceResolver : IServiceResolver 10 | { 11 | private readonly IComponentContext _componentContext; 12 | 13 | public AutofacServiceResolver(IComponentContext componentContext) 14 | { 15 | _componentContext = componentContext; 16 | } 17 | 18 | public void Dispose() 19 | { 20 | var d = _componentContext as IDisposable; 21 | d?.Dispose(); 22 | } 23 | 24 | public object GetService(Type serviceType) 25 | { 26 | return _componentContext.ResolveOptional(serviceType); 27 | } 28 | 29 | public object Resolve(Type serviceType) 30 | { 31 | return _componentContext.ResolveOptional(serviceType); 32 | } 33 | 34 | #if NET8_0_OR_GREATER 35 | public object GetKeyedService(Type serviceType, object serviceKey) 36 | { 37 | throw new NotImplementedException(); 38 | } 39 | 40 | public object GetRequiredKeyedService(Type serviceType, object serviceKey) 41 | { 42 | throw new NotImplementedException(); 43 | } 44 | #endif 45 | } 46 | } -------------------------------------------------------------------------------- /src/AspectCore.Extensions.Autofac/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyConfiguration("")] 9 | [assembly: AssemblyCompany("")] 10 | [assembly: AssemblyProduct("AspectCore.Container.Autofac")] 11 | [assembly: AssemblyTrademark("")] 12 | 13 | // Setting ComVisible to false makes the types in this assembly not visible 14 | // to COM components. If you need to access a type in this assembly from 15 | // COM, set the ComVisible attribute to true on that type. 16 | [assembly: ComVisible(false)] 17 | 18 | // The following GUID is for the ID of the typelib if this project is exposed to COM 19 | [assembly: Guid("05c1db04-90da-4d5d-a2a0-655dcc14e1f8")] 20 | -------------------------------------------------------------------------------- /src/AspectCore.Extensions.Configuration/Attributes/ConfigurationBindingAttribute.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace AspectCore.Extensions.Configuration 4 | { 5 | public class ConfigurationBindingAttribute : ConfigurationMetadataAttribute 6 | { 7 | public override string[] Sections { get; } 8 | 9 | public override string Key { get; } = null; 10 | 11 | public override ConfigurationBindType Type { get; } = ConfigurationBindType.Class; 12 | 13 | public ConfigurationBindingAttribute(params string[] sections) 14 | { 15 | Sections = sections; 16 | } 17 | } 18 | } -------------------------------------------------------------------------------- /src/AspectCore.Extensions.Configuration/Attributes/ConfigurationMetadataAttribute.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | 4 | namespace AspectCore.Extensions.Configuration 5 | { 6 | public abstract class ConfigurationMetadataAttribute : Attribute, IConfigurationMetadataProvider 7 | { 8 | public abstract string[] Sections { get; } 9 | public abstract string Key { get; } 10 | public abstract ConfigurationBindType Type { get; } 11 | 12 | public string GetSection() 13 | { 14 | if (Sections == null || Sections.Length == 0) 15 | { 16 | return null; 17 | } 18 | 19 | if (Sections.Length ==1) 20 | { 21 | return Sections[0]; 22 | } 23 | 24 | return Sections.Aggregate((x, y) => x + ":" + y); 25 | } 26 | } 27 | } -------------------------------------------------------------------------------- /src/AspectCore.Extensions.Configuration/Attributes/ConfigurationValueAttribute.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace AspectCore.Extensions.Configuration 4 | { 5 | public class ConfigurationValueAttribute : ConfigurationMetadataAttribute 6 | { 7 | public override string Key { get; } 8 | 9 | public override ConfigurationBindType Type { get; } = ConfigurationBindType.Value; 10 | 11 | public override string[] Sections { get; } 12 | 13 | public ConfigurationValueAttribute(string key, params string[] sections) 14 | { 15 | Key = key; 16 | Sections = sections; 17 | } 18 | } 19 | } -------------------------------------------------------------------------------- /src/AspectCore.Extensions.Configuration/ConfigurationBindType.cs: -------------------------------------------------------------------------------- 1 | namespace AspectCore.Extensions.Configuration 2 | { 3 | public enum ConfigurationBindType 4 | { 5 | Value, 6 | Class 7 | } 8 | } -------------------------------------------------------------------------------- /src/AspectCore.Extensions.Configuration/IConfigurationMetadataProvider.cs: -------------------------------------------------------------------------------- 1 | namespace AspectCore.Extensions.Configuration 2 | { 3 | public interface IConfigurationMetadataProvider 4 | { 5 | string[] Sections { get; } 6 | 7 | string Key { get; } 8 | 9 | ConfigurationBindType Type { get; } 10 | } 11 | } -------------------------------------------------------------------------------- /src/AspectCore.Extensions.Configuration/ServiceContainerExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using AspectCore.DependencyInjection; 3 | 4 | namespace AspectCore.Extensions.Configuration 5 | { 6 | public static class ServiceContainerExtensions 7 | { 8 | public static IServiceContext AddConfigurationInject(this IServiceContext context) 9 | { 10 | if (context == null) 11 | { 12 | throw new ArgumentNullException(nameof(context)); 13 | } 14 | 15 | context.AddType(Lifetime.Singleton); 16 | return context; 17 | } 18 | } 19 | } -------------------------------------------------------------------------------- /src/AspectCore.Extensions.DataAnnotations/AnnotationPropertyValidator.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.ComponentModel.DataAnnotations; 3 | using AspectCore.DynamicProxy; 4 | using AspectCore.Extensions.DataValidation; 5 | 6 | namespace AspectCore.Extensions.DataAnnotations 7 | { 8 | [NonAspect] 9 | public class AnnotationPropertyValidator : IPropertyValidator 10 | { 11 | public IEnumerable Validate(PropertyValidationContext propertyValidationContext) 12 | { 13 | var propertyMetaData = propertyValidationContext.PropertyMetaData; 14 | foreach (var attribute in propertyMetaData.Attributes) 15 | { 16 | if (attribute is ValidationAttribute validation) 17 | { 18 | var validationContext = new ValidationContext(propertyMetaData.Container ?? propertyMetaData.Value, null, null) 19 | { 20 | MemberName = propertyMetaData.MemberName, 21 | DisplayName = propertyMetaData.DisplayName 22 | }; 23 | var result = validation.GetValidationResult(propertyMetaData.Value, validationContext); 24 | if (result != ValidationResult.Success) 25 | { 26 | yield return new DataValidationError(propertyMetaData.MemberName, result.ErrorMessage); 27 | } 28 | } 29 | } 30 | } 31 | } 32 | } -------------------------------------------------------------------------------- /src/AspectCore.Extensions.DataAnnotations/AspectCore.Extensions.DataAnnotations.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | DataAnnotations extension system for AspectCore Framework. 7 | AspectCore.Extensions.DataAnnotations 8 | AspectCore.Extensions.DataAnnotations 9 | AspectCore.Extensions.DataAnnotations 10 | DataValidation;DataAnnotations;AspectCore;AOP 11 | DataAnnotations extension system for AspectCore Framework. 12 | net9.0;net8.0;net7.0;net6.0;netstandard2.1;netstandard2.0 13 | true 14 | true 15 | snupkg 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /src/AspectCore.Extensions.DataAnnotations/ServiceContainerExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using AspectCore.Configuration; 3 | using AspectCore.Extensions.DataValidation; 4 | using AspectCore.DependencyInjection; 5 | 6 | namespace AspectCore.Extensions.DataAnnotations 7 | { 8 | public static class ServiceContainerExtensions 9 | { 10 | public static IServiceContext AddDataAnnotations(this IServiceContext services, params AspectPredicate[] predicates) 11 | { 12 | if (services == null) 13 | { 14 | throw new ArgumentNullException(nameof(services)); 15 | } 16 | services.AddType(); 17 | services.AddType(); 18 | services.AddType(); 19 | services.Configuration.Interceptors.AddTyped(predicates); 20 | return services; 21 | } 22 | } 23 | } -------------------------------------------------------------------------------- /src/AspectCore.Extensions.DataValidation/AspectContextExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using AspectCore.DynamicProxy; 5 | 6 | namespace AspectCore.Extensions.DataValidation 7 | { 8 | public static class AspectContextExtensions 9 | { 10 | private const string DataValidationContextKey = "DataValidation-Context"; 11 | 12 | public static DataValidationContext GetDataValidationContext(this AspectContext aspectContext) 13 | { 14 | if (aspectContext == null) 15 | { 16 | throw new ArgumentNullException(nameof(aspectContext)); 17 | } 18 | return aspectContext.AdditionalData[DataValidationContextKey] as DataValidationContext; 19 | } 20 | 21 | public static void SetDataValidationContext(this AspectContext aspectContext, DataValidationContext dataValidationContext) 22 | { 23 | if (aspectContext == null) 24 | { 25 | throw new ArgumentNullException(nameof(aspectContext)); 26 | } 27 | if (dataValidationContext == null) 28 | { 29 | throw new ArgumentNullException(nameof(dataValidationContext)); 30 | } 31 | aspectContext.AdditionalData[DataValidationContextKey] = dataValidationContext; 32 | } 33 | } 34 | } -------------------------------------------------------------------------------- /src/AspectCore.Extensions.DataValidation/AspectCore.Extensions.DataValidation.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | DataValidation extension system for AspectCore Framework. 7 | AspectCore.Extensions.DataValidation 8 | AspectCore.Extensions.DataValidation 9 | AspectCore.Extensions.DataValidation 10 | DataValidation;AspectCore;AOP 11 | DataValidation extension system for AspectCore Framework. 12 | net9.0;net8.0;net7.0;net6.0;netstandard2.1;netstandard2.0 13 | true 14 | true 15 | snupkg 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /src/AspectCore.Extensions.DataValidation/DataMetaData.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | using AspectCore.DynamicProxy.Parameters; 4 | using AspectCore.Extensions.Reflection; 5 | using System.ComponentModel.DataAnnotations; 6 | 7 | namespace AspectCore.Extensions.DataValidation 8 | { 9 | public sealed class DataMetaData 10 | { 11 | public Type Type { get; } 12 | 13 | public Attribute[] Attributes { get; } 14 | 15 | public object Value { get; } 16 | 17 | public DataValidationErrorCollection Errors { get; } 18 | 19 | public DataValidationState State { get; set; } 20 | 21 | public DataMetaData(Parameter paramter) 22 | { 23 | Type = paramter.Type; 24 | Value = paramter.Value; 25 | Attributes = paramter.ParameterInfo.GetReflector().GetCustomAttributes(); 26 | Errors = new DataValidationErrorCollection(); 27 | } 28 | } 29 | } -------------------------------------------------------------------------------- /src/AspectCore.Extensions.DataValidation/DataState.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace AspectCore.Extensions.DataValidation 6 | { 7 | internal class DataState : IDataState 8 | { 9 | private readonly bool _isDataValid; 10 | 11 | public bool IsValid 12 | { 13 | get 14 | { 15 | return _isDataValid && Errors.Count == 0; 16 | } 17 | } 18 | 19 | public DataValidationErrorCollection Errors { get; } 20 | 21 | public DataState(bool isValid, DataValidationErrorCollection dataValidationErrors) 22 | { 23 | _isDataValid = isValid; 24 | Errors = dataValidationErrors ?? throw new ArgumentNullException(nameof(dataValidationErrors)); 25 | } 26 | } 27 | } -------------------------------------------------------------------------------- /src/AspectCore.Extensions.DataValidation/DataStateFactory.cs: -------------------------------------------------------------------------------- 1 | using System.Linq; 2 | 3 | namespace AspectCore.Extensions.DataValidation 4 | { 5 | public class DataStateFactory : IDataStateFactory 6 | { 7 | public IDataState CreateDataState(DataValidationContext dataValidationContext) 8 | { 9 | var dataValidationErrors = new DataValidationErrorCollection(); 10 | foreach (var error in dataValidationContext.DataMetaDatas.SelectMany(x => x.Errors)) 11 | dataValidationErrors.Add(error); 12 | var isValid = dataValidationContext.DataMetaDatas.All(x => x.State != DataValidationState.Invalid); 13 | return new DataState(isValid, dataValidationErrors); 14 | } 15 | } 16 | } -------------------------------------------------------------------------------- /src/AspectCore.Extensions.DataValidation/DataValidationContext.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Linq; 3 | using AspectCore.DynamicProxy; 4 | using AspectCore.DynamicProxy.Parameters; 5 | 6 | namespace AspectCore.Extensions.DataValidation 7 | { 8 | public sealed class DataValidationContext 9 | { 10 | public IEnumerable DataMetaDatas { get; } 11 | 12 | public AspectContext AspectContext { get; } 13 | 14 | public DataValidationContext(AspectContext aspectContext) 15 | { 16 | AspectContext = aspectContext; 17 | DataMetaDatas = aspectContext.GetParameters().Select(param => new DataMetaData(param)).ToArray(); 18 | } 19 | } 20 | } -------------------------------------------------------------------------------- /src/AspectCore.Extensions.DataValidation/DataValidationError.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace AspectCore.Extensions.DataValidation 4 | { 5 | public sealed class DataValidationError 6 | { 7 | public string Key { get; } 8 | 9 | public string ErrorMessage { get; } 10 | 11 | public Exception Exception { get; } 12 | 13 | public DataValidationError(string key, string errorMessage) 14 | { 15 | Key = key ?? string.Empty; 16 | ErrorMessage = errorMessage ?? string.Empty; 17 | } 18 | 19 | public DataValidationError(string key, Exception exception) 20 | : this(key, null, exception) 21 | { 22 | } 23 | 24 | public DataValidationError(string key, string errorMessage, Exception exception) 25 | : this(key, errorMessage) 26 | { 27 | if (exception == null) 28 | { 29 | throw new ArgumentNullException(nameof(exception)); 30 | } 31 | 32 | Exception = exception; 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/AspectCore.Extensions.DataValidation/DataValidationErrorCollection.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | 5 | namespace AspectCore.Extensions.DataValidation 6 | { 7 | public sealed class DataValidationErrorCollection : IEnumerable, IEnumerable 8 | { 9 | private readonly ICollection collection = new List(); 10 | 11 | public int Count 12 | { 13 | get 14 | { 15 | return collection.Count; 16 | } 17 | } 18 | 19 | public void Add(DataValidationError dataValidationError) 20 | { 21 | if (dataValidationError == null) 22 | { 23 | throw new ArgumentNullException(nameof(dataValidationError)); 24 | } 25 | collection.Add(dataValidationError); 26 | } 27 | 28 | public IEnumerator GetEnumerator() 29 | { 30 | return collection.GetEnumerator(); 31 | } 32 | 33 | IEnumerator IEnumerable.GetEnumerator() 34 | { 35 | return GetEnumerator(); 36 | } 37 | } 38 | } -------------------------------------------------------------------------------- /src/AspectCore.Extensions.DataValidation/DataValidationState.cs: -------------------------------------------------------------------------------- 1 | namespace AspectCore.Extensions.DataValidation 2 | { 3 | public enum DataValidationState 4 | { 5 | Unvalidated, 6 | Invalid, 7 | Valid, 8 | Skipped 9 | } 10 | } -------------------------------------------------------------------------------- /src/AspectCore.Extensions.DataValidation/IDataState.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using AspectCore.DynamicProxy; 5 | 6 | namespace AspectCore.Extensions.DataValidation 7 | { 8 | [NonAspect] 9 | public interface IDataState 10 | { 11 | bool IsValid { get; } 12 | 13 | DataValidationErrorCollection Errors { get; } 14 | } 15 | } -------------------------------------------------------------------------------- /src/AspectCore.Extensions.DataValidation/IDataStateFactory.cs: -------------------------------------------------------------------------------- 1 | using AspectCore.DynamicProxy; 2 | 3 | namespace AspectCore.Extensions.DataValidation 4 | { 5 | [NonAspect] 6 | public interface IDataStateFactory 7 | { 8 | IDataState CreateDataState(DataValidationContext dataValidationContext); 9 | } 10 | } -------------------------------------------------------------------------------- /src/AspectCore.Extensions.DataValidation/IDataStateProvider.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using AspectCore.DynamicProxy; 5 | 6 | namespace AspectCore.Extensions.DataValidation 7 | { 8 | [NonAspect] 9 | public interface IDataStateProvider 10 | { 11 | IDataState DataState { get; set; } 12 | } 13 | } -------------------------------------------------------------------------------- /src/AspectCore.Extensions.DataValidation/IDataValidator.cs: -------------------------------------------------------------------------------- 1 | using AspectCore.DynamicProxy; 2 | 3 | namespace AspectCore.Extensions.DataValidation 4 | { 5 | [NonAspect] 6 | public interface IDataValidator 7 | { 8 | void Validate(DataValidationContext context); 9 | } 10 | } -------------------------------------------------------------------------------- /src/AspectCore.Extensions.DataValidation/IPropertyValidator.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using AspectCore.DynamicProxy; 3 | 4 | namespace AspectCore.Extensions.DataValidation 5 | { 6 | [NonAspect] 7 | public interface IPropertyValidator 8 | { 9 | IEnumerable Validate(PropertyValidationContext propertyValidationContext); 10 | } 11 | } -------------------------------------------------------------------------------- /src/AspectCore.Extensions.DataValidation/PropertyMetaData.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.ComponentModel.DataAnnotations; 3 | using System.Linq; 4 | using System.Reflection; 5 | using AspectCore.Extensions.Reflection; 6 | 7 | namespace AspectCore.Extensions.DataValidation 8 | { 9 | public class PropertyMetaData 10 | { 11 | public Type Type { get; } 12 | 13 | public Attribute[] Attributes { get; } 14 | 15 | public object Value { get; } 16 | 17 | public string DisplayName { get; } 18 | 19 | public string MemberName { get; } 20 | 21 | public object Container { get; set; } 22 | 23 | public PropertyMetaData(PropertyInfo property, object container) 24 | { 25 | if (property == null) 26 | { 27 | throw new ArgumentNullException(nameof(property)); 28 | } 29 | Type = property.PropertyType; 30 | MemberName = property.Name; 31 | Attributes = property.GetReflector().GetCustomAttributes(); 32 | var displayAttribute = Attributes.FirstOrDefault(x => x is DisplayAttribute) as DisplayAttribute; 33 | DisplayName = displayAttribute?.Name ?? MemberName; 34 | if (container != null) 35 | Value = property.GetReflector().GetValue(container); 36 | Container = container; 37 | } 38 | } 39 | } -------------------------------------------------------------------------------- /src/AspectCore.Extensions.DataValidation/PropertyValidationContext.cs: -------------------------------------------------------------------------------- 1 | using AspectCore.DynamicProxy; 2 | 3 | namespace AspectCore.Extensions.DataValidation 4 | { 5 | public sealed class PropertyValidationContext 6 | { 7 | public PropertyMetaData PropertyMetaData { get; } 8 | 9 | public AspectContext AspectContext { get; } 10 | 11 | public PropertyValidationContext(PropertyMetaData propertyMetaData, AspectContext aspectContext) 12 | { 13 | PropertyMetaData = propertyMetaData; 14 | AspectContext = aspectContext; 15 | } 16 | } 17 | } -------------------------------------------------------------------------------- /src/AspectCore.Extensions.DataValidation/SkipValidationAttribute.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace AspectCore.Extensions.DataValidation 4 | { 5 | public class SkipValidationAttribute : Attribute 6 | { 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src/AspectCore.Extensions.DependencyInjection/DynamicProxyServiceProviderFactory.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Microsoft.Extensions.DependencyInjection; 3 | 4 | namespace AspectCore.Extensions.DependencyInjection 5 | { 6 | public class DynamicProxyServiceProviderFactory : IServiceProviderFactory 7 | { 8 | private ServiceProviderOptions _serviceProviderOptions; 9 | 10 | 11 | public DynamicProxyServiceProviderFactory() 12 | : this(null) 13 | { 14 | } 15 | 16 | public DynamicProxyServiceProviderFactory(bool validateScopes) 17 | : this(new ServiceProviderOptions() {ValidateScopes = validateScopes}) 18 | { 19 | } 20 | 21 | public DynamicProxyServiceProviderFactory(ServiceProviderOptions serviceProviderOptions) 22 | { 23 | _serviceProviderOptions = serviceProviderOptions; 24 | } 25 | 26 | public IServiceCollection CreateBuilder(IServiceCollection services) 27 | { 28 | return services; 29 | } 30 | 31 | public IServiceProvider CreateServiceProvider(IServiceCollection containerBuilder) 32 | { 33 | return _serviceProviderOptions == null 34 | ? containerBuilder.BuildDynamicProxyProvider() 35 | : containerBuilder.BuildDynamicProxyProvider(_serviceProviderOptions); 36 | } 37 | } 38 | } -------------------------------------------------------------------------------- /src/AspectCore.Extensions.DependencyInjection/MsdiScopeResolverFactory.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using AspectCore.DependencyInjection; 3 | using Microsoft.Extensions.DependencyInjection; 4 | 5 | namespace AspectCore.Extensions.DependencyInjection 6 | { 7 | internal class MsdiScopeResolverFactory : IScopeResolverFactory 8 | { 9 | private readonly IServiceProvider _serviceProvider; 10 | 11 | public MsdiScopeResolverFactory(IServiceProvider serviceProvider) 12 | { 13 | _serviceProvider = serviceProvider; 14 | } 15 | 16 | public IServiceResolver CreateScope() 17 | { 18 | return new MsdiServiceResolver(_serviceProvider.CreateScope().ServiceProvider); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/AspectCore.Extensions.DependencyInjection/MsdiServiceResolver.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using AspectCore.DependencyInjection; 3 | 4 | namespace AspectCore.Extensions.DependencyInjection 5 | { 6 | internal class MsdiServiceResolver : IServiceResolver 7 | { 8 | private readonly IServiceProvider _serviceProvider; 9 | public MsdiServiceResolver(IServiceProvider serviceProvider) 10 | { 11 | _serviceProvider = serviceProvider; 12 | } 13 | 14 | public void Dispose() 15 | { 16 | var d = _serviceProvider as IDisposable; 17 | d?.Dispose(); 18 | } 19 | 20 | public object GetService(Type serviceType) 21 | { 22 | return _serviceProvider.GetService(serviceType); 23 | } 24 | 25 | public object Resolve(Type serviceType) 26 | { 27 | return _serviceProvider.GetService(serviceType); 28 | } 29 | 30 | #if NET8_0_OR_GREATER 31 | public object GetKeyedService(Type serviceType, object serviceKey) 32 | { 33 | throw new NotImplementedException(); 34 | } 35 | 36 | public object GetRequiredKeyedService(Type serviceType, object serviceKey) 37 | { 38 | throw new NotImplementedException(); 39 | } 40 | #endif 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/AspectCore.Extensions.DependencyInjection/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyConfiguration("")] 9 | [assembly: AssemblyCompany("")] 10 | [assembly: AssemblyProduct("AspectCore Framework")] 11 | [assembly: AssemblyTrademark("")] 12 | 13 | // Setting ComVisible to false makes the types in this assembly not visible 14 | // to COM components. If you need to access a type in this assembly from 15 | // COM, set the ComVisible attribute to true on that type. 16 | [assembly: ComVisible(false)] 17 | 18 | // The following GUID is for the ID of the typelib if this project is exposed to COM 19 | [assembly: Guid("00642272-6be8-430c-93d5-db63fec23184")] 20 | -------------------------------------------------------------------------------- /src/AspectCore.Extensions.DependencyInjection/ServiceContextProviderFactory.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using AspectCore.DynamicProxy; 3 | using AspectCore.DependencyInjection; 4 | using Microsoft.Extensions.DependencyInjection; 5 | 6 | namespace AspectCore.Extensions.DependencyInjection 7 | { 8 | [NonAspect] 9 | public class ServiceContextProviderFactory : IServiceProviderFactory 10 | { 11 | public IServiceContext CreateBuilder(IServiceCollection services) 12 | { 13 | return services.ToServiceContext(); 14 | } 15 | 16 | public IServiceProvider CreateServiceProvider(IServiceContext contextBuilder) 17 | { 18 | return contextBuilder.Build(); 19 | } 20 | } 21 | } -------------------------------------------------------------------------------- /src/AspectCore.Extensions.DependencyInjection/ServiceScope.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using AspectCore.DependencyInjection; 3 | using Microsoft.Extensions.DependencyInjection; 4 | 5 | namespace AspectCore.Extensions.DependencyInjection 6 | { 7 | internal class ServiceScope : IServiceScope 8 | { 9 | private readonly IServiceResolver _serviceResolver; 10 | public IServiceProvider ServiceProvider => _serviceResolver; 11 | 12 | public ServiceScope(IServiceResolver serviceResolver) 13 | { 14 | _serviceResolver = serviceResolver; 15 | } 16 | 17 | public void Dispose() 18 | { 19 | _serviceResolver.Dispose(); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/AspectCore.Extensions.DependencyInjection/ServiceScopeFactory.cs: -------------------------------------------------------------------------------- 1 | using AspectCore.DependencyInjection; 2 | using Microsoft.Extensions.DependencyInjection; 3 | 4 | namespace AspectCore.Extensions.DependencyInjection 5 | { 6 | internal class ServiceScopeFactory : IServiceScopeFactory 7 | { 8 | private readonly IServiceResolver _serviceResolver; 9 | 10 | public ServiceScopeFactory(IServiceResolver serviceResolver) 11 | { 12 | _serviceResolver = serviceResolver; 13 | } 14 | 15 | public IServiceScope CreateScope() 16 | { 17 | return new ServiceScope(_serviceResolver.CreateScope()); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/AspectCore.Extensions.DependencyInjection/SupportRequiredService.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using AspectCore.DependencyInjection; 3 | using Microsoft.Extensions.DependencyInjection; 4 | 5 | namespace AspectCore.Extensions.DependencyInjection 6 | { 7 | internal class SupportRequiredService : ISupportRequiredService 8 | { 9 | private readonly IServiceResolver _serviceResolver; 10 | 11 | public SupportRequiredService(IServiceResolver serviceResolver) 12 | { 13 | _serviceResolver = serviceResolver; 14 | } 15 | 16 | public object GetRequiredService(Type serviceType) 17 | { 18 | if (serviceType == null) 19 | { 20 | throw new ArgumentNullException(nameof(serviceType)); 21 | } 22 | return _serviceResolver.ResolveRequired(serviceType); 23 | } 24 | } 25 | } -------------------------------------------------------------------------------- /src/AspectCore.Extensions.LightInject/AspectCore.Extensions.LightInject.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | Interceptor and dynamicProxy support for LightInject via AspectCore Framework. 7 | DynamicProxy;Aop;LightInject;AspectCore 8 | Interceptor and dynamicProxy support for LightInject via AspectCore Framework. 9 | false 10 | net9.0;net8.0;net7.0;net6.0;netstandard2.1;netstandard2.0 11 | true 12 | true 13 | snupkg 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /src/AspectCore.Extensions.LightInject/LightInjectExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Globalization; 4 | using System.Linq; 5 | using System.Text; 6 | using LightInject; 7 | 8 | namespace AspectCore.Extensions.LightInject 9 | { 10 | internal static class LightInjectExtensions 11 | { 12 | public static IServiceRegistry AddSingleton(this IServiceRegistry services, string name = default) 13 | where T : class 14 | where TImpl : class, T 15 | { 16 | return services.Register(name ?? string.Empty, new PerContainerLifetime()); 17 | } 18 | 19 | public static IServiceRegistry AddSingleton(this IServiceRegistry services, T instance) 20 | where T : class 21 | { 22 | return services.RegisterInstance(instance); 23 | } 24 | 25 | public static IServiceRegistry AddTransient(this IServiceRegistry services, Type service, Type impl) 26 | { 27 | // The default behavior in LightInject is to treat all objects as transients unless otherwise specified. 28 | return services.Register(service, impl); 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/AspectCore.Extensions.LightInject/LightInjectScopeResolverFactory.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using AspectCore.DependencyInjection; 5 | using AspectCore.DynamicProxy; 6 | using LightInject; 7 | 8 | namespace AspectCore.Extensions.LightInject 9 | { 10 | [NonAspect] 11 | internal class LightInjectScopeResolverFactory : IScopeResolverFactory 12 | { 13 | private readonly IServiceContainer _container; 14 | 15 | public LightInjectScopeResolverFactory(IServiceContainer container) 16 | { 17 | _container = container; 18 | } 19 | 20 | public IServiceResolver CreateScope() 21 | { 22 | return new LightInjectServiceResolver(_container.BeginScope()); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/AspectCore.Extensions.Reflection/AspectCore.Extensions.Reflection.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Reflection extension system for AspectCore Framework. 5 | AspectCore.Extensions.Reflection 6 | False 7 | AspectCore.Extensions.Reflection 8 | AspectCore.Extensions.Reflection 9 | Reflection;Aop;DynamicProxy 10 | Reflection extension system for AspectCore Framework. 11 | net9.0;net8.0;net7.0;net6.0;netstandard2.1;netstandard2.0 12 | true 13 | true 14 | snupkg 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /src/AspectCore.Extensions.Reflection/CallOptions.cs: -------------------------------------------------------------------------------- 1 | namespace AspectCore.Extensions.Reflection 2 | { 3 | public enum CallOptions 4 | { 5 | Call, 6 | Callvirt 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src/AspectCore.Extensions.Reflection/ConstructorReflector.OpenGeneric.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Reflection; 4 | using System.Text; 5 | 6 | namespace AspectCore.Extensions.Reflection 7 | { 8 | public partial class ConstructorReflector 9 | { 10 | private class OpenGenericConstructorReflector : ConstructorReflector 11 | { 12 | public OpenGenericConstructorReflector(ConstructorInfo constructorInfo) : base(constructorInfo) 13 | { 14 | } 15 | 16 | protected override Func CreateInvoker() => null; 17 | 18 | public override object Invoke(params object[] args) => throw new InvalidOperationException($"Cannot create an instance of {_reflectionInfo.DeclaringType} because Type.ContainsGenericParameters is true."); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/AspectCore.Extensions.Reflection/Emit/IndexedLocalBuilder.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Reflection.Emit; 3 | 4 | namespace AspectCore.Extensions.Reflection.Emit 5 | { 6 | public struct IndexedLocalBuilder 7 | { 8 | public LocalBuilder LocalBuilder { get; } 9 | public Type LocalType { get; } 10 | public int Index { get; } 11 | public int LocalIndex { get; } 12 | 13 | public IndexedLocalBuilder(LocalBuilder localBuilder, int index) 14 | { 15 | LocalBuilder = localBuilder; 16 | LocalType = localBuilder.LocalType; 17 | LocalIndex = localBuilder.LocalIndex; 18 | Index = index; 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/AspectCore.Extensions.Reflection/Extensions/ParameterInfoExtensions.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | 3 | namespace AspectCore.Extensions.Reflection 4 | { 5 | public static class ParameterInfoExtensions 6 | { 7 | public static bool HasDefaultValueByAttributes(this ParameterInfo parameter) 8 | { 9 | // parameter.HasDefaultValue will throw a FormatException when parameter is DateTime type with default value 10 | return (parameter.Attributes & ParameterAttributes.HasDefault) != 0; 11 | } 12 | 13 | public static object DefaultValueSafely(this ParameterInfo parameter) 14 | { 15 | try 16 | { 17 | // parameter.DefaultValue will throw a FormatException when parameter is DateTime type with default value 18 | return parameter.DefaultValue; 19 | } 20 | catch 21 | { 22 | return null; 23 | } 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/AspectCore.Extensions.Reflection/Factories/ConstructorReflector.Factory.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Reflection; 3 | 4 | namespace AspectCore.Extensions.Reflection 5 | { 6 | public partial class ConstructorReflector 7 | { 8 | internal static ConstructorReflector Create(ConstructorInfo constructorInfo) 9 | { 10 | if (constructorInfo == null) 11 | { 12 | throw new ArgumentNullException(nameof(constructorInfo)); 13 | } 14 | return ReflectorCacheUtils.GetOrAdd(constructorInfo, info => 15 | { 16 | if (info.DeclaringType.GetTypeInfo().ContainsGenericParameters) 17 | { 18 | return new OpenGenericConstructorReflector(info); 19 | } 20 | return new ConstructorReflector(info); 21 | }); 22 | } 23 | } 24 | } -------------------------------------------------------------------------------- /src/AspectCore.Extensions.Reflection/Factories/CustomAttributeReflector.Factory.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | 3 | namespace AspectCore.Extensions.Reflection 4 | { 5 | public partial class CustomAttributeReflector 6 | { 7 | internal static CustomAttributeReflector Create(CustomAttributeData customAttributeData) 8 | { 9 | return ReflectorCacheUtils.GetOrAdd(customAttributeData, data => new CustomAttributeReflector(data)); 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/AspectCore.Extensions.Reflection/Factories/FieldReflector.Factory.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Reflection; 3 | 4 | namespace AspectCore.Extensions.Reflection 5 | { 6 | public partial class FieldReflector 7 | { 8 | internal static FieldReflector Create(FieldInfo reflectionInfo) 9 | { 10 | if (reflectionInfo == null) 11 | { 12 | throw new ArgumentNullException(nameof(reflectionInfo)); 13 | } 14 | 15 | return ReflectorCacheUtils.GetOrAdd(reflectionInfo, CreateInternal); 16 | 17 | FieldReflector CreateInternal(FieldInfo field) 18 | { 19 | if (field.DeclaringType.GetTypeInfo().ContainsGenericParameters) 20 | { 21 | return new OpenGenericFieldReflector(field); 22 | } 23 | 24 | if (field.DeclaringType.IsEnum) 25 | { 26 | return new EnumFieldReflector(field); 27 | } 28 | 29 | if (field.IsStatic) 30 | { 31 | return new StaticFieldReflector(field); 32 | } 33 | 34 | return new FieldReflector(field); 35 | } 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/AspectCore.Extensions.Reflection/Factories/MethodReflector.Factory.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Reflection; 3 | 4 | namespace AspectCore.Extensions.Reflection 5 | { 6 | public partial class MethodReflector 7 | { 8 | internal static MethodReflector Create(MethodInfo reflectionInfo, CallOptions callOption) 9 | { 10 | if (reflectionInfo == null) 11 | { 12 | throw new ArgumentNullException(nameof(reflectionInfo)); 13 | } 14 | return ReflectorCacheUtils, MethodReflector>.GetOrAdd(new Pair(reflectionInfo, callOption), CreateInternal); 15 | 16 | MethodReflector CreateInternal(Pair item) 17 | { 18 | var methodInfo = item.Item1; 19 | if (methodInfo.ContainsGenericParameters) 20 | { 21 | return new OpenGenericMethodReflector(methodInfo); 22 | } 23 | if (methodInfo.IsStatic) 24 | { 25 | return new StaticMethodReflector(methodInfo); 26 | } 27 | if (methodInfo.DeclaringType.GetTypeInfo().IsValueType || callOption == CallOptions.Call) 28 | { 29 | return new CallMethodReflector(methodInfo); 30 | } 31 | return new MethodReflector(methodInfo); 32 | } 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/AspectCore.Extensions.Reflection/Factories/ParameterReflector.Factory.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Reflection; 3 | 4 | namespace AspectCore.Extensions.Reflection 5 | { 6 | public partial class ParameterReflector 7 | { 8 | internal static ParameterReflector Create(ParameterInfo parameterInfo) 9 | { 10 | if (parameterInfo == null) 11 | { 12 | throw new ArgumentNullException(nameof(parameterInfo)); 13 | } 14 | return ReflectorCacheUtils.GetOrAdd(parameterInfo, info => new ParameterReflector(info)); 15 | } 16 | } 17 | } -------------------------------------------------------------------------------- /src/AspectCore.Extensions.Reflection/Factories/TypeReflector.Factory.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Reflection; 4 | using System.Text; 5 | using AspectCore.Extensions.Reflection.Internals; 6 | 7 | namespace AspectCore.Extensions.Reflection 8 | { 9 | public partial class TypeReflector 10 | { 11 | internal static TypeReflector Create(TypeInfo typeInfo) 12 | { 13 | if (typeInfo == null) 14 | { 15 | throw new ArgumentNullException(nameof(typeInfo)); 16 | } 17 | return ReflectorCacheUtils.GetOrAdd(typeInfo, info => new TypeReflector(info)); 18 | } 19 | } 20 | } -------------------------------------------------------------------------------- /src/AspectCore.Extensions.Reflection/FieldReflector.OpenGeneric.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Reflection; 3 | 4 | namespace AspectCore.Extensions.Reflection 5 | { 6 | public partial class FieldReflector 7 | { 8 | private class OpenGenericFieldReflector : FieldReflector 9 | { 10 | public OpenGenericFieldReflector(FieldInfo reflectionInfo) : base(reflectionInfo) 11 | { 12 | } 13 | 14 | protected override Func CreateGetter() => null; 15 | 16 | protected override Action CreateSetter() => null; 17 | 18 | public override object GetValue(object instance) => throw new InvalidOperationException("Late bound operations cannot be performed on fields with types for which Type.ContainsGenericParameters is true"); 19 | 20 | public override void SetValue(object instance, object value) => throw new InvalidOperationException("Late bound operations cannot be performed on fields with types for which Type.ContainsGenericParameters is true"); 21 | 22 | public override object GetStaticValue() => throw new InvalidOperationException("Late bound operations cannot be performed on fields with types for which Type.ContainsGenericParameters is true"); 23 | 24 | public override void SetStaticValue(object value) => throw new InvalidOperationException("Late bound operations cannot be performed on fields with types for which Type.ContainsGenericParameters is true"); 25 | } 26 | } 27 | } -------------------------------------------------------------------------------- /src/AspectCore.Extensions.Reflection/ICustomAttributeReflectorProvider.cs: -------------------------------------------------------------------------------- 1 | namespace AspectCore.Extensions.Reflection 2 | { 3 | public interface ICustomAttributeReflectorProvider 4 | { 5 | CustomAttributeReflector[] CustomAttributeReflectors { get; } 6 | } 7 | } -------------------------------------------------------------------------------- /src/AspectCore.Extensions.Reflection/IParameterReflectorProvider.cs: -------------------------------------------------------------------------------- 1 | namespace AspectCore.Extensions.Reflection 2 | { 3 | public interface IParameterReflectorProvider 4 | { 5 | ParameterReflector[] ParameterReflectors { get; } 6 | } 7 | } -------------------------------------------------------------------------------- /src/AspectCore.Extensions.Reflection/Internals/ConstantsUtils.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Reflection; 5 | using System.Threading.Tasks; 6 | 7 | namespace AspectCore.Extensions.Reflection.Internals 8 | { 9 | internal static class MethodInfoConstant 10 | { 11 | internal static readonly MethodInfo GetTypeFromHandle = InternalExtensions.GetMethod>(handle => Type.GetTypeFromHandle(handle)); 12 | 13 | internal static readonly MethodInfo GetMethodFromHandle = InternalExtensions.GetMethod>((h1, h2) => MethodBase.GetMethodFromHandle(h1, h2)); 14 | 15 | internal static readonly ConstructorInfo ArgumentNullExceptionCtor = typeof(ArgumentNullException).GetTypeInfo().GetConstructor(new Type[] { typeof(string) }); 16 | 17 | internal static readonly ConstructorInfo ObjectCtor = typeof(object).GetTypeInfo().DeclaredConstructors.Single(); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/AspectCore.Extensions.Reflection/MemberReflector.CustomAttribute.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Reflection; 4 | using System.Text; 5 | 6 | namespace AspectCore.Extensions.Reflection 7 | { 8 | public abstract partial class MemberReflector : ICustomAttributeReflectorProvider where TMemberInfo : MemberInfo 9 | { 10 | private readonly CustomAttributeReflector[] _customAttributeReflectors; 11 | 12 | public CustomAttributeReflector[] CustomAttributeReflectors => _customAttributeReflectors; 13 | } 14 | } -------------------------------------------------------------------------------- /src/AspectCore.Extensions.Reflection/MemberReflector.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Reflection; 3 | using System.Linq; 4 | 5 | namespace AspectCore.Extensions.Reflection 6 | { 7 | public abstract partial class MemberReflector where TMemberInfo : MemberInfo 8 | { 9 | protected TMemberInfo _reflectionInfo; 10 | 11 | public virtual string Name => _reflectionInfo.Name; 12 | 13 | protected MemberReflector(TMemberInfo reflectionInfo) 14 | { 15 | _reflectionInfo = reflectionInfo ?? throw new ArgumentNullException(nameof(reflectionInfo)); 16 | _customAttributeReflectors = _reflectionInfo.CustomAttributes.Select(data => CustomAttributeReflector.Create(data)).ToArray(); 17 | } 18 | 19 | public override string ToString() => $"{_reflectionInfo.MemberType} : {_reflectionInfo} DeclaringType : {_reflectionInfo.DeclaringType}"; 20 | 21 | public TMemberInfo GetMemberInfo() => _reflectionInfo; 22 | 23 | public virtual string DisplayName => _reflectionInfo.Name; 24 | } 25 | } -------------------------------------------------------------------------------- /src/AspectCore.Extensions.Reflection/MethodReflector.DisplayName.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | 3 | namespace AspectCore.Extensions.Reflection 4 | { 5 | public partial class MethodReflector 6 | { 7 | private readonly string _displayName; 8 | 9 | public override string DisplayName => _displayName; 10 | 11 | private static string GetDisplayName(MethodInfo method) 12 | { 13 | var name = $"{method.ReturnType.GetReflector().DisplayName} {method.Name}"; 14 | if (method.IsGenericMethod) 15 | { 16 | name += "<"; 17 | var arguments = method.GetGenericArguments(); 18 | name += arguments[0].GetReflector().DisplayName; 19 | for (var i = 1; i < arguments.Length; i++) 20 | { 21 | name += ("," + arguments[i].GetReflector().DisplayName); 22 | } 23 | name += ">"; 24 | } 25 | var parameterTypes = method.GetParameterTypes(); 26 | name += "("; 27 | if (parameterTypes.Length == 0) 28 | { 29 | name += ")"; 30 | return name; 31 | } 32 | name += parameterTypes[0].GetReflector().DisplayName; 33 | for (var i = 1; i < parameterTypes.Length; i++) 34 | { 35 | name += ("," + parameterTypes[i].GetReflector().DisplayName); 36 | } 37 | name += ")"; 38 | return name; 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/AspectCore.Extensions.Reflection/MethodReflector.OpenGeneric.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Reflection; 3 | 4 | namespace AspectCore.Extensions.Reflection 5 | { 6 | public partial class MethodReflector 7 | { 8 | private class OpenGenericMethodReflector : MethodReflector 9 | { 10 | public OpenGenericMethodReflector(MethodInfo reflectionInfo) 11 | : base(reflectionInfo) 12 | { 13 | } 14 | 15 | protected override Func CreateInvoker() 16 | { 17 | return null; 18 | } 19 | 20 | public override object Invoke(object instance, params object[] parameters) 21 | { 22 | throw new InvalidOperationException("Late bound operations cannot be performed on types or methods for which ContainsGenericParameters is true."); 23 | } 24 | 25 | public override object StaticInvoke(params object[] parameters) 26 | { 27 | throw new InvalidOperationException("Late bound operations cannot be performed on types or methods for which ContainsGenericParameters is true."); 28 | } 29 | } 30 | } 31 | } -------------------------------------------------------------------------------- /src/AspectCore.Extensions.Reflection/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyConfiguration("")] 9 | [assembly: AssemblyCompany("")] 10 | [assembly: AssemblyProduct("AspectCore Framework")] 11 | [assembly: AssemblyTrademark("")] 12 | 13 | // Setting ComVisible to false makes the types in this assembly not visible 14 | // to COM components. If you need to access a type in this assembly from 15 | // COM, set the ComVisible attribute to true on that type. 16 | [assembly: ComVisible(false)] 17 | 18 | // The following GUID is for the ID of the typelib if this project is exposed to COM 19 | [assembly: Guid("cab96f50-44e1-4d8e-9149-ddcba1fbe493")] 20 | -------------------------------------------------------------------------------- /src/AspectCore.Extensions.Reflection/PropertyReflector.OpenGeneric.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Reflection; 3 | 4 | namespace AspectCore.Extensions.Reflection 5 | { 6 | public partial class PropertyReflector 7 | { 8 | private class OpenGenericPropertyReflector : PropertyReflector 9 | { 10 | public OpenGenericPropertyReflector(PropertyInfo reflectionInfo) : base(reflectionInfo) 11 | { 12 | } 13 | 14 | public override object GetValue(object instance) => throw new InvalidOperationException("Late bound operations cannot be performed on property with types for which Type.ContainsGenericParameters is true"); 15 | 16 | public override void SetValue(object instance, object value) => throw new InvalidOperationException("Late bound operations cannot be performed on property with types for which Type.ContainsGenericParameters is true"); 17 | 18 | public override object GetStaticValue() => throw new InvalidOperationException("Late bound operations cannot be performed on property with types for which Type.ContainsGenericParameters is true"); 19 | 20 | public override void SetStaticValue(object value) => throw new InvalidOperationException("Late bound operations cannot be performed on property with types for which Type.ContainsGenericParameters is true"); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/AspectCore.Extensions.Windsor/AspectCore.Extensions.Windsor.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | Interceptor and dynamicProxy support for Castle.Windsor via AspectCore Framework. 7 | AspectCore.Extensions.Windsor 8 | AspectCore.Extensions.Windsor 9 | AspectCore.Extensions.Windsor 10 | DynamicProxy;Aop;Windsor;AspectCore 11 | Interceptor and dynamicProxy support for Castle.Windsor via AspectCore Framework. 12 | net9.0;net8.0;net7.0;net6.0;netstandard2.1;netstandard2.0 13 | true 14 | true 15 | snupkg 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /src/AspectCore.Extensions.Windsor/CompatibleCollectionResolver.cs: -------------------------------------------------------------------------------- 1 | using Castle.Core; 2 | using Castle.MicroKernel; 3 | using Castle.MicroKernel.Context; 4 | using Castle.MicroKernel.Resolvers.SpecializedResolvers; 5 | 6 | namespace AspectCore.Extensions.Windsor 7 | { 8 | public class CompatibleCollectionResolver : CollectionResolver 9 | { 10 | public CompatibleCollectionResolver(IKernel kernel) 11 | : base(kernel, allowEmptyCollections: true) 12 | { 13 | } 14 | 15 | public override bool CanResolve(CreationContext context, ISubDependencyResolver contextHandlerResolver, ComponentModel model, 16 | DependencyModel dependency) 17 | { 18 | if (kernel.HasComponent(dependency.TargetItemType)) 19 | { 20 | return false; 21 | } 22 | 23 | return base.CanResolve(context, contextHandlerResolver, model, dependency); 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/AspectCore.Extensions.Windsor/FacilityExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using AspectCore.Configuration; 3 | using Castle.MicroKernel; 4 | using Castle.Windsor; 5 | using System.Linq; 6 | 7 | namespace AspectCore.Extensions.Windsor 8 | { 9 | public static class FacilityExtensions 10 | { 11 | public static IKernel AddAspectCoreFacility(this IKernel Kernel, Action configure = null) 12 | { 13 | if (Kernel == null) 14 | { 15 | throw new ArgumentNullException(nameof(Kernel)); 16 | } 17 | var config = new AspectConfiguration(); 18 | configure?.Invoke(config); 19 | if (Kernel.GetFacilities().All(x => !(x.GetType() == typeof(AspectCoreFacility)))) 20 | Kernel.AddFacility(new AspectCoreFacility(config)); 21 | return Kernel; 22 | } 23 | 24 | public static IWindsorContainer AddAspectCoreFacility(this IWindsorContainer windsorContainer, Action configure = null) 25 | { 26 | if (windsorContainer == null) 27 | { 28 | throw new ArgumentNullException(nameof(windsorContainer)); 29 | } 30 | AddAspectCoreFacility(windsorContainer.Kernel, configure); 31 | return windsorContainer; 32 | } 33 | } 34 | } -------------------------------------------------------------------------------- /src/AspectCore.Extensions.Windsor/WindsorAspectBuilderFactory.cs: -------------------------------------------------------------------------------- 1 | using System.Linq; 2 | using AspectCore.DynamicProxy; 3 | 4 | namespace AspectCore.Extensions.Windsor 5 | { 6 | [NonAspect] 7 | internal class WindsorAspectBuilderFactory : IAspectBuilderFactory 8 | { 9 | private readonly IAspectBuilderFactory _aspectBuilderFactory; 10 | private readonly AspectDelegate _complete; 11 | 12 | public WindsorAspectBuilderFactory(IAspectBuilderFactory aspectBuilderFactory, AspectDelegate complete) 13 | { 14 | _aspectBuilderFactory = aspectBuilderFactory; 15 | _complete = complete; 16 | } 17 | 18 | public IAspectBuilder Create(AspectContext context) 19 | { 20 | var builder = _aspectBuilderFactory.Create(context); 21 | return new AspectBuilder(_complete, builder.Delegates.ToList()); 22 | } 23 | } 24 | } -------------------------------------------------------------------------------- /tests/AspectCore.Extensions.Autofac.Test/AdditionalInterceptorSelectorTests.cs: -------------------------------------------------------------------------------- 1 | using System.Threading.Tasks; 2 | using AspectCore.DynamicProxy; 3 | using AspectCore.Extensions.Autofac; 4 | using Autofac; 5 | using Xunit; 6 | 7 | namespace AspectCoreTest.Autofac 8 | { 9 | public class AdditionalInterceptorSelectorTests 10 | { 11 | [Fact] 12 | public void ImplementationMethod_Test() 13 | { 14 | var builder = new ContainerBuilder(); 15 | builder.RegisterDynamicProxy(); 16 | builder.RegisterType().As(); 17 | var provider = builder.Build(); 18 | var service = provider.Resolve(); 19 | var val = service.GetValue("le"); 20 | Assert.Equal("lemon", val); 21 | } 22 | 23 | public class Intercept : AbstractInterceptorAttribute 24 | { 25 | public override Task Invoke(AspectContext context, AspectDelegate next) 26 | { 27 | context.Parameters[0] = "lemon"; 28 | return context.Invoke(next); 29 | } 30 | } 31 | 32 | public interface IService 33 | { 34 | string GetValue(string val); 35 | } 36 | 37 | public class Service : IService 38 | { 39 | [Intercept] 40 | public string GetValue(string val) 41 | { 42 | return val; 43 | } 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /tests/AspectCore.Extensions.Autofac.Test/Fakes/CacheInterceptorAttribute.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Linq; 3 | using System.Threading.Tasks; 4 | using AspectCore.DynamicProxy; 5 | using AspectCore.DynamicProxy.Parameters; 6 | 7 | namespace AspectCore.Extensions.Test.Fakes 8 | { 9 | public class CacheInterceptorAttribute : AbstractInterceptorAttribute 10 | { 11 | private readonly IDictionary cache = new Dictionary(); 12 | 13 | public async override Task Invoke(AspectContext context, AspectDelegate next) 14 | { 15 | var parameters = context.GetParameters(); 16 | if (parameters.Any()) 17 | { 18 | var id = default(int); 19 | if (int.TryParse(parameters[0].Value.ToString(), out id)) 20 | { 21 | var result = default(Model); 22 | if (cache.TryGetValue(id, out result)) 23 | { 24 | context.ReturnValue = result; 25 | return; 26 | } 27 | } 28 | } 29 | await next(context); 30 | var value = context.ReturnValue as Model; 31 | if (value != null) 32 | { 33 | cache[value.Id] = value; 34 | } 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /tests/AspectCore.Extensions.Autofac.Test/Fakes/Controller.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | 6 | namespace AspectCore.Extensions.Test.Fakes 7 | { 8 | public class Controller : IController 9 | { 10 | public IService Service { get; } 11 | 12 | public Controller(IService service) 13 | { 14 | Service = service; 15 | } 16 | 17 | public Model Execute() 18 | { 19 | return Service.Get(100); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /tests/AspectCore.Extensions.Autofac.Test/Fakes/FakeServiceWithOut.cs: -------------------------------------------------------------------------------- 1 | using System.Globalization; 2 | using System.Threading.Tasks; 3 | using AspectCore.DynamicProxy; 4 | 5 | namespace AspectCore.Extensions.Test.Fakes 6 | { 7 | public class FakeServiceWithOutInterceptor : AbstractInterceptorAttribute 8 | { 9 | public override async Task Invoke(AspectContext context, AspectDelegate next) 10 | { 11 | await next(context); 12 | } 13 | } 14 | 15 | [FakeServiceWithOutInterceptor] 16 | public interface IFakeServiceWithOut 17 | { 18 | bool OutDecimal(out decimal num); 19 | 20 | bool OutInt(out int num); 21 | } 22 | 23 | public class FakeServiceWithOut : IFakeServiceWithOut 24 | { 25 | public bool OutDecimal(out decimal num) 26 | { 27 | num = 1.0M; 28 | return true; 29 | } 30 | 31 | public bool OutInt(out int num) 32 | { 33 | num = 1; 34 | return true; 35 | } 36 | } 37 | } -------------------------------------------------------------------------------- /tests/AspectCore.Extensions.Autofac.Test/Fakes/IController.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | 6 | namespace AspectCore.Extensions.Test.Fakes 7 | { 8 | [CacheInterceptor] 9 | public interface IController 10 | { 11 | IService Service { get; } 12 | Model Execute(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /tests/AspectCore.Extensions.Autofac.Test/Fakes/IService.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | 6 | namespace AspectCore.Extensions.Test.Fakes 7 | { 8 | public interface IService 9 | { 10 | [CacheInterceptor] 11 | Model Get(int id); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /tests/AspectCore.Extensions.Autofac.Test/Fakes/Model.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | 6 | namespace AspectCore.Extensions.Test.Fakes 7 | { 8 | public class Model 9 | { 10 | public int Id { get; set; } 11 | 12 | public Guid Version { get; set; } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /tests/AspectCore.Extensions.Autofac.Test/Fakes/Service.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace AspectCore.Extensions.Test.Fakes 4 | { 5 | public class Service : IService 6 | { 7 | public virtual Model Get(int id) 8 | { 9 | return new Model { Id = id, Version = Guid.NewGuid() }; 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /tests/AspectCore.Extensions.Autofac.Test/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyConfiguration("")] 9 | [assembly: AssemblyCompany("")] 10 | [assembly: AssemblyProduct("AspectCore.Container.Autofac.Test")] 11 | [assembly: AssemblyTrademark("")] 12 | 13 | // Setting ComVisible to false makes the types in this assembly not visible 14 | // to COM components. If you need to access a type in this assembly from 15 | // COM, set the ComVisible attribute to true on that type. 16 | [assembly: ComVisible(false)] 17 | 18 | // The following GUID is for the ID of the typelib if this project is exposed to COM 19 | [assembly: Guid("42599bc2-6584-4ebe-ac03-743d4958a7c3")] 20 | -------------------------------------------------------------------------------- /tests/AspectCore.Extensions.Autofac.Test/SpecificationTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Autofac; 3 | using Autofac.Extensions.DependencyInjection; 4 | using Microsoft.Extensions.DependencyInjection; 5 | using Microsoft.Extensions.DependencyInjection.Specification; 6 | 7 | namespace AspectCoreTest.Autofac 8 | { 9 | public class SpecificationTests : DependencyInjectionSpecificationTests 10 | { 11 | protected override IServiceProvider CreateServiceProvider(IServiceCollection serviceCollection) 12 | { 13 | var builder = new ContainerBuilder(); 14 | builder.Populate(serviceCollection); 15 | return new AutofacServiceProvider(builder.Build()); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /tests/AspectCore.Extensions.Configuration.Tests/ConfigurationValueTest.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using Microsoft.Extensions.Configuration; 3 | using Xunit; 4 | using AspectCore.DependencyInjection; 5 | 6 | namespace AspectCore.Extensions.Configuration.Tests 7 | { 8 | public class ConfigurationValueTest 9 | { 10 | [Fact] 11 | public void LoadValue() 12 | { 13 | var dict = new Dictionary 14 | { 15 | {"creator:age", "24"}, 16 | {"creator:name", "lemon"} 17 | }; 18 | var builder = new ConfigurationBuilder().AddEnvironmentVariables(); 19 | builder.AddInMemoryCollection(dict); 20 | var configuration = builder.Build(); 21 | var container = new ServiceContext(); 22 | container.AddInstance(configuration); 23 | container.AddConfigurationInject(); 24 | container.AddType(); 25 | var service = container.Build().Resolve(); 26 | Assert.Equal(service.ToString(), "lemon-24"); 27 | } 28 | } 29 | 30 | public class ValueConfigService 31 | { 32 | [ConfigurationValue("age", "creator")] 33 | private int age; 34 | 35 | [ConfigurationValue("name", "creator")] 36 | private string name; 37 | 38 | public override string ToString() 39 | { 40 | return $"{name}-{age}"; 41 | } 42 | } 43 | } -------------------------------------------------------------------------------- /tests/AspectCore.Extensions.DependencyInjection.Test/Issues/ParamWithInTests.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.Extensions.DependencyInjection; 2 | using System; 3 | using Xunit; 4 | 5 | namespace AspectCore.Extensions.DependencyInjection.Test.Issues 6 | { 7 | // https://github.com/dotnetcore/AspectCore-Framework/issues/287 8 | public class ParamWithInTests 9 | { 10 | public interface IParamWithIn 11 | { 12 | object TestFunc(string val, in ReadOnlyMemory bytes); 13 | } 14 | 15 | public class ParamWithIn : IParamWithIn 16 | { 17 | public object TestFunc(string val, in ReadOnlyMemory bytes) 18 | { 19 | return bytes; 20 | } 21 | } 22 | 23 | [Fact] 24 | public void ParamWithIn_Test() 25 | { 26 | var sp = new ServiceCollection() 27 | .AddScoped() 28 | .BuildDynamicProxyProvider(); 29 | 30 | var service = sp.GetRequiredService(); 31 | ReadOnlyMemory bytes = new ReadOnlyMemory(new byte[] {0, 1, 2}); 32 | var obj = service.TestFunc("val1", bytes); 33 | Assert.NotNull(obj); 34 | Assert.Equal(bytes, obj); 35 | } 36 | } 37 | } -------------------------------------------------------------------------------- /tests/AspectCore.Extensions.DependencyInjection.Test/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyConfiguration("")] 9 | [assembly: AssemblyCompany("")] 10 | [assembly: AssemblyProduct("AspectCore.Container.DependencyInjection.Test")] 11 | [assembly: AssemblyTrademark("")] 12 | 13 | // Setting ComVisible to false makes the types in this assembly not visible 14 | // to COM components. If you need to access a type in this assembly from 15 | // COM, set the ComVisible attribute to true on that type. 16 | [assembly: ComVisible(false)] 17 | 18 | // The following GUID is for the ID of the typelib if this project is exposed to COM 19 | [assembly: Guid("a8b2b7ad-f5cd-4fb7-b0cf-a35a6c33aea7")] 20 | -------------------------------------------------------------------------------- /tests/AspectCore.Extensions.DependencyInjection.Test/SpecificationTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Microsoft.Extensions.DependencyInjection; 3 | using Microsoft.Extensions.DependencyInjection.Specification; 4 | 5 | namespace AspectCore.Extensions.DependencyInjection.Test 6 | { 7 | //public class SpecificationTests : DependencyInjectionSpecificationTests 8 | //{ 9 | // protected override IServiceProvider CreateServiceProvider(IServiceCollection serviceCollection) 10 | // { 11 | // serviceCollection.AddAspectCore(option => 12 | // { 13 | // option.InterceptorFactories.AddDelegate((ctx, next) => next(ctx)); 14 | // option.InterceptorFactories.AddDelegate(next => ctx => next(ctx)); 15 | // }); 16 | // return serviceCollection.BuildAspectCoreServiceProvider(); 17 | // } 18 | //} 19 | } 20 | -------------------------------------------------------------------------------- /tests/AspectCore.Extensions.DependencyInjection.Test/UseMicrosoftDISpecificationTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using AspectCore.Configuration; 3 | using Microsoft.Extensions.DependencyInjection; 4 | using Microsoft.Extensions.DependencyInjection.Specification; 5 | 6 | namespace AspectCore.Extensions.DependencyInjection.Test 7 | { 8 | public class UseMicrosoftDISpecificationTests : DependencyInjectionSpecificationTests 9 | { 10 | protected override IServiceProvider CreateServiceProvider(IServiceCollection serviceCollection) 11 | { 12 | serviceCollection.ConfigureDynamicProxy(config => 13 | { 14 | config.Interceptors.AddDelegate((ctx, next) => next(ctx)); 15 | }); 16 | 17 | return serviceCollection.BuildDynamicProxyProvider(); 18 | } 19 | } 20 | } -------------------------------------------------------------------------------- /tests/AspectCore.Extensions.DependencyInjection.Test/Use_Built_In_ContainerSpecificationTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using AspectCore.Configuration; 3 | using AspectCore.DependencyInjection; 4 | using Microsoft.Extensions.DependencyInjection; 5 | using Microsoft.Extensions.DependencyInjection.Specification; 6 | 7 | namespace AspectCore.Extensions.DependencyInjection.Test 8 | { 9 | // public class Use_Built_In_ContainerSpecificationTests : DependencyInjectionSpecificationTests 10 | // { 11 | // protected override IServiceProvider CreateServiceProvider(IServiceCollection serviceCollection) 12 | // { 13 | // IServiceContainer container = serviceCollection.ToServiceContainer(); 14 | // 15 | // container.Configuration.Interceptors.AddDelegate((ctx, next) => next(ctx)); 16 | // 17 | // return container.Build(); 18 | // } 19 | // } 20 | } -------------------------------------------------------------------------------- /tests/AspectCore.Extensions.Hosting.Tests/AspectCore.Extensions.Hosting.Tests.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | net9.0;net8.0;net7.0;net6.0 5 | false 6 | 7 | 8 | 9 | 10 | 11 | 12 | all 13 | runtime; build; native; contentfiles; analyzers; buildtransitive 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /tests/AspectCore.Extensions.Hosting.Tests/FakeClasses.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Extensions.Hosting.Tests 6 | { 7 | public interface IService 8 | { 9 | string GetValue(); 10 | } 11 | 12 | public class Service : IService 13 | { 14 | public string GetValue() 15 | { 16 | return "service"; 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /tests/AspectCore.Extensions.LightInject.Test/AdditionalInterceptorSelectorTests.cs: -------------------------------------------------------------------------------- 1 | using System.Threading.Tasks; 2 | using AspectCore.DynamicProxy; 3 | using AspectCore.Extensions.LightInject; 4 | using LightInject; 5 | using Xunit; 6 | 7 | namespace AspectCoreTest.LightInject 8 | { 9 | public class AdditionalInterceptorSelectorTests 10 | { 11 | [Fact] 12 | public void ImplementationMethod_Test() 13 | { 14 | var builder = new ServiceContainer(); 15 | builder.RegisterDynamicProxy(); 16 | builder.Register(); 17 | var provider = builder; 18 | var service = provider.GetInstance(); 19 | var val = service.GetValue("le"); 20 | Assert.Equal("lemon", val); 21 | } 22 | 23 | public class Intercept : AbstractInterceptorAttribute 24 | { 25 | public override Task Invoke(AspectContext context, AspectDelegate next) 26 | { 27 | context.Parameters[0] = "lemon"; 28 | return context.Invoke(next); 29 | } 30 | } 31 | 32 | public interface IService 33 | { 34 | string GetValue(string val); 35 | } 36 | 37 | public class Service : IService 38 | { 39 | [Intercept] 40 | public string GetValue(string val) 41 | { 42 | return val; 43 | } 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /tests/AspectCore.Extensions.LightInject.Test/AspectCore.Extensions.LightInject.Test.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | net9.0;net8.0;net7.0;net6.0 5 | portable 6 | true 7 | false 8 | false 9 | false 10 | AspectCoreTest.LightInject 11 | 12 | 13 | 14 | 15 | all 16 | runtime; build; native; contentfiles; analyzers; buildtransitive 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /tests/AspectCore.Extensions.LightInject.Test/AsyncIncreamentAttribute.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading.Tasks; 3 | using AspectCore.DynamicProxy; 4 | 5 | namespace AspectCoreTest.LightInject 6 | { 7 | [AttributeUsage(AttributeTargets.Method)] 8 | public class AsyncIncreamentAttribute : AbstractInterceptorAttribute 9 | { 10 | public override async Task Invoke(AspectContext context, AspectDelegate next) 11 | { 12 | await context.Invoke(next); 13 | await Task.Delay(100); // Simulate a TRUE asynchronous method here for testing thread context switching. 14 | 15 | if (context.ReturnValue is Task task) 16 | { 17 | var result = await task; 18 | context.ReturnValue = Task.FromResult(result + 1); 19 | } 20 | else if (context.ReturnValue is ValueTask valueTask) 21 | { 22 | var result = await valueTask; 23 | context.ReturnValue = new ValueTask(result + 1); 24 | } 25 | else if (context.ReturnValue is int result) 26 | { 27 | context.ReturnValue = result + 1; 28 | } 29 | } 30 | } 31 | } -------------------------------------------------------------------------------- /tests/AspectCore.Extensions.LightInject.Test/Fakes/CacheInterceptorAttribute.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Linq; 3 | using System.Threading.Tasks; 4 | using AspectCore.DynamicProxy; 5 | using AspectCore.DynamicProxy.Parameters; 6 | 7 | namespace AspectCoreTest.LightInject.Fakes 8 | { 9 | public class CacheInterceptorAttribute : AbstractInterceptorAttribute 10 | { 11 | private readonly IDictionary cache = new Dictionary(); 12 | 13 | public override async Task Invoke(AspectContext context, AspectDelegate next) 14 | { 15 | var parameters = context.GetParameters(); 16 | if (parameters.Any()) 17 | { 18 | var id = default(int); 19 | if (int.TryParse(parameters[0].Value.ToString(), out id)) 20 | { 21 | var result = default(Model); 22 | if (cache.TryGetValue(id, out result)) 23 | { 24 | context.ReturnValue = result; 25 | return; 26 | } 27 | } 28 | } 29 | await next(context); 30 | var value = context.ReturnValue as Model; 31 | if (value != null) 32 | { 33 | cache[value.Id] = value; 34 | } 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /tests/AspectCore.Extensions.LightInject.Test/Fakes/Controller.cs: -------------------------------------------------------------------------------- 1 | namespace AspectCoreTest.LightInject.Fakes 2 | { 3 | public class Controller : IController 4 | { 5 | public IService Service { get; } 6 | 7 | public Controller(IService service) 8 | { 9 | Service = service; 10 | } 11 | 12 | public Model Execute() 13 | { 14 | return Service.Get(100); 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /tests/AspectCore.Extensions.LightInject.Test/Fakes/IController.cs: -------------------------------------------------------------------------------- 1 | namespace AspectCoreTest.LightInject.Fakes 2 | { 3 | [CacheInterceptor] 4 | public interface IController 5 | { 6 | IService Service { get; } 7 | Model Execute(); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /tests/AspectCore.Extensions.LightInject.Test/Fakes/IService.cs: -------------------------------------------------------------------------------- 1 | namespace AspectCoreTest.LightInject.Fakes 2 | { 3 | public interface IService 4 | { 5 | [CacheInterceptor] 6 | Model Get(int id); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /tests/AspectCore.Extensions.LightInject.Test/Fakes/Model.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace AspectCoreTest.LightInject.Fakes 4 | { 5 | public class Model 6 | { 7 | public int Id { get; set; } 8 | 9 | public Guid Version { get; set; } 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /tests/AspectCore.Extensions.LightInject.Test/Fakes/Service.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace AspectCoreTest.LightInject.Fakes 4 | { 5 | public class Service : IService 6 | { 7 | public virtual Model Get(int id) 8 | { 9 | return new Model { Id = id, Version = Guid.NewGuid() }; 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /tests/AspectCore.Extensions.Reflection.Test/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyConfiguration("")] 9 | [assembly: AssemblyCompany("")] 10 | [assembly: AssemblyProduct("AspectCore.Extensions.Reflection.Test")] 11 | [assembly: AssemblyTrademark("")] 12 | 13 | // Setting ComVisible to false makes the types in this assembly not visible 14 | // to COM components. If you need to access a type in this assembly from 15 | // COM, set the ComVisible attribute to true on that type. 16 | [assembly: ComVisible(false)] 17 | 18 | // The following GUID is for the ID of the typelib if this project is exposed to COM 19 | [assembly: Guid("e0f82471-75e5-4d48-bc00-d6b829a45846")] 20 | -------------------------------------------------------------------------------- /tests/AspectCore.Extensions.Windsor.Test/Fakes/CacheService.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading.Tasks; 3 | 4 | namespace AspectCoreTest.Windsor.Fakes 5 | { 6 | public class CacheService : ICacheService 7 | { 8 | public Model Get(int id) 9 | { 10 | return new Model { Id = id, Version = Guid.NewGuid() }; 11 | } 12 | 13 | public Task GetAsync(int id) 14 | { 15 | return Task.FromResult(Get(id)); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /tests/AspectCore.Extensions.Windsor.Test/Fakes/Controller.cs: -------------------------------------------------------------------------------- 1 | namespace AspectCoreTest.Windsor.Fakes 2 | { 3 | public class Controller : IController 4 | { 5 | public ICacheService Service { get; } 6 | 7 | public Controller(ICacheService service) 8 | { 9 | Service = service; 10 | } 11 | 12 | public Model Execute() 13 | { 14 | return Service.Get(100); 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /tests/AspectCore.Extensions.Windsor.Test/Fakes/ICacheService.cs: -------------------------------------------------------------------------------- 1 | using System.Threading.Tasks; 2 | 3 | namespace AspectCoreTest.Windsor.Fakes 4 | { 5 | public interface ICacheService 6 | { 7 | [CacheInterceptor] 8 | Model Get(int id); 9 | 10 | [CacheInterceptor] 11 | Task GetAsync(int id); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /tests/AspectCore.Extensions.Windsor.Test/Fakes/IController.cs: -------------------------------------------------------------------------------- 1 | namespace AspectCoreTest.Windsor.Fakes 2 | { 3 | [CacheInterceptor] 4 | public interface IController 5 | { 6 | ICacheService Service { get; } 7 | Model Execute(); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /tests/AspectCore.Extensions.Windsor.Test/Fakes/Model.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace AspectCoreTest.Windsor.Fakes 4 | { 5 | public class Model 6 | { 7 | public int Id { get; set; } 8 | 9 | public Guid Version { get; set; } 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /tests/AspectCore.Tests/AspectCore.Tests.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | net9.0;net8.0;net7.0;net6.0 4 | false 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | all 28 | runtime; build; native; contentfiles; analyzers; buildtransitive 29 | 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /tests/AspectCore.Tests/DynamicProxy/AsyncAspectTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using AspectCore.DynamicProxy; 4 | using System.Text; 5 | using Xunit; 6 | using System.Threading.Tasks; 7 | using AspectCore.DependencyInjection; 8 | 9 | namespace AspectCore.Tests.DynamicProxy 10 | { 11 | public class AsyncAspectTests : DynamicProxyTestBase 12 | { 13 | [Fact] 14 | public void DynAsync_Test() 15 | { 16 | var proxy = ProxyGenerator.CreateClassProxy(); 17 | var result = proxy.DynAsync(100); 18 | Assert.IsAssignableFrom>(result); 19 | } 20 | 21 | [Fact] 22 | public void Async_Test() 23 | { 24 | var proxy = ProxyGenerator.CreateClassProxy(); 25 | var result = proxy.Async(100); 26 | Assert.IsAssignableFrom>(result); 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /tests/AspectCore.Tests/DynamicProxy/DynamicProxyTestBase.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using AspectCore.Configuration; 5 | using AspectCore.DynamicProxy; 6 | 7 | namespace AspectCore.Tests.DynamicProxy 8 | { 9 | public class DynamicProxyTestBase 10 | { 11 | protected IProxyGenerator ProxyGenerator { get; } 12 | 13 | public DynamicProxyTestBase() 14 | { 15 | var builder = new ProxyGeneratorBuilder(); 16 | builder.Configure(Configure); 17 | ProxyGenerator = builder.Build(); 18 | } 19 | 20 | protected virtual void Configure(IAspectConfiguration configuration) { } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /tests/AspectCore.Tests/DynamicProxy/ExplicitImplementationTest.cs: -------------------------------------------------------------------------------- 1 | using AspectCore.Configuration; 2 | using AspectCore.DynamicProxy; 3 | using Xunit; 4 | 5 | namespace AspectCore.Tests.DynamicProxy 6 | { 7 | public class ExplicitImplementationTest : DynamicProxyTestBase 8 | { 9 | [Fact] 10 | public void ExplicitImplementation_NonAspect_Test() 11 | { 12 | var service = 13 | ProxyGenerator.CreateClassProxy(); 14 | var result = ((IFakeExplicitImplementation)service).GetVal_NonAspect(); 15 | Assert.Equal("lemon", result); 16 | var result2 = ((IFakeExplicitImplementation)service).GetVal2(); 17 | Assert.Equal(1, result2); 18 | } 19 | 20 | [Fact] 21 | public void ExplicitImplementation_Aspect_Test() 22 | { 23 | var service = 24 | ProxyGenerator.CreateClassProxy(); 25 | var result = ((IFakeExplicitImplementation)service).GetVal(); 26 | Assert.Equal("lemon", result); 27 | } 28 | 29 | protected override void Configure(IAspectConfiguration configuration) 30 | { 31 | configuration.Interceptors.AddDelegate((ctx, next) => ctx.Invoke(next), Predicates.ForMethod("GetVal")); 32 | base.Configure(configuration); 33 | } 34 | } 35 | } -------------------------------------------------------------------------------- /tests/AspectCore.Tests/DynamicProxy/ExplicitMethodTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Reflection; 5 | using System.Text; 6 | using AspectCore.DynamicProxy; 7 | using Xunit; 8 | using AspectCore.Extensions.Reflection; 9 | 10 | namespace AspectCore.Tests.DynamicProxy 11 | { 12 | public class ExplicitMethodTests 13 | { 14 | [Fact] 15 | public void Find_ExplicitMethod() 16 | { 17 | var method = typeof(IFakeExplicitMethod).GetMethods().First(); 18 | method = method.MakeGenericMethod(typeof(string)); 19 | var explicitMethod = typeof(FakeExplicitMethod).GetTypeInfo().GetMethodBySignature(method); 20 | Assert.NotNull(explicitMethod); 21 | } 22 | 23 | public interface IFakeExplicitMethod 24 | { 25 | void Method(); 26 | } 27 | 28 | public class FakeExplicitMethod : IFakeExplicitMethod 29 | { 30 | void IFakeExplicitMethod.Method() 31 | { 32 | throw new NotImplementedException(); 33 | } 34 | } 35 | } 36 | } -------------------------------------------------------------------------------- /tests/AspectCore.Tests/DynamicProxy/OpenGenericMethodTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using AspectCore.Configuration; 5 | using AspectCore.DynamicProxy; 6 | using Microsoft.Extensions.ObjectPool; 7 | using Xunit; 8 | 9 | namespace AspectCore.Tests.DynamicProxy 10 | { 11 | public class OpenGenericMethodTests : DynamicProxyTestBase 12 | { 13 | [Fact] 14 | public void OpenGenericProxy() 15 | { 16 | var proxy = ProxyGenerator.CreateClassProxy(); 17 | 18 | } 19 | 20 | 21 | protected override void Configure(IAspectConfiguration configuration) 22 | { 23 | configuration.Interceptors.AddDelegate((ctx, next) => next(ctx)); 24 | } 25 | } 26 | } -------------------------------------------------------------------------------- /tests/AspectCore.Tests/DynamicProxy/ParameterInjectionTest.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading.Tasks; 3 | using AspectCore.Configuration; 4 | using AspectCore.DynamicProxy; 5 | using AspectCore.DynamicProxy.Parameters; 6 | using Xunit; 7 | 8 | namespace AspectCore.Tests.DynamicProxy 9 | { 10 | public class ParameterInjectionTest : DynamicProxyTestBase 11 | { 12 | [Fact] 13 | public void Parameter_Intercept() 14 | { 15 | var service = ProxyGenerator.CreateClassProxy(); 16 | Assert.Throws(() => 17 | { 18 | service.Run(null); 19 | }); 20 | } 21 | 22 | protected override void Configure(IAspectConfiguration configuration) 23 | { 24 | configuration.Interceptors.AddTyped(); 25 | } 26 | } 27 | 28 | public class AppService 29 | { 30 | public virtual void Run([NotNull]string name) 31 | { 32 | } 33 | } 34 | } -------------------------------------------------------------------------------- /tests/AspectCore.Tests/DynamicProxy/PredicatesTests.cs: -------------------------------------------------------------------------------- 1 | using AspectCore.Configuration; 2 | using Xunit; 3 | 4 | namespace AspectCore.Tests.DynamicProxy 5 | { 6 | public class PredicatesTests 7 | { 8 | [Fact] 9 | public void ImplementBaseClass() 10 | { 11 | var predicate = Predicates.Implement(typeof(ServiceBase)); 12 | var method = typeof(Transient).GetMethod(nameof(Transient.Foo)); 13 | Assert.True(predicate(method)); 14 | } 15 | 16 | [Fact] 17 | public void ImplementInterface() 18 | { 19 | var predicate = Predicates.Implement(typeof(Tests.IService)); 20 | var method = typeof(Transient).GetMethod("get_Id"); 21 | Assert.True(predicate(method)); 22 | } 23 | } 24 | } -------------------------------------------------------------------------------- /tests/AspectCore.Tests/Injector/ConstructorInjectionTest.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using AspectCore.DependencyInjection; 5 | using Xunit; 6 | 7 | namespace AspectCore.Tests.Injector 8 | { 9 | public class ConstructorInjectionTest : InjectorTestBase 10 | { 11 | [Fact] 12 | public void Constructor_Inject() 13 | { 14 | var userService = ServiceResolver.Resolve(); 15 | Assert.NotNull(userService); 16 | Assert.NotNull(userService.Repository); 17 | Assert.NotNull(userService.Logger); 18 | } 19 | 20 | [Fact] 21 | public void Equal() 22 | { 23 | var userService1 = ServiceResolver.Resolve(); 24 | var userService2 = ServiceResolver.Resolve(); 25 | Assert.NotEqual(userService1, userService2); 26 | Assert.Equal(userService1.Repository, userService2.Repository); 27 | } 28 | 29 | protected override void ConfigureService(IServiceContext serviceContext) 30 | { 31 | serviceContext.AddType(Lifetime.Singleton); 32 | serviceContext.AddType(typeof(IRepository<>), typeof(Repository<>), Lifetime.Scoped); 33 | serviceContext.AddType(); 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /tests/AspectCore.Tests/Injector/InjectorTestBase.cs: -------------------------------------------------------------------------------- 1 | using AspectCore.DependencyInjection; 2 | using AspectCore.DynamicProxy; 3 | 4 | namespace AspectCore.Tests.Injector 5 | { 6 | public class InjectorTestBase 7 | { 8 | public IServiceResolver ServiceResolver { get; } 9 | 10 | public IServiceContext ServiceContext { get; } 11 | 12 | public InjectorTestBase() 13 | { 14 | ServiceContext = new ServiceContext(); 15 | ConfigureService(ServiceContext); 16 | ServiceResolver = ServiceContext.Build(); 17 | } 18 | 19 | protected virtual void ConfigureService(IServiceContext serviceContext) { } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /tests/AspectCore.Tests/Injector/OpenClosedGenericsTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using AspectCore.DependencyInjection; 6 | using Xunit; 7 | 8 | namespace AspectCore.Tests.Injector 9 | { 10 | public class OpenClosedGenericsTests:InjectorTestBase 11 | { 12 | [Fact] 13 | public void ResolvesMixedOpenClosedGenericsAsEnumerable() 14 | { 15 | 16 | var enumerable = ServiceResolver.Resolve>>().ToArray(); 17 | 18 | Assert.Equal(3, enumerable.Length); 19 | } 20 | 21 | protected override void ConfigureService(IServiceContext serviceContext) 22 | { 23 | var instance = new FakeOpenGenericService(null); 24 | 25 | serviceContext.AddType(Lifetime.Singleton); 26 | serviceContext.AddType(typeof(IFakeOpenGenericService), typeof(FakeService), Lifetime.Singleton); 27 | serviceContext.AddType(typeof(IFakeOpenGenericService<>), typeof(FakeOpenGenericService<>), Lifetime.Singleton); 28 | serviceContext.AddInstance>(instance); 29 | } 30 | } 31 | 32 | 33 | } 34 | -------------------------------------------------------------------------------- /tests/AspectCore.Tests/Injector/PropertyInjectionTest.cs: -------------------------------------------------------------------------------- 1 | using AspectCore.DependencyInjection; 2 | using Xunit; 3 | 4 | namespace AspectCore.Tests.Injector 5 | { 6 | public class PropertyInjectionTest : InjectorTestBase 7 | { 8 | [Fact] 9 | public void Public_Property_Inject() 10 | { 11 | var service = ServiceResolver.Resolve(); 12 | Assert.NotNull(service); 13 | } 14 | 15 | [Fact] 16 | public void NonPublic_Property_Inject() 17 | { 18 | var service = (PropertyInjectionService)ServiceResolver.Resolve(); 19 | Assert.NotNull(service); 20 | Assert.NotNull(service.InternalLogger); 21 | Assert.Equal(service.InternalLogger, ServiceResolver.Resolve()); 22 | } 23 | 24 | protected override void ConfigureService(IServiceContext serviceContext) 25 | { 26 | serviceContext.Transients.AddType(); 27 | serviceContext.Singletons.AddType(); 28 | serviceContext.Transients.AddDelegate(typeof(IPropertyInjectionService), resolver => new PropertyInjectionService()); 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /tests/AspectCore.Tests/Injector/TransientServiceAccessorTest.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | using AspectCore.DependencyInjection; 5 | using Xunit; 6 | 7 | namespace AspectCore.Tests.Injector 8 | { 9 | public class TransientServiceAccessorTest : InjectorTestBase 10 | { 11 | [Fact] 12 | public void Get_Value() 13 | { 14 | var accessor1 = ServiceResolver.Resolve>(); 15 | Assert.NotEqual(accessor1.Value, accessor1.Value); 16 | Assert.NotEqual(accessor1.Value, ServiceResolver.Resolve()); 17 | using (var scope = ServiceResolver.CreateScope()) 18 | { 19 | var accessor2= scope.Resolve>(); 20 | Assert.Equal(accessor1, accessor2); 21 | Assert.NotEqual(accessor1.Value, accessor2.Value); 22 | } 23 | } 24 | protected override void ConfigureService(IServiceContext serviceContext) 25 | { 26 | serviceContext.AddType(); 27 | } 28 | } 29 | } -------------------------------------------------------------------------------- /tests/AspectCore.Tests/Integrate/IntegrateTestBase.cs: -------------------------------------------------------------------------------- 1 | using AspectCore.Configuration; 2 | using AspectCore.DynamicProxy; 3 | using AspectCore.DependencyInjection; 4 | 5 | namespace AspectCore.Tests.Integrate 6 | { 7 | public class IntegrateTestBase 8 | { 9 | public IServiceResolver ServiceResolver { get; } 10 | 11 | public IServiceContext ServiceContext { get; } 12 | 13 | public IntegrateTestBase() 14 | { 15 | ServiceContext = new ServiceContext(); 16 | ServiceContext.Configure(Configure); 17 | ConfigureService(ServiceContext); 18 | ServiceResolver = ServiceContext.Build(); 19 | } 20 | 21 | protected virtual void ConfigureService(IServiceContext serviceContext) { } 22 | 23 | protected virtual void Configure(IAspectConfiguration configuration) { } 24 | } 25 | } -------------------------------------------------------------------------------- /tests/AspectCore.Tests/Issues/DynamicProxy/InterfaceDefaultMethodsShouldBeUsedTests.cs: -------------------------------------------------------------------------------- 1 | using AspectCore.DynamicProxy; 2 | using AspectCore.Tests.DynamicProxy; 3 | using Xunit; 4 | 5 | namespace AspectCore.Tests.Issues.DynamicProxy; 6 | 7 | // https://github.com/dotnetcore/AspectCore-Framework/issues/223 8 | public class InterfaceDefaultMethodsShouldBeUsedTests : DynamicProxyTestBase 9 | { 10 | public interface IService 11 | { 12 | int Get() => 1; 13 | } 14 | 15 | public class Service : IService{} 16 | 17 | [Fact] 18 | public void CreateInterfaceProxy_WithoutImplementationType_Test() 19 | { 20 | var service = ProxyGenerator.CreateInterfaceProxy(); 21 | var result = service.Get(); 22 | Assert.Equal(1, result); 23 | } 24 | } -------------------------------------------------------------------------------- /tests/AspectCore.Tests/Issues/DynamicProxy/NullableEnumWithDefaultValueTests.cs: -------------------------------------------------------------------------------- 1 | using System.Threading.Tasks; 2 | using AspectCore.DependencyInjection; 3 | using AspectCore.DynamicProxy; 4 | using AspectCore.Tests.DynamicProxy; 5 | using Xunit; 6 | 7 | namespace AspectCore.Tests.Issues.DynamicProxy 8 | { 9 | // https://github.com/dotnetcore/AspectCore-Framework/issues/203 10 | public class NullableEnumWithDefaultValueTests : DynamicProxyTestBase 11 | { 12 | public class Interceptor : AbstractInterceptorAttribute 13 | { 14 | public override Task Invoke(AspectContext context, AspectDelegate next) 15 | { 16 | return context.Invoke(next); 17 | } 18 | } 19 | 20 | public class Service 21 | { 22 | [Interceptor] 23 | public virtual bool Get(Bool? flag = Bool.True) 24 | { 25 | return flag == Bool.True; 26 | } 27 | } 28 | 29 | public enum Bool 30 | { 31 | False, 32 | True 33 | } 34 | 35 | [Fact] 36 | public void ClassProxy_Test() 37 | { 38 | var service = ProxyGenerator.CreateClassProxy(); 39 | var output = service.Get(); 40 | Assert.True(output); 41 | } 42 | } 43 | } 44 | --------------------------------------------------------------------------------