OpenRasta DI PerRequest终身问题

时间:2011-03-02 05:06:57

标签: openrasta

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

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

这适用于第一个请求;记录消息后,第二个请求抛出OpenRasta.DI.DependencyResolutionException:

  

忽略构造函数,以下依赖项没有注册:IRepository

DependencyLifetime.Singleton和DependencyLifetime.Transient工作正常,只是PerRequest似乎有问题。我在卡西尼跑。我做错了吗?

2 个答案:

答案 0 :(得分:5)

解决此问题的方法:

实施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>();

答案 1 :(得分:0)

修正了2.2版本,很快就会进入nuget。