Skip to content

#279, #295 仍有问题,重构 #306

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 0 additions & 34 deletions src/AspectCore.Abstractions/DynamicProxy/AspectExceptionWrapper.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace AspectCore.DynamicProxy
{
public class AspectInvalidCastException : AspectInvocationException
{
public AspectInvalidCastException(AspectContext aspectContext, string message) : base(aspectContext, message) { }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace AspectCore.DynamicProxy
{
public class AspectInvalidOperationException : AspectInvocationException
{
public AspectInvalidOperationException(AspectContext aspectContext, string message) : base(aspectContext, message) { }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ public class AspectInvocationException : Exception
{
public AspectContext AspectContext { get; }

public AspectInvocationException(AspectContext aspectContext, string message) : this(aspectContext, message, null) { }

public AspectInvocationException(AspectContext aspectContext, Exception innerException)
: base($"Exception has been thrown by the aspect of an invocation. ---> {innerException?.Message}.", innerException)
: this(aspectContext, $"Exception has been thrown by the aspect of an invocation. ---> {innerException?.Message}.", innerException) { }

public AspectInvocationException(AspectContext aspectContext, string message, Exception innerException) : base(message, innerException)
{
AspectContext = aspectContext;
}
Expand Down

This file was deleted.

10 changes: 4 additions & 6 deletions src/AspectCore.Core/DependencyInjection/ServiceContext.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using System;
using AspectCore.Configuration;
using AspectCore.DynamicProxy;
using AspectCore.DynamicProxy.Parameters;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using AspectCore.Configuration;
using AspectCore.DynamicProxy;
using AspectCore.DynamicProxy.Parameters;

namespace AspectCore.DependencyInjection
{
Expand Down Expand Up @@ -99,8 +99,6 @@ private void AddInternalServices()
Scopeds.AddType<IParameterInterceptorSelector, ParameterInterceptorSelector>();
if (!Contains(typeof(IAspectCachingProvider)))
Singletons.AddType<IAspectCachingProvider, AspectCachingProvider>();
if (!Contains(typeof(IAspectExceptionWrapper)))
Transients.AddType<IAspectExceptionWrapper, AspectExceptionWrapper>();
}

public int Count => _collection.Count;
Expand Down
49 changes: 23 additions & 26 deletions src/AspectCore.Core/DynamicProxy/AspectActivator.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using AspectCore.Core.Utils;
using AspectCore.Configuration;
using AspectCore.Core.Utils;
using System;
using System.Runtime.ExceptionServices;
using System.Threading.Tasks;

namespace AspectCore.DynamicProxy
Expand All @@ -9,14 +11,13 @@ internal sealed class AspectActivator : IAspectActivator
{
private readonly IAspectContextFactory _aspectContextFactory;
private readonly IAspectBuilderFactory _aspectBuilderFactory;
private readonly IAspectExceptionWrapper _aspectExceptionWrapper;
private readonly IAspectConfiguration _aspectConfiguration;

public AspectActivator(IAspectContextFactory aspectContextFactory, IAspectBuilderFactory aspectBuilderFactory,
IAspectExceptionWrapper aspectExceptionWrapper)
public AspectActivator(IAspectContextFactory aspectContextFactory, IAspectBuilderFactory aspectBuilderFactory, IAspectConfiguration aspectConfiguration)
{
_aspectContextFactory = aspectContextFactory;
_aspectBuilderFactory = aspectBuilderFactory;
_aspectExceptionWrapper = aspectExceptionWrapper;
_aspectConfiguration = aspectConfiguration;
}

public TResult Invoke<TResult>(AspectActivatorContext activatorContext)
Expand All @@ -28,8 +29,7 @@ public TResult Invoke<TResult>(AspectActivatorContext activatorContext)
var task = aspectBuilder.Build()(context);
if (task.IsFaulted)
{
_aspectExceptionWrapper.Wrap(context, task.Exception.InnerException);
return default;
ExceptionDispatchInfo.Capture(task.Exception.InnerException).Throw();
}
if (!task.IsCompleted)
{
Expand All @@ -42,12 +42,13 @@ public TResult Invoke<TResult>(AspectActivatorContext activatorContext)
}
catch (Exception ex)
{
_aspectExceptionWrapper.Wrap(context, ex);
return default;
if (!_aspectConfiguration.ThrowAspectException || ex is AspectInvocationException _)
throw;

throw new AspectInvocationException(context, ex);
}
finally
{
_aspectExceptionWrapper.ThrowIfFailed();
_aspectContextFactory.ReleaseContext(context);
}
}
Expand All @@ -62,8 +63,7 @@ public async Task<TResult> InvokeTask<TResult>(AspectActivatorContext activatorC

if (invoke.IsFaulted)
{
_aspectExceptionWrapper.Wrap(context, invoke.Exception.InnerException);
return default;
ExceptionDispatchInfo.Capture(invoke.Exception.InnerException).Throw();
}

if (!invoke.IsCompleted)
Expand All @@ -80,19 +80,18 @@ public async Task<TResult> InvokeTask<TResult>(AspectActivatorContext activatorC
case Task _:
return default;
default:
_aspectExceptionWrapper.Wrap(context, new InvalidCastException(
$"Unable to cast object of type '{context.ReturnValue.GetType()}' to type '{typeof(Task<TResult>)}'."));
return default;
throw new AspectInvalidCastException(context, $"Unable to cast object of type '{context.ReturnValue.GetType()}' to type '{typeof(Task<TResult>)}'.");
}
}
catch (Exception ex)
{
_aspectExceptionWrapper.Wrap(context, ex);
return default;
if (!_aspectConfiguration.ThrowAspectException || ex is AspectInvocationException _)
throw;

throw new AspectInvocationException(context, ex);
}
finally
{
_aspectExceptionWrapper.ThrowIfFailed();
_aspectContextFactory.ReleaseContext(context);
}
}
Expand All @@ -107,8 +106,7 @@ public async ValueTask<TResult> InvokeValueTask<TResult>(AspectActivatorContext

if (invoke.IsFaulted)
{
_aspectExceptionWrapper.Wrap(context, invoke.Exception.InnerException);
return default;
ExceptionDispatchInfo.Capture(invoke.Exception.InnerException).Throw();
}

if (!invoke.IsCompleted)
Expand All @@ -125,19 +123,18 @@ public async ValueTask<TResult> InvokeValueTask<TResult>(AspectActivatorContext
case ValueTask task:
return default;
default:
_aspectExceptionWrapper.Wrap(context, new InvalidCastException(
$"Unable to cast object of type '{context.ReturnValue.GetType()}' to type '{typeof(ValueTask<TResult>)}'."));
return default;
throw new AspectInvalidCastException(context, $"Unable to cast object of type '{context.ReturnValue.GetType()}' to type '{typeof(ValueTask<TResult>)}'.");
}
}
catch (Exception ex)
{
_aspectExceptionWrapper.Wrap(context, ex);
return default;
if (!_aspectConfiguration.ThrowAspectException || ex is AspectInvocationException _)
throw;

throw new AspectInvocationException(context, ex);
}
finally
{
_aspectExceptionWrapper.ThrowIfFailed();
_aspectContextFactory.ReleaseContext(context);
}
}
Expand Down
11 changes: 6 additions & 5 deletions src/AspectCore.Core/DynamicProxy/AspectActivatorFactory.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using AspectCore.Configuration;
using System;

namespace AspectCore.DynamicProxy
{
Expand All @@ -7,18 +8,18 @@ public sealed class AspectActivatorFactory : IAspectActivatorFactory
{
private readonly IAspectContextFactory _aspectContextFactory;
private readonly IAspectBuilderFactory _aspectBuilderFactory;
private readonly IAspectExceptionWrapper _aspectExceptionWrapper;
private readonly IAspectConfiguration _aspectConfiguration;

public AspectActivatorFactory(IAspectContextFactory aspectContextFactory, IAspectBuilderFactory aspectBuilderFactory, IAspectExceptionWrapper aspectExceptionWrapper)
public AspectActivatorFactory(IAspectContextFactory aspectContextFactory, IAspectBuilderFactory aspectBuilderFactory, IAspectConfiguration aspectConfiguration)
{
_aspectContextFactory = aspectContextFactory ?? throw new ArgumentNullException(nameof(aspectContextFactory));
_aspectBuilderFactory = aspectBuilderFactory ?? throw new ArgumentNullException(nameof(aspectBuilderFactory));
_aspectExceptionWrapper = aspectExceptionWrapper ?? throw new ArgumentNullException(nameof(aspectExceptionWrapper));
_aspectConfiguration = aspectConfiguration ?? throw new ArgumentNullException(nameof(aspectConfiguration));
}

public IAspectActivator Create()
{
return new AspectActivator(_aspectContextFactory, _aspectBuilderFactory, _aspectExceptionWrapper);
return new AspectActivator(_aspectContextFactory, _aspectBuilderFactory, _aspectConfiguration);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public static Task<object> UnwrapAsyncReturnValue(this AspectContext aspectConte

if (!aspectContext.IsAsync())
{
throw new AspectInvocationException(aspectContext, new InvalidOperationException("This operation only support asynchronous method."));
throw new AspectInvalidOperationException(aspectContext, "This operation only support asynchronous method.");
}

var returnValue = aspectContext.ReturnValue;
Expand Down
16 changes: 4 additions & 12 deletions src/AspectCore.Extensions.Autofac/ContainerBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using AspectCore.Configuration;
using AspectCore.Configuration;
using AspectCore.DependencyInjection;
using AspectCore.DynamicProxy;
using AspectCore.DynamicProxy.Parameters;
using AspectCore.DependencyInjection;
using Autofac;
using Autofac.Core;
using Autofac.Core.Activators;
using Autofac.Core.Activators.Delegate;
using Autofac.Core.Activators.Reflection;
using Autofac.Core.Resolving.Pipeline;
using AParameter = Autofac.Core.Parameter;
using System;
using System.Linq;

namespace AspectCore.Extensions.Autofac
{
Expand Down Expand Up @@ -58,7 +51,6 @@ public static ContainerBuilder RegisterDynamicProxy(this ContainerBuilder contai
containerBuilder.RegisterType<AspectBuilderFactory>().As<IAspectBuilderFactory>().SingleInstance();
containerBuilder.RegisterType<ProxyTypeGenerator>().As<IProxyTypeGenerator>().SingleInstance();
containerBuilder.RegisterType<AspectCachingProvider>().As<IAspectCachingProvider>().SingleInstance();
containerBuilder.RegisterType<AspectExceptionWrapper>().As<IAspectExceptionWrapper>().InstancePerDependency();

//全局注册中间件
containerBuilder.ComponentRegistryBuilder.Registered += (sender, args) =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using System;
using System.Linq;
using AspectCore.Configuration;
using AspectCore.Configuration;
using AspectCore.DependencyInjection;
using AspectCore.DynamicProxy;
using AspectCore.DynamicProxy.Parameters;
using AspectCore.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using System;
using System.Linq;

namespace AspectCore.Extensions.DependencyInjection
{
Expand Down Expand Up @@ -59,7 +59,6 @@ internal static IServiceCollection TryAddDynamicProxyServices(this IServiceColle
services.TryAddSingleton<IAspectBuilderFactory, AspectBuilderFactory>();
services.TryAddSingleton<IProxyTypeGenerator, ProxyTypeGenerator>();
services.TryAddSingleton<IAspectCachingProvider, AspectCachingProvider>();
services.TryAddTransient<IAspectExceptionWrapper, AspectExceptionWrapper>();

services.AddSingleton<IInterceptorSelector, ConfigureInterceptorSelector>();
services.AddSingleton<IInterceptorSelector, AttributeInterceptorSelector>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using System;
using System.Collections.Concurrent;
using System.Linq;
using System.Reflection;
using AspectCore.Configuration;
using AspectCore.Configuration;
using AspectCore.DependencyInjection;
using AspectCore.DynamicProxy;
using AspectCore.DynamicProxy.Parameters;
using LightInject;
using System;
using System.Linq;
using System.Reflection;
using IServiceContainer = LightInject.IServiceContainer;

namespace AspectCore.Extensions.LightInject
Expand Down Expand Up @@ -72,8 +71,7 @@ public static IServiceContainer RegisterDynamicProxy(this IServiceContainer cont
.AddSingleton<IAspectValidatorBuilder, AspectValidatorBuilder>()
.AddSingleton<IAspectBuilderFactory, AspectBuilderFactory>()
.AddSingleton<IProxyTypeGenerator, ProxyTypeGenerator>()
.AddSingleton<IAspectCachingProvider, AspectCachingProvider>()
.AddTransient(typeof(IAspectExceptionWrapper), typeof(AspectExceptionWrapper));
.AddSingleton<IAspectCachingProvider, AspectCachingProvider>();

var aspectValidator = new AspectValidatorBuilder(aspectConfig).Build();
container.Decorate(aspectValidator.CreateDecorator());
Expand Down
9 changes: 4 additions & 5 deletions src/AspectCore.Extensions.Windsor/AspectCoreFacility.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
using System;
using System.Linq;
using AspectCore.Configuration;
using AspectCore.Configuration;
using AspectCore.DependencyInjection;
using AspectCore.DynamicProxy;
using AspectCore.DynamicProxy.Parameters;
using AspectCore.DependencyInjection;
using Castle.Core;
using Castle.Core.Configuration;
using Castle.MicroKernel;
using Castle.MicroKernel.Registration;
using System;
using System.Linq;

namespace AspectCore.Extensions.Windsor
{
Expand Down Expand Up @@ -36,7 +36,6 @@ public void Init(IKernel kernel, IConfiguration facilityConfig)
Component.For<IInterceptorCollector>().ImplementedBy<InterceptorCollector>().LifestyleSingleton(),
Component.For<IAspectContextFactory>().ImplementedBy<AspectContextFactory>().LifestyleSingleton(),
Component.For<IAspectCachingProvider>().ImplementedBy<AspectCachingProvider>().LifestyleSingleton(),
Component.For<IAspectExceptionWrapper>().ImplementedBy<AspectExceptionWrapper>().LifestyleTransient(),
Component.For<IAspectActivatorFactory>().ImplementedBy<AspectActivatorFactory>().LifestyleSingleton(),
Component.For<IAspectValidatorBuilder>().ImplementedBy<AspectValidatorBuilder>().LifestyleSingleton(),
Component.For<IPropertyInjectorFactory>().ImplementedBy<PropertyInjectorFactory>().LifestyleSingleton(),
Expand Down
Loading