openrasta

OpenRasta DI PerRequest lifetime problem


I'm using OpenRasta 2.0.3214.437 in an ASP.NET 4 web application. I'm registering a custom dependency in the internal container using:

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

This works perfectly for the first request; the second request throws an OpenRasta.DI.DependencyResolutionException after logging the message:

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

DependencyLifetime.Singleton and DependencyLifetime.Transient work fine, just the PerRequest seems to have the issue. I'm running in Cassini. Am I doing something wrong?


Solution

  • Workaround to this issue:

    Implement an 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;
        }
    
    }
    

    Then register the contributor in your IConfigurationSource:

    ResourceSpace.Uses.PipelineContributor<RepositoryPipelineContributor>();