From a497669c03ed4616b484c270d40d010c6f516626 Mon Sep 17 00:00:00 2001 From: Sachira Chinthana Jayasanka Date: Fri, 26 Apr 2019 09:54:03 +0530 Subject: [PATCH 1/3] Project updated to use .NET Standard 2.0 instead of .NET Core for better compatibility --- SimpleProxy.Caching/SimpleProxy.Caching.csproj | 2 +- SimpleProxy.Diagnostics/SimpleProxy.Diagnostics.csproj | 4 ++-- SimpleProxy.Logging/SimpleProxy.Logging.csproj | 4 ++-- SimpleProxy.UnitOfWork/SimpleProxy.UnitOfWork.csproj | 4 ++-- SimpleProxy/InvocationContext.cs | 2 +- SimpleProxy/SimpleProxy.csproj | 2 +- 6 files changed, 9 insertions(+), 9 deletions(-) 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/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..5ea1865 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 From 7a492980d7c21949a99adf9bd5cf3946e5209c0d Mon Sep 17 00:00:00 2001 From: Sachira Chinthana Jayasanka Date: Fri, 26 Apr 2019 10:14:41 +0530 Subject: [PATCH 2/3] .NET Core 2.2 reference removed from project --- SimpleProxy/SimpleProxy.csproj | 7 ------- 1 file changed, 7 deletions(-) diff --git a/SimpleProxy/SimpleProxy.csproj b/SimpleProxy/SimpleProxy.csproj index 5ea1865..714f8f1 100644 --- a/SimpleProxy/SimpleProxy.csproj +++ b/SimpleProxy/SimpleProxy.csproj @@ -13,11 +13,4 @@ - - - - C:\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.netcore.app\2.2.0\ref\netcoreapp2.2\System.Runtime.dll - - - From 0494b3101d5e97614e1c4cd05779ed42ad048640 Mon Sep 17 00:00:00 2001 From: Sachira Chinthana Jayasanka Date: Fri, 26 Apr 2019 11:22:55 +0530 Subject: [PATCH 3/3] Added interception for async methods Currently interception of async methods doesn't work as expected. Added Castle.Core.AsyncInterceptor and used IAsyncInterceptor in it to intercept async methods. --- SimpleProxy/Internal/CoreInterceptor.cs | 112 +++++++++++++++++++++--- SimpleProxy/SimpleProxy.csproj | 1 + 2 files changed, 100 insertions(+), 13 deletions(-) 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/SimpleProxy.csproj b/SimpleProxy/SimpleProxy.csproj index 714f8f1..880740c 100644 --- a/SimpleProxy/SimpleProxy.csproj +++ b/SimpleProxy/SimpleProxy.csproj @@ -9,6 +9,7 @@ +