异步任务和简单注入器生命周期范围
Async tasks and Simple Injector Lifetime scopes
我有以下方法可以解决 IConsumeAsync
其中 returns 的任务。
private Task ConsumeMessageAsync<TMessage, TConsumer>(TMessage message)
where TMessage : class
where TConsumer : IConsumeAsync<TMessage>
{
var tsc = new TaskCompletionSource<object>();
var instance = (IConsumeAsync<TMessage>)container
.GetInstance(typeof(TConsumer));
instance
.Consume(message)
.ContinueWith(task =>
{
if (task.IsFaulted && task.Exception != null)
{
tsc.SetException(task.Exception);
}
else
{
tsc.SetResult(null);
}
});
return tsc.Task;
}
我需要用生命周期范围 ala 包装它
using(var scope = container.BeginLifetimeScope()){
}
我尝试了以下方法,但 100% 无效
var scope = container.BeginLifetimeScope();
var wrapper = ConsumeMessageAsync<TMessage, TConsumer>(message);
wrapper.ContinueWith(x => scope.Dispose());
wrapper.Start();
我收到以下错误:
Additional information: It is not safe to use a LifetimeScope instance
across threads. Make sure the complete operation that the lifetime
scope surrounds gets executed within the same thread and make sure
that the LifetimeScope instance gets disposed on the same thread as it
gets created. Dispose was called on thread with ManagedThreadId 27,
but was created on thread with id 21.
我不太确定如何使用 using 语句在 .NET 中包装异步任务以尝试使用额外的包装器任务来控制流程的手动类型。
您需要改用 AsyncScopedLifestyle。 ThreadScopedLifestyle
(以前称为 LifetimeScopeLifestyle
)创建一个特定于线程的作用域,而 AsyncScopedLifestyle
创建一个与异步方法的逻辑控制流一起流动的作用域。
// using SimpleInjector.Lifestyles;
using (AsyncScopedLifestyle.BeginScope(container)) {
var service = container.GetInstance<ISomeService>();
// ...
}
我有以下方法可以解决 IConsumeAsync
其中 returns 的任务。
private Task ConsumeMessageAsync<TMessage, TConsumer>(TMessage message)
where TMessage : class
where TConsumer : IConsumeAsync<TMessage>
{
var tsc = new TaskCompletionSource<object>();
var instance = (IConsumeAsync<TMessage>)container
.GetInstance(typeof(TConsumer));
instance
.Consume(message)
.ContinueWith(task =>
{
if (task.IsFaulted && task.Exception != null)
{
tsc.SetException(task.Exception);
}
else
{
tsc.SetResult(null);
}
});
return tsc.Task;
}
我需要用生命周期范围 ala 包装它
using(var scope = container.BeginLifetimeScope()){
}
我尝试了以下方法,但 100% 无效
var scope = container.BeginLifetimeScope();
var wrapper = ConsumeMessageAsync<TMessage, TConsumer>(message);
wrapper.ContinueWith(x => scope.Dispose());
wrapper.Start();
我收到以下错误:
Additional information: It is not safe to use a LifetimeScope instance across threads. Make sure the complete operation that the lifetime scope surrounds gets executed within the same thread and make sure that the LifetimeScope instance gets disposed on the same thread as it gets created. Dispose was called on thread with ManagedThreadId 27, but was created on thread with id 21.
我不太确定如何使用 using 语句在 .NET 中包装异步任务以尝试使用额外的包装器任务来控制流程的手动类型。
您需要改用 AsyncScopedLifestyle。 ThreadScopedLifestyle
(以前称为 LifetimeScopeLifestyle
)创建一个特定于线程的作用域,而 AsyncScopedLifestyle
创建一个与异步方法的逻辑控制流一起流动的作用域。
// using SimpleInjector.Lifestyles;
using (AsyncScopedLifestyle.BeginScope(container)) {
var service = container.GetInstance<ISomeService>();
// ...
}