Skip to content

Commit aa00865

Browse files
fix #279 (#295)
* fix #279 * Change AspectExceptionWrapper lifetime to Transient
1 parent cbecc37 commit aa00865

File tree

8 files changed

+46
-30
lines changed

8 files changed

+46
-30
lines changed
Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Runtime.ExceptionServices;
23
using AspectCore.Configuration;
34

45
namespace AspectCore.DynamicProxy
@@ -7,25 +8,27 @@ namespace AspectCore.DynamicProxy
78
public class AspectExceptionWrapper : IAspectExceptionWrapper
89
{
910
private readonly IAspectConfiguration _configuration;
11+
private ExceptionDispatchInfo _exceptionInfo;
1012

1113
public AspectExceptionWrapper(IAspectConfiguration configuration)
1214
{
1315
_configuration = configuration;
1416
}
1517

16-
public Exception Wrap(AspectContext aspectContext, Exception exception)
18+
public void Wrap(AspectContext aspectContext, Exception exception)
1719
{
18-
if (!_configuration.ThrowAspectException)
20+
if (!_configuration.ThrowAspectException || exception is AspectInvocationException _)
1921
{
20-
return exception;
22+
_exceptionInfo = ExceptionDispatchInfo.Capture(exception);
23+
return;
2124
}
2225

23-
if (exception is AspectInvocationException aspectInvocationException)
24-
{
25-
return aspectInvocationException;
26-
}
26+
_exceptionInfo = ExceptionDispatchInfo.Capture(new AspectInvocationException(aspectContext, exception));
27+
}
2728

28-
return new AspectInvocationException(aspectContext, exception);
29+
public void ThrowIfFailed()
30+
{
31+
_exceptionInfo?.Throw();
2932
}
3033
}
3134
}

src/AspectCore.Abstractions/DynamicProxy/IAspectExceptionWrapper.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ namespace AspectCore.DynamicProxy
44
{
55
public interface IAspectExceptionWrapper
66
{
7-
Exception Wrap(AspectContext aspectContext, Exception exception);
7+
void Wrap(AspectContext aspectContext, Exception exception);
8+
9+
void ThrowIfFailed();
810
}
911
}

src/AspectCore.Core/DependencyInjection/ServiceContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ private void AddInternalServices()
100100
if (!Contains(typeof(IAspectCachingProvider)))
101101
Singletons.AddType<IAspectCachingProvider, AspectCachingProvider>();
102102
if (!Contains(typeof(IAspectExceptionWrapper)))
103-
Singletons.AddType<IAspectExceptionWrapper, AspectExceptionWrapper>();
103+
Transients.AddType<IAspectExceptionWrapper, AspectExceptionWrapper>();
104104
}
105105

106106
public int Count => _collection.Count;

src/AspectCore.Core/DynamicProxy/AspectActivator.cs

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
using System;
2-
using System.Diagnostics;
1+
using AspectCore.Core.Utils;
2+
using System;
33
using System.Threading.Tasks;
4-
using AspectCore.Core.Utils;
5-
using AspectCore.Utils;
64

75
namespace AspectCore.DynamicProxy
86
{
@@ -29,7 +27,10 @@ public TResult Invoke<TResult>(AspectActivatorContext activatorContext)
2927
var aspectBuilder = _aspectBuilderFactory.Create(context);
3028
var task = aspectBuilder.Build()(context);
3129
if (task.IsFaulted)
32-
throw _aspectExceptionWrapper.Wrap(context, task.Exception.InnerException);
30+
{
31+
_aspectExceptionWrapper.Wrap(context, task.Exception.InnerException);
32+
return default;
33+
}
3334
if (!task.IsCompleted)
3435
{
3536
// try to avoid potential deadlocks.
@@ -41,10 +42,12 @@ public TResult Invoke<TResult>(AspectActivatorContext activatorContext)
4142
}
4243
catch (Exception ex)
4344
{
44-
throw _aspectExceptionWrapper.Wrap(context, ex);
45+
_aspectExceptionWrapper.Wrap(context, ex);
46+
return default;
4547
}
4648
finally
4749
{
50+
_aspectExceptionWrapper.ThrowIfFailed();
4851
_aspectContextFactory.ReleaseContext(context);
4952
}
5053
}
@@ -59,7 +62,8 @@ public async Task<TResult> InvokeTask<TResult>(AspectActivatorContext activatorC
5962

6063
if (invoke.IsFaulted)
6164
{
62-
throw _aspectExceptionWrapper.Wrap(context, invoke.Exception?.InnerException);
65+
_aspectExceptionWrapper.Wrap(context, invoke.Exception.InnerException);
66+
return default;
6367
}
6468

6569
if (!invoke.IsCompleted)
@@ -70,22 +74,25 @@ public async Task<TResult> InvokeTask<TResult>(AspectActivatorContext activatorC
7074
switch (context.ReturnValue)
7175
{
7276
case null:
73-
return default(TResult);
77+
return default;
7478
case Task<TResult> taskWithResult:
7579
return taskWithResult.Result;
7680
case Task _:
77-
return default(TResult);
81+
return default;
7882
default:
79-
throw _aspectExceptionWrapper.Wrap(context, new InvalidCastException(
83+
_aspectExceptionWrapper.Wrap(context, new InvalidCastException(
8084
$"Unable to cast object of type '{context.ReturnValue.GetType()}' to type '{typeof(Task<TResult>)}'."));
85+
return default;
8186
}
8287
}
8388
catch (Exception ex)
8489
{
85-
throw _aspectExceptionWrapper.Wrap(context, ex);
90+
_aspectExceptionWrapper.Wrap(context, ex);
91+
return default;
8692
}
8793
finally
8894
{
95+
_aspectExceptionWrapper.ThrowIfFailed();
8996
_aspectContextFactory.ReleaseContext(context);
9097
}
9198
}
@@ -100,7 +107,8 @@ public async ValueTask<TResult> InvokeValueTask<TResult>(AspectActivatorContext
100107

101108
if (invoke.IsFaulted)
102109
{
103-
throw _aspectExceptionWrapper.Wrap(context, invoke.Exception?.InnerException);
110+
_aspectExceptionWrapper.Wrap(context, invoke.Exception.InnerException);
111+
return default;
104112
}
105113

106114
if (!invoke.IsCompleted)
@@ -111,22 +119,25 @@ public async ValueTask<TResult> InvokeValueTask<TResult>(AspectActivatorContext
111119
switch (context.ReturnValue)
112120
{
113121
case null:
114-
return default(TResult);
122+
return default;
115123
case ValueTask<TResult> taskWithResult:
116124
return taskWithResult.Result;
117125
case ValueTask task:
118-
return default(TResult);
126+
return default;
119127
default:
120-
throw _aspectExceptionWrapper.Wrap(context, new InvalidCastException(
128+
_aspectExceptionWrapper.Wrap(context, new InvalidCastException(
121129
$"Unable to cast object of type '{context.ReturnValue.GetType()}' to type '{typeof(ValueTask<TResult>)}'."));
130+
return default;
122131
}
123132
}
124133
catch (Exception ex)
125134
{
126-
throw _aspectExceptionWrapper.Wrap(context, ex);
135+
_aspectExceptionWrapper.Wrap(context, ex);
136+
return default;
127137
}
128138
finally
129139
{
140+
_aspectExceptionWrapper.ThrowIfFailed();
130141
_aspectContextFactory.ReleaseContext(context);
131142
}
132143
}

src/AspectCore.Extensions.Autofac/ContainerBuilderExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public static ContainerBuilder RegisterDynamicProxy(this ContainerBuilder contai
5858
containerBuilder.RegisterType<AspectBuilderFactory>().As<IAspectBuilderFactory>().SingleInstance();
5959
containerBuilder.RegisterType<ProxyTypeGenerator>().As<IProxyTypeGenerator>().SingleInstance();
6060
containerBuilder.RegisterType<AspectCachingProvider>().As<IAspectCachingProvider>().SingleInstance();
61-
containerBuilder.RegisterType<AspectExceptionWrapper>().As<IAspectExceptionWrapper>().SingleInstance();
61+
containerBuilder.RegisterType<AspectExceptionWrapper>().As<IAspectExceptionWrapper>().InstancePerDependency();
6262

6363
//全局注册中间件
6464
containerBuilder.ComponentRegistryBuilder.Registered += (sender, args) =>

src/AspectCore.Extensions.DependencyInjection/ServiceCollectionExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ internal static IServiceCollection TryAddDynamicProxyServices(this IServiceColle
5959
services.TryAddSingleton<IAspectBuilderFactory, AspectBuilderFactory>();
6060
services.TryAddSingleton<IProxyTypeGenerator, ProxyTypeGenerator>();
6161
services.TryAddSingleton<IAspectCachingProvider, AspectCachingProvider>();
62-
services.TryAddSingleton<IAspectExceptionWrapper, AspectExceptionWrapper>();
62+
services.TryAddTransient<IAspectExceptionWrapper, AspectExceptionWrapper>();
6363

6464
services.AddSingleton<IInterceptorSelector, ConfigureInterceptorSelector>();
6565
services.AddSingleton<IInterceptorSelector, AttributeInterceptorSelector>();

src/AspectCore.Extensions.LightInject/ContainerBuilderExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public static IServiceContainer RegisterDynamicProxy(this IServiceContainer cont
7373
.AddSingleton<IAspectBuilderFactory, AspectBuilderFactory>()
7474
.AddSingleton<IProxyTypeGenerator, ProxyTypeGenerator>()
7575
.AddSingleton<IAspectCachingProvider, AspectCachingProvider>()
76-
.AddSingleton<IAspectExceptionWrapper, AspectExceptionWrapper>();
76+
.AddTransient(typeof(IAspectExceptionWrapper), typeof(AspectExceptionWrapper));
7777

7878
var aspectValidator = new AspectValidatorBuilder(aspectConfig).Build();
7979
container.Decorate(aspectValidator.CreateDecorator());

src/AspectCore.Extensions.Windsor/AspectCoreFacility.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public void Init(IKernel kernel, IConfiguration facilityConfig)
3636
Component.For<IInterceptorCollector>().ImplementedBy<InterceptorCollector>().LifestyleSingleton(),
3737
Component.For<IAspectContextFactory>().ImplementedBy<AspectContextFactory>().LifestyleSingleton(),
3838
Component.For<IAspectCachingProvider>().ImplementedBy<AspectCachingProvider>().LifestyleSingleton(),
39-
Component.For<IAspectExceptionWrapper>().ImplementedBy<AspectExceptionWrapper>().LifestyleSingleton(),
39+
Component.For<IAspectExceptionWrapper>().ImplementedBy<AspectExceptionWrapper>().LifestyleTransient(),
4040
Component.For<IAspectActivatorFactory>().ImplementedBy<AspectActivatorFactory>().LifestyleSingleton(),
4141
Component.For<IAspectValidatorBuilder>().ImplementedBy<AspectValidatorBuilder>().LifestyleSingleton(),
4242
Component.For<IPropertyInjectorFactory>().ImplementedBy<PropertyInjectorFactory>().LifestyleSingleton(),

0 commit comments

Comments
 (0)