the*_*Dmi 1 c# castle-dynamicproxy task
我不知何故在兜圈子......我尝试使用 Castle Dynamic Proxy 创建一个带有目标的接口代理。代理应该
InterceptedException如果调用抛出一个 则抛出 new InvalidOperationException。e如果调用抛出另一个异常,则抛出e。换句话说,拦截器应该捕获并转换特定的异常类型,而不是在所有其他情况下拦截。
我让这个适用于同步方法。但是,我需要返回任务的异步方法具有相同的行为。
我尝试向返回的任务添加延续并检查IsFaulted(Exception类似于此答案。这适用于 return 的方法Task,但不适用于返回的方法,Task<T>因为我的延续是类型Task(并且我不知道T拦截器中有什么) 。
public class ConvertNotFoundInterceptorTest
{
[Fact]
public void Non_throwing_func_returns_a_result()
{
Assert.Equal(43, RunTest(i => i + 1));
}
[Fact]
public void InvalidOperationExceptions_are_converted_to_IndexOutOfRangeExceptions()
{
var exception = Assert.Throws<AggregateException>(() => RunTest(i => { throw new InvalidOperationException("ugh"); }));
Assert.True(exception.InnerException is IndexOutOfRangeException);
}
[Fact]
public void Other_exceptions_are_preserved()
{
var exception = Assert.Throws<AggregateException>(() => RunTest(i => { throw new ArgumentException("ugh"); }));
Assert.True(exception.InnerException is ArgumentException);
}
private static int RunTest(Func<int, int> func)
{
var generator = new ProxyGenerator();
var proxiedSubject = generator.CreateInterfaceProxyWithTarget<ISubject>(new Subject(func), new ConvertNotFoundInterceptor());
return proxiedSubject.DoAsync(42).Result;
}
public interface ISubject
{
Task<int> DoAsync(int input);
}
public class Subject : ISubject
{
private readonly Func<int, int> _func;
public Subject(Func<int, int> func)
{
_func = func;
}
public async Task<int> DoAsync(int input)
{
return await Task.Run(() => _func(input));
}
}
}
Run Code Online (Sandbox Code Playgroud)
public class ConvertNotFoundInterceptor : IInterceptor
{
public void Intercept(IInvocation invocation)
{
invocation.Proceed();
var task = invocation.ReturnValue as Task;
if (task != null)
{
var continuation = task.ContinueWith(
t =>
{
if (t.Exception != null && t.Exception.InnerException is InvalidOperationException)
{
throw new IndexOutOfRangeException();
}
}, TaskContinuationOptions.OnlyOnFaulted);
// The following line fails (InvalidCastException: Unable to cast object
// of type 'System.Threading.Tasks.ContinuationTaskFromTask'
// to type 'System.Threading.Tasks.Task`1[System.Int32]'.)
invocation.ReturnValue = continuation;
}
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,此处所示的实现不考虑同步情况。我故意遗漏了那部分。
将上述拦截逻辑添加到异步方法中的正确方法是什么?
好的,这不起作用,Task<dynamic>因为 Castle 动态代理要求ReturnValue是完全匹配的类型。但是,您可以通过使用dynamicfor 调度来相当优雅地完成此任务:
public class ConvertNotFoundInterceptor : IInterceptor
{
public void Intercept(IInvocation invocation)
{
invocation.Proceed();
var task = invocation.ReturnValue as Task;
if (task != null)
invocation.ReturnValue = ConvertNotFoundAsync((dynamic)task);
}
private static async Task ConvertNotFoundAsync(Task source)
{
try
{
await source.ConfigureAwait(false);
}
catch (InvalidOperationException)
{
throw new IndexOutOfRangeException();
}
}
private static async Task<T> ConvertNotFoundAsync<T>(Task<T> source)
{
try
{
return await source.ConfigureAwait(false);
}
catch (InvalidOperationException)
{
throw new IndexOutOfRangeException();
}
}
}
Run Code Online (Sandbox Code Playgroud)
我非常喜欢async/await语法,因为它们可以正确处理使用ContinueWith.