私は以下に示すようにAsyncCommandHandlerDecorator<TCommand>
とを持っています:LifetimeScopedCommandHandlerDecorator<TCommand>
public class AsyncCommandHandlerDecorator<TCommand>
: ICommandHandler<TCommand> where TCommand : ICommand {
private readonly Func<ICommandHandler<TCommand>> _factory;
public AsyncCommandHandlerDecorator(Func<ICommandHandler<TCommand>> factory) {
_factory = factory;
}
[SecurityCritical]
// with or whitout SecurityCritical attribute, problem exists.
public void Handle(TCommand command) {
ThreadPool.QueueUserWorkItem(_ => {
var handler = _factory();
handler.Handle(command);
});
}
}
// AND
public class LifetimeScopedCommandHandlerDecorator<TCommand>
: ICommandHandler<TCommand> where TCommand : ICommand {
private readonly Container _container;
private readonly Func<ICommandHandler<TCommand>> _factory;
public LifetimeScopedCommandHandlerDecorator(Container container,
Func<ICommandHandler<TCommand>> factory) {
_container = container;
_factory = factory;
}
[SecurityCritical]
// with or whitout SecurityCritical attribute, problem exists.
public void Handle(TCommand command) {
using (_container.BeginLifetimeScope()) {
var handler = _factory();
handler.Handle(command);
}
}
}
そして私はこのようなデコレータを登録します:
container.RegisterSingleDecorator(
typeof(ICommandHandler<>),
typeof(LifetimeScopedCommandHandlerDecorator<>),
c => c.ImplementationType.Name.StartsWith("Async"));
container.RegisterSingleDecorator(
typeof(ICommandHandler<>),
typeof(AsyncCommandHandlerDecorator<>),
c => c.ImplementationType.Name.StartsWith("Async"));
しかし、非同期プロセスが呼び出そうとすると、次のエラーが発生します。
セキュリティ透過メソッド「LifetimeScopedCommandHandlerDecorator`1.Handle(!0)」によるセキュリティクリティカルメソッド「SimpleInjector.SimpleInjectorLifetimeScopeExtensions.BeginLifetimeScope(SimpleInjector.Container)」へのアクセスに失敗しました。
との両方でSecurityCritical
属性を使用しようとしましたが、役に立ちませんでした。何かアイデアはありますか?LifetimeScopedCommandHandlerDecorator.Handle
AsyncCommandHandlerDecorator.Handle
アップデート:
例外はタイプMethodAccessException
であり、完全なスタックトレースは次のとおりです。
MYNAMESPACE.LifetimeScopedCommandHandlerDecorator`1.Handle(TCommandコマンド)で
MYNAMESPACE.AsyncCommandHandlerDecorator`1。<>c_DisplayClass1.b _0(Object _) で
in C:\MYPROJECTPATH\AsyncCommandHandlerDecorator.cs:line 23
System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object state)で
System.Threading.ExecutionContext.RunInternal(ExecutionContext executeContext、ContextCallbackコールバック、オブジェクト状態、ブール値preserveSyncCtx)で
System.Threading.ExecutionContext.Run(ExecutionContext executeContext、ContextCallbackコールバック、オブジェクト状態、ブール値preserveSyncCtx)で
System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()で
System.Threading.ThreadPoolWorkQueue.Dispatch()で
System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()で
更新2:
属性の有無にかかわらずSecurityCritical
、問題が存在します。