Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decorators does not follow ExternallyOwned - problem with lifetime scope on decorator with IDisposable implementation #1402

Open
fr4gles opened this issue Nov 9, 2023 · 0 comments

Comments

@fr4gles
Copy link

fr4gles commented Nov 9, 2023

Describe the Bug

According to documentation: You cannot specify a lifetime scope on a decorator. The lifetime of a decorator is tied to the lifetime of the thing it decorates. but it is not working when thing it decorates implements IDisposable

Steps to Reproduce

using Autofac;

Console.WriteLine("Hello, World!");

var builder = new ContainerBuilder();

builder.RegisterType<Parser>().As<IParser>().ExternallyOwned().InstancePerLifetimeScope();
builder.RegisterDecorator<ParserDecorator, IParser>(); // according to docs cant be marked as ExternallyOwned but has same lifetime as IParser

using var container = builder.Build(); // <== using --> Dispose() is called on ParserDecorator decorator even if IParser is ExternallyOwned
using var parser = container.Resolve<IParser>(); // <== using --> Dispose() is called on IParser
parser.Parse();

// Output (please notice Dispose() called twice):
/* 
Hello, World!
Before Parse
Parsing...
Parsed
After Parse
Disposed ParserDecorator
Disposed Parser
Disposed ParserDecorator
Disposed Parser
*/

public interface IParser : IDisposable
{
    void Parse();
}

public class Parser : IParser
{
    public void Parse()
    {
        Console.WriteLine("Parsing...");
        Console.WriteLine("Parsed");
    }

    public void Dispose()
    {
        Console.WriteLine("Disposed Parser");
    }
}

public class ParserDecorator : IParser
{
    private readonly IParser _parser;

    public ParserDecorator(IParser parser)
    {
        _parser = parser;
    }

    public void Parse()
    {
        Console.WriteLine("Before Parse");
        _parser.Parse();
        Console.WriteLine("After Parse");
    }

    public void Dispose()
    {
        Console.WriteLine("Disposed ParserDecorator");
        _parser.Dispose();
    }
}

Expected Behavior

Decorators follows "externally owned" state on "thing" it decorates.

Dependency Versions

Autofac: 7.1.0

Tags

autofac decorator externally owned idisposable

@fr4gles fr4gles changed the title Decorators does not follow ExternalllyOwned - problem with lifetime scope on decorator with IDisposable implementation Decorators does not follow ExternallyOwned - problem with lifetime scope on decorator with IDisposable implementation Jan 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant