diff --git a/SimpleProxy.Caching/SimpleProxy.Caching.csproj b/SimpleProxy.Caching/SimpleProxy.Caching.csproj index bd2dfae..64ab230 100644 --- a/SimpleProxy.Caching/SimpleProxy.Caching.csproj +++ b/SimpleProxy.Caching/SimpleProxy.Caching.csproj @@ -1,7 +1,7 @@  - netcoreapp2.2 + netstandard2.0 diff --git a/SimpleProxy.Diagnostics/SimpleProxy.Diagnostics.csproj b/SimpleProxy.Diagnostics/SimpleProxy.Diagnostics.csproj index 1e4d55e..e668179 100644 --- a/SimpleProxy.Diagnostics/SimpleProxy.Diagnostics.csproj +++ b/SimpleProxy.Diagnostics/SimpleProxy.Diagnostics.csproj @@ -1,7 +1,7 @@ - + - netcoreapp2.2 + netstandard2.0 diff --git a/SimpleProxy.Logging/SimpleProxy.Logging.csproj b/SimpleProxy.Logging/SimpleProxy.Logging.csproj index 848bdba..7f2c09f 100644 --- a/SimpleProxy.Logging/SimpleProxy.Logging.csproj +++ b/SimpleProxy.Logging/SimpleProxy.Logging.csproj @@ -1,7 +1,7 @@ - + - netcoreapp2.2 + netstandard2.0 diff --git a/SimpleProxy.UnitOfWork/SimpleProxy.UnitOfWork.csproj b/SimpleProxy.UnitOfWork/SimpleProxy.UnitOfWork.csproj index cd16c99..91521d5 100644 --- a/SimpleProxy.UnitOfWork/SimpleProxy.UnitOfWork.csproj +++ b/SimpleProxy.UnitOfWork/SimpleProxy.UnitOfWork.csproj @@ -1,7 +1,7 @@ - + - netcoreapp2.2 + netstandard2.0 diff --git a/SimpleProxy/Internal/CoreInterceptor.cs b/SimpleProxy/Internal/CoreInterceptor.cs index 1a8c09a..83c199a 100644 --- a/SimpleProxy/Internal/CoreInterceptor.cs +++ b/SimpleProxy/Internal/CoreInterceptor.cs @@ -1,4 +1,8 @@ -namespace SimpleProxy.Internal +using System.Collections.Generic; +using System.Threading.Tasks; +using SimpleProxy.Interfaces; + +namespace SimpleProxy.Internal { using System; using System.Linq; @@ -9,7 +13,7 @@ /// /// The Master Interceptor Class wraps a proxied object and handles all of its interceptions /// - internal class CoreInterceptor : IInterceptor + internal class CoreInterceptor : IAsyncInterceptor { /// /// Gets the @@ -32,34 +36,116 @@ public CoreInterceptor(IServiceProvider serviceProvider, SimpleProxyConfiguratio this.proxyConfiguration = proxyConfiguration; } - /// - public void Intercept(IInvocation invocation) + public void InterceptSynchronous(IInvocation invocation) { - // Map the configured interceptors to this type based on its attributes - var invocationMetadataCollection = InvocationExtensions.GetInterceptorMetadataForMethod(invocation, this.serviceProvider, this.proxyConfiguration); + var proceedWithInterception = InterceptBeforeProceed(invocation, out var invocationMetadataCollection, out var orderingStrategy); - // If there are no configured interceptors, leave now - if (invocationMetadataCollection == null || !invocationMetadataCollection.Any()) + if (!proceedWithInterception) { invocation.Proceed(); return; } - // Get the Ordering Strategy for Interceptors - var orderingStrategy = this.proxyConfiguration.OrderingStrategy; + // Execute the Real Method + if (!invocationMetadataCollection.Any(p => p.InvocationIsBypassed)) + { + invocation.Proceed(); + } - // Process the BEFORE Interceptions - foreach (var invocationContext in orderingStrategy.OrderBeforeInterception(invocationMetadataCollection)) + InterceptAfterProceed(invocationMetadataCollection, orderingStrategy); + } + + public void InterceptAsynchronous(IInvocation invocation) + { + invocation.ReturnValue = InternalInterceptAsynchronous(invocation); + } + + private async Task InternalInterceptAsynchronous(IInvocation invocation) + { + var proceedWithInterception = InterceptBeforeProceed(invocation, out var invocationMetadataCollection, out var orderingStrategy); + + if (!proceedWithInterception) { - invocationContext.Interceptor.BeforeInvoke(invocationContext); + invocation.Proceed(); + var task = (Task)invocation.ReturnValue; + await task; + return; } // Execute the Real Method if (!invocationMetadataCollection.Any(p => p.InvocationIsBypassed)) { invocation.Proceed(); + var task = (Task)invocation.ReturnValue; + await task; } + InterceptAfterProceed(invocationMetadataCollection, orderingStrategy); + } + + public void InterceptAsynchronous(IInvocation invocation) + { + invocation.ReturnValue = InternalInterceptAsynchronous(invocation); + } + + private async Task InternalInterceptAsynchronous(IInvocation invocation) + { + var proceedWithInterception = InterceptBeforeProceed(invocation, out var invocationMetadataCollection, out var orderingStrategy); + + TResult result; + + if (!proceedWithInterception) + { + invocation.Proceed(); + var task = (Task)invocation.ReturnValue; + result = await task; + return result; + } + + // Execute the Real Method + if (!invocationMetadataCollection.Any(p => p.InvocationIsBypassed)) + { + invocation.Proceed(); + var task = (Task)invocation.ReturnValue; + result = await task; + } + else + { + result = default; + } + + InterceptAfterProceed(invocationMetadataCollection, orderingStrategy); + + return result; + } + + + private bool InterceptBeforeProceed(IInvocation invocation, out List invocationMetadataCollection, out IOrderingStrategy orderingStrategy) + { + // Map the configured interceptors to this type based on its attributes + invocationMetadataCollection = InvocationExtensions.GetInterceptorMetadataForMethod(invocation, this.serviceProvider, this.proxyConfiguration); + + // If there are no configured interceptors, leave now + if (invocationMetadataCollection == null || !invocationMetadataCollection.Any()) + { + orderingStrategy = null; + return false; + } + + // Get the Ordering Strategy for Interceptors + orderingStrategy = this.proxyConfiguration.OrderingStrategy; + + // Process the BEFORE Interceptions + foreach (var invocationContext in orderingStrategy.OrderBeforeInterception(invocationMetadataCollection)) + { + invocationContext.Interceptor.BeforeInvoke(invocationContext); + } + + return true; + } + + private static void InterceptAfterProceed(List invocationMetadataCollection, IOrderingStrategy orderingStrategy) + { // Process the AFTER Interceptions foreach (var invocationContext in orderingStrategy.OrderAfterInterception(invocationMetadataCollection)) { diff --git a/SimpleProxy/InvocationContext.cs b/SimpleProxy/InvocationContext.cs index b88f852..54dbd6f 100644 --- a/SimpleProxy/InvocationContext.cs +++ b/SimpleProxy/InvocationContext.cs @@ -136,7 +136,7 @@ public void SetTemporaryData(string name, object value) /// public object GetTemporaryData(string name) { - return this.TempData.GetValueOrDefault(name); + return this.TempData.TryGetValue(name, out var value) ? value : default; } /// diff --git a/SimpleProxy/SimpleProxy.csproj b/SimpleProxy/SimpleProxy.csproj index 4e3c345..880740c 100644 --- a/SimpleProxy/SimpleProxy.csproj +++ b/SimpleProxy/SimpleProxy.csproj @@ -1,7 +1,7 @@  - netcoreapp2.2 + netstandard2.0 Simple Proxy is a simple extension library built to solve the problem of Aspect Orientated Programming in Net Core projects Robert Perry https://github.com/f135ta/SimpleProxy @@ -9,15 +9,9 @@ + - - - - C:\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.netcore.app\2.2.0\ref\netcoreapp2.2\System.Runtime.dll - - -