OpenRasta DI PerRequest lifetime problem

576 views Asked by At

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?

2

There are 2 answers

1
Sam On BEST ANSWER

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>();
0
SerialSeb On

Fixed in the 2.2 release, coming to a nuget soon.