gpt4 book ai didi

c# - 如何使用 Ninject InRequestScope 处理异步调用?

转载 作者:太空狗 更新时间:2023-10-29 21:22:14 25 4
gpt4 key购买 nike

我们在 ASP.NET Web Api 应用程序中使用 Ninject,我们将 DbContextInRequestScope 绑定(bind)>。这适用于我们的大多数请求,因为它们同步完成所有工作,因此可以在请求完成后安全地处理上下文。

但是,我们根据请求执行异步 Web 服务调用,该调用具有作为回调传递的延续方法,并且该回调方法需要使用数据库上下文。但是我们的请求不应该等待异步服务调用完成,而是立即返回(这是一个明确的要求)。

这是该情况的一个简化示例。

public class MyController : ApiController
{
private readonly MyDbContext dbContext;
private readonly SomeWebService service;

public MyController(MyDbContext dbContext, SomeWebService service)
{
this.dbContext = dbContext;
this.service = service;
}

public IHttpActionResult MyActionWithAsyncCall()
{
// Doing stuff.

// Calling webservice method, passing the Callback as the continuation.
service.MethodWithCallback(param1, param2, this.Callback);

// Returning without waiting for the service call to be completed.
return Ok();
}

private void Callback()
{
// Trying to use the DbContext:
var person = dbContext.People.First();
// The above line sometimes throws exception, because the context has been disposed.
}
}

Ninject 应该如何处理这种情况?有没有办法显式地“延长”绑定(bind) DbContext 实例的生命周期?还是 Callback 方法应该创建全新的 DbContext?如果应该,它应该使用什么范围?

最佳答案

无法通过 .InRequestScope() 显式延长对象的生命周期。扩展到请求结束后。

如果没有业务要求请求和@回调期间的工作必须在单个事务中发生,我会使用两个 DbContext实例。一个在请求期间,一个在回调期间。注意:据我所知,这也意味着您不能从第一个上下文中获取实体并在第二个上下文中更新/保存它。这意味着您只能将标识符(以及与操作相关的其他数据)从请求传递到回调。回调必须“创建”一个新的 DbContext 并从上下文中检索相应的实体。

条件绑定(bind)选择

作为替代方案,您可以为这种特殊情况声明一个特殊绑定(bind)。 Ninject 支持所谓的 contextual bindings .这意味着您将有两个绑定(bind),标准绑定(bind)和上下文、特殊情况绑定(bind):

Bind<DbContext>().ToSelf().InRequestScope();

Bind<DbContext>().ToSelf()
.WhenInjectedInto<SomeController>();

请注意,第二个绑定(bind)未指定范围 - 这意味着 SomeController负责电话.Dispose() .在您的情况下,这意味着回调必须处理上下文。您还需要在所有错误情况下处理上下文(回调代码中的错误,触发回调之前发生的错误,......)。

此外,实际上您的应用程序可能更复杂并且 .WhenInjectedInto<SomeController>是不够的/不正确的,因为您可能想将相同的实例注入(inject) Controller 加上存储库和查询对象..什么不是。

这意味着您将需要范围界定,但范围不同于 .InRequestScope() .您可以使用 .InCallScope()或命名范围 - 两者都包含在 named scope extension 中.

此外,您需要调整 When健康)状况。您可以调整它以遍历请求并查看是否有 FooController请求链中的任何位置。但这不是很高效,相反我建议使用 ninject IParameter指定您想要特殊情况处理。参数将是:

public class NonRequestScopedParameter : Ninject.Parameters.IParameter
{
public bool Equals(IParameter other)
{
if (other == null)
{
return false;
}

return other is NonRequestScopedParameter;
}

public object GetValue(IContext context, ITarget target)
{
throw new NotSupportedException("this parameter does not provide a value");
}

public string Name
{
get { return typeof(NonRequestScopedParameter).Name; }
}

// this is very important
public bool ShouldInherit
{
get { return true; }
}
}

这将应用于像这样的绑定(bind):

kernel.Bind<SomeController>().ToSelf()
.WithParameter(new NonRequestScopedParameter());

kernel.Bind<DbContext>().ToSelf()
.When(x => x.Parameters.OfType<NonRequestScopedParameter>().Any())
.InCallScope(); // or whatever scope you're using

关于c# - 如何使用 Ninject InRequestScope 处理异步调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26775080/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com