gpt4 book ai didi

OpenRasta DI PerRequest 生命周期问题

转载 作者:行者123 更新时间:2023-12-04 15:15:23 26 4
gpt4 key购买 nike

我在 ASP.NET 4 Web 应用程序中使用 OpenRasta 2.0.3214.437。我正在使用以下方法在内部容器中注册自定义依赖项:

ResourceSpace.Uses.CustomDependency<IRepository, Repository>(DependencyLifetime.PerRequest);

这对于第一个请求非常有效;第二个请求在记录消息后抛出 OpenRasta.DI.DependencyResolutionException:

Ignoring constructor, following dependencies didn't have a registration: IRepository



DependencyLifetime.Singleton 和 DependencyLifetime.Transient 工作正常,只是 PerRequest 似乎有问题。我在卡西尼号上运行。难道我做错了什么?

最佳答案

此问题的解决方法:

实现一个 IPipelineContributor:

public class RepositoryPipelineContributor : IPipelineContributor
{
private readonly IDependencyResolver resolver;

public RepositoryPipelineContributor(IDependencyResolver resolver)
{
this.resolver = resolver;
}

public void Initialize(IPipeline pipelineRunner)
{
pipelineRunner.Notify(CreateRepository)
.Before<KnownStages.IOperationExecution>();
}

private PipelineContinuation CreateRepository(ICommunicationContext arg)
{
resolver.AddDependencyInstance<IRepository>(new Repository(), DependencyLifetime.PerRequest);
return PipelineContinuation.Continue;
}

}

然后在您的 IConfigurationSource 中注册贡献者:
ResourceSpace.Uses.PipelineContributor<RepositoryPipelineContributor>();

关于OpenRasta DI PerRequest 生命周期问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5163606/

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