Skip to content

Commit cddd616

Browse files
#279, #295 仍有问题,重构 (#306)
1 parent b479bf3 commit cddd616

File tree

15 files changed

+232
-121
lines changed

15 files changed

+232
-121
lines changed

src/AspectCore.Abstractions/DynamicProxy/AspectExceptionWrapper.cs

Lines changed: 0 additions & 34 deletions
This file was deleted.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace AspectCore.DynamicProxy
2+
{
3+
public class AspectInvalidCastException : AspectInvocationException
4+
{
5+
public AspectInvalidCastException(AspectContext aspectContext, string message) : base(aspectContext, message) { }
6+
}
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@ public class AspectInvocationException : Exception
66
{
77
public AspectContext AspectContext { get; }
88

9+
public AspectInvocationException(AspectContext aspectContext, string message) : this(aspectContext, message, null) { }
10+
911
public AspectInvocationException(AspectContext aspectContext, Exception innerException)
10-
: base($"Exception has been thrown by the aspect of an invocation. ---> {innerException?.Message}.", 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)
1115
{
1216
AspectContext = aspectContext;
1317
}

src/AspectCore.Abstractions/DynamicProxy/IAspectExceptionWrapper.cs

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/AspectCore.Core/DependencyInjection/ServiceContext.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
using System;
1+
using AspectCore.Configuration;
2+
using AspectCore.DynamicProxy;
3+
using AspectCore.DynamicProxy.Parameters;
4+
using System;
25
using System.Collections;
36
using System.Collections.Generic;
47
using System.Linq;
5-
using AspectCore.Configuration;
6-
using AspectCore.DynamicProxy;
7-
using AspectCore.DynamicProxy.Parameters;
88

99
namespace AspectCore.DependencyInjection
1010
{
@@ -99,8 +99,6 @@ private void AddInternalServices()
9999
Scopeds.AddType<IParameterInterceptorSelector, ParameterInterceptorSelector>();
100100
if (!Contains(typeof(IAspectCachingProvider)))
101101
Singletons.AddType<IAspectCachingProvider, AspectCachingProvider>();
102-
if (!Contains(typeof(IAspectExceptionWrapper)))
103-
Transients.AddType<IAspectExceptionWrapper, AspectExceptionWrapper>();
104102
}
105103

106104
public int Count => _collection.Count;

src/AspectCore.Core/DynamicProxy/AspectActivator.cs

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
using AspectCore.Core.Utils;
1+
using AspectCore.Configuration;
2+
using AspectCore.Core.Utils;
23
using System;
4+
using System.Runtime.ExceptionServices;
35
using System.Threading.Tasks;
46

57
namespace AspectCore.DynamicProxy
@@ -9,14 +11,13 @@ internal sealed class AspectActivator : IAspectActivator
911
{
1012
private readonly IAspectContextFactory _aspectContextFactory;
1113
private readonly IAspectBuilderFactory _aspectBuilderFactory;
12-
private readonly IAspectExceptionWrapper _aspectExceptionWrapper;
14+
private readonly IAspectConfiguration _aspectConfiguration;
1315

14-
public AspectActivator(IAspectContextFactory aspectContextFactory, IAspectBuilderFactory aspectBuilderFactory,
15-
IAspectExceptionWrapper aspectExceptionWrapper)
16+
public AspectActivator(IAspectContextFactory aspectContextFactory, IAspectBuilderFactory aspectBuilderFactory, IAspectConfiguration aspectConfiguration)
1617
{
1718
_aspectContextFactory = aspectContextFactory;
1819
_aspectBuilderFactory = aspectBuilderFactory;
19-
_aspectExceptionWrapper = aspectExceptionWrapper;
20+
_aspectConfiguration = aspectConfiguration;
2021
}
2122

2223
public TResult Invoke<TResult>(AspectActivatorContext activatorContext)
@@ -28,8 +29,7 @@ public TResult Invoke<TResult>(AspectActivatorContext activatorContext)
2829
var task = aspectBuilder.Build()(context);
2930
if (task.IsFaulted)
3031
{
31-
_aspectExceptionWrapper.Wrap(context, task.Exception.InnerException);
32-
return default;
32+
ExceptionDispatchInfo.Capture(task.Exception.InnerException).Throw();
3333
}
3434
if (!task.IsCompleted)
3535
{
@@ -42,12 +42,13 @@ public TResult Invoke<TResult>(AspectActivatorContext activatorContext)
4242
}
4343
catch (Exception ex)
4444
{
45-
_aspectExceptionWrapper.Wrap(context, ex);
46-
return default;
45+
if (!_aspectConfiguration.ThrowAspectException || ex is AspectInvocationException _)
46+
throw;
47+
48+
throw new AspectInvocationException(context, ex);
4749
}
4850
finally
4951
{
50-
_aspectExceptionWrapper.ThrowIfFailed();
5152
_aspectContextFactory.ReleaseContext(context);
5253
}
5354
}
@@ -62,8 +63,7 @@ public async Task<TResult> InvokeTask<TResult>(AspectActivatorContext activatorC
6263

6364
if (invoke.IsFaulted)
6465
{
65-
_aspectExceptionWrapper.Wrap(context, invoke.Exception.InnerException);
66-
return default;
66+
ExceptionDispatchInfo.Capture(invoke.Exception.InnerException).Throw();
6767
}
6868

6969
if (!invoke.IsCompleted)
@@ -80,19 +80,18 @@ public async Task<TResult> InvokeTask<TResult>(AspectActivatorContext activatorC
8080
case Task _:
8181
return default;
8282
default:
83-
_aspectExceptionWrapper.Wrap(context, new InvalidCastException(
84-
$"Unable to cast object of type '{context.ReturnValue.GetType()}' to type '{typeof(Task<TResult>)}'."));
85-
return default;
83+
throw new AspectInvalidCastException(context, $"Unable to cast object of type '{context.ReturnValue.GetType()}' to type '{typeof(Task<TResult>)}'.");
8684
}
8785
}
8886
catch (Exception ex)
8987
{
90-
_aspectExceptionWrapper.Wrap(context, ex);
91-
return default;
88+
if (!_aspectConfiguration.ThrowAspectException || ex is AspectInvocationException _)
89+
throw;
90+
91+
throw new AspectInvocationException(context, ex);
9292
}
9393
finally
9494
{
95-
_aspectExceptionWrapper.ThrowIfFailed();
9695
_aspectContextFactory.ReleaseContext(context);
9796
}
9897
}
@@ -107,8 +106,7 @@ public async ValueTask<TResult> InvokeValueTask<TResult>(AspectActivatorContext
107106

108107
if (invoke.IsFaulted)
109108
{
110-
_aspectExceptionWrapper.Wrap(context, invoke.Exception.InnerException);
111-
return default;
109+
ExceptionDispatchInfo.Capture(invoke.Exception.InnerException).Throw();
112110
}
113111

114112
if (!invoke.IsCompleted)
@@ -125,19 +123,18 @@ public async ValueTask<TResult> InvokeValueTask<TResult>(AspectActivatorContext
125123
case ValueTask task:
126124
return default;
127125
default:
128-
_aspectExceptionWrapper.Wrap(context, new InvalidCastException(
129-
$"Unable to cast object of type '{context.ReturnValue.GetType()}' to type '{typeof(ValueTask<TResult>)}'."));
130-
return default;
126+
throw new AspectInvalidCastException(context, $"Unable to cast object of type '{context.ReturnValue.GetType()}' to type '{typeof(ValueTask<TResult>)}'.");
131127
}
132128
}
133129
catch (Exception ex)
134130
{
135-
_aspectExceptionWrapper.Wrap(context, ex);
136-
return default;
131+
if (!_aspectConfiguration.ThrowAspectException || ex is AspectInvocationException _)
132+
throw;
133+
134+
throw new AspectInvocationException(context, ex);
137135
}
138136
finally
139137
{
140-
_aspectExceptionWrapper.ThrowIfFailed();
141138
_aspectContextFactory.ReleaseContext(context);
142139
}
143140
}
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using AspectCore.Configuration;
2+
using System;
23

34
namespace AspectCore.DynamicProxy
45
{
@@ -7,18 +8,18 @@ public sealed class AspectActivatorFactory : IAspectActivatorFactory
78
{
89
private readonly IAspectContextFactory _aspectContextFactory;
910
private readonly IAspectBuilderFactory _aspectBuilderFactory;
10-
private readonly IAspectExceptionWrapper _aspectExceptionWrapper;
11+
private readonly IAspectConfiguration _aspectConfiguration;
1112

12-
public AspectActivatorFactory(IAspectContextFactory aspectContextFactory, IAspectBuilderFactory aspectBuilderFactory, IAspectExceptionWrapper aspectExceptionWrapper)
13+
public AspectActivatorFactory(IAspectContextFactory aspectContextFactory, IAspectBuilderFactory aspectBuilderFactory, IAspectConfiguration aspectConfiguration)
1314
{
1415
_aspectContextFactory = aspectContextFactory ?? throw new ArgumentNullException(nameof(aspectContextFactory));
1516
_aspectBuilderFactory = aspectBuilderFactory ?? throw new ArgumentNullException(nameof(aspectBuilderFactory));
16-
_aspectExceptionWrapper = aspectExceptionWrapper ?? throw new ArgumentNullException(nameof(aspectExceptionWrapper));
17+
_aspectConfiguration = aspectConfiguration ?? throw new ArgumentNullException(nameof(aspectConfiguration));
1718
}
1819

1920
public IAspectActivator Create()
2021
{
21-
return new AspectActivator(_aspectContextFactory, _aspectBuilderFactory, _aspectExceptionWrapper);
22+
return new AspectActivator(_aspectContextFactory, _aspectBuilderFactory, _aspectConfiguration);
2223
}
2324
}
2425
}

src/AspectCore.Core/DynamicProxy/Extensions/AspectContextRuntimeExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public static Task<object> UnwrapAsyncReturnValue(this AspectContext aspectConte
8282

8383
if (!aspectContext.IsAsync())
8484
{
85-
throw new AspectInvocationException(aspectContext, new InvalidOperationException("This operation only support asynchronous method."));
85+
throw new AspectInvalidOperationException(aspectContext, "This operation only support asynchronous method.");
8686
}
8787

8888
var returnValue = aspectContext.ReturnValue;

src/AspectCore.Extensions.Autofac/ContainerBuilderExtensions.cs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,11 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Reflection;
5-
using AspectCore.Configuration;
1+
using AspectCore.Configuration;
2+
using AspectCore.DependencyInjection;
63
using AspectCore.DynamicProxy;
74
using AspectCore.DynamicProxy.Parameters;
8-
using AspectCore.DependencyInjection;
95
using Autofac;
10-
using Autofac.Core;
11-
using Autofac.Core.Activators;
12-
using Autofac.Core.Activators.Delegate;
13-
using Autofac.Core.Activators.Reflection;
146
using Autofac.Core.Resolving.Pipeline;
15-
using AParameter = Autofac.Core.Parameter;
7+
using System;
8+
using System.Linq;
169

1710
namespace AspectCore.Extensions.Autofac
1811
{
@@ -58,7 +51,6 @@ public static ContainerBuilder RegisterDynamicProxy(this ContainerBuilder contai
5851
containerBuilder.RegisterType<AspectBuilderFactory>().As<IAspectBuilderFactory>().SingleInstance();
5952
containerBuilder.RegisterType<ProxyTypeGenerator>().As<IProxyTypeGenerator>().SingleInstance();
6053
containerBuilder.RegisterType<AspectCachingProvider>().As<IAspectCachingProvider>().SingleInstance();
61-
containerBuilder.RegisterType<AspectExceptionWrapper>().As<IAspectExceptionWrapper>().InstancePerDependency();
6254

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

0 commit comments

Comments
 (0)