基本的には、以下のようなCastle.DynamicProxyを傍受に使用しているクラスがあります。Castle.DynamicProxyを使用して非同期のInteceptorを作成することはできますか?
using System;
using System.Collections.Concurrent;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Castle.DynamicProxy;
namespace SaaS.Core.IoC
{
public abstract class AsyncInterceptor : IInterceptor
{
private readonly ILog _logger;
private readonly ConcurrentDictionary<Type, Func<Task, IInvocation, Task>> wrapperCreators =
new ConcurrentDictionary<Type, Func<Task, IInvocation, Task>>();
protected AsyncInterceptor(ILog logger)
{
_logger = logger;
}
void IInterceptor.Intercept(IInvocation invocation)
{
if (!typeof(Task).IsAssignableFrom(invocation.Method.ReturnType))
{
InterceptSync(invocation);
return;
}
try
{
CheckCurrentSyncronizationContext();
var method = invocation.Method;
if ((method != null) && typeof(Task).IsAssignableFrom(method.ReturnType))
{
var taskWrapper = GetWrapperCreator(method.ReturnType);
Task.Factory.StartNew(
async() => { await InterceptAsync(invocation, taskWrapper).ConfigureAwait(true); }
, // this will use current synchronization context
CancellationToken.None,
TaskCreationOptions.AttachedToParent,
TaskScheduler.FromCurrentSynchronizationContext()).Wait();
}
}
catch (Exception ex)
{
//this is not really burring the exception
//excepiton is going back in the invocation.ReturnValue which
//is a Task that failed. with the same excpetion
//as ex.
}
}
....
は、最初は、このコードはでした:
Task.Run(async() => { await InterceptAsync(invocation, taskWrapper)).Wait()
しかし、我々はこれまで任意の呼び出しの後のHttpContextを失ったので、私たちはそれを切り替える必要があった。だから我々は渡すことができ
Task.Factory.StartNew
TaskScheduler.FromCurrentSynchronizationContext()
私たちは本当にちょうどいいからあるスレッドを別のスレッドにウェーティングする。私は本当に
async Task IInterceptor.Intercept(IInvocation invocation)
に
void IInterceptor.Intercept(IInvocation invocation)
の署名を変更し、Task.RunまたはTask.Factoryを取り除くだけ作るのが大好きだこと:
await InterceptAsync(invocation, taskWrapper);
問題Castle.DynamicProxy IInterecptorはこれを許可しません。私は本当に迎撃を待っています。私はすることができます。結果は、私は呼び出している非同期呼び出しのポイントは何ですか?待つことができなくても、私はこのスレッドの実行をもたらすことができるという利点を失います。私は彼らのDynamicProxyのためにCastle Windsorについていませんので、私はこれを行う別の方法を探しています。私たちはUnityを検討しましたが、AutoFacの実装全体を置き換えるつもりはありません。
ご協力いただければ幸いです。
リンクをありがとうございます。私はそれをチェックして、あなたの例をあげます。私はこれに対する答えを数日間探してきました。 –