У меня есть 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
, и вот полная трассировка стека:
at 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 (состояние объекта)
в System.Threading.ExecutionContext.RunInternal (ExecutionContext ExecutionContext, обратный вызов ContextCallback, состояние объекта, логическое значение saveSyncCtx)
в System.Threading.ExecutionContext.Run (ExecutionContext executeContext, обратный вызов ContextCallback, состояние объекта, логическое значение saveSyncCtx)
в System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
в System.Threading.ThreadPoolWorkQueue.Dispatch()
в System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()
ОБНОВЛЕНИЕ 2:
С атрибутом SecurityCritical
или без него проблема существует.
[SecurityCritical]
? Без них проблема не исчезнет? - person Steven   schedule 24.11.2012SecurityCritical
. С ними или без них, разницы никакой. - person amiry jd   schedule 24.11.2012BeginLifetimeScope
нет атрибутаSecurityCritical
. Итак, почему эта ошибка возникает? Есть идеи? - person amiry jd   schedule 24.11.2012