Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenRasta DI PerRequest lifetime problem

Tags:

openrasta

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?

like image 252
Sam Avatar asked Mar 02 '11 05:03

Sam


1 Answers

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>();
like image 108
Sam Avatar answered Oct 13 '22 19:10

Sam