You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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!");varbuilder=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 IParserusingvarcontainer= builder.Build();// <== using --> Dispose() is called on ParserDecorator decorator even if IParser is ExternallyOwnedusingvarparser= container.Resolve<IParser>();// <== using --> Dispose() is called on IParser
parser.Parse();// Output (please notice Dispose() called twice):/* Hello, World!Before ParseParsing...ParsedAfter ParseDisposed ParserDecoratorDisposed ParserDisposed ParserDecoratorDisposed Parser*/publicinterfaceIParser:IDisposable{voidParse();}publicclassParser:IParser{publicvoidParse(){
Console.WriteLine("Parsing...");
Console.WriteLine("Parsed");}publicvoidDispose(){
Console.WriteLine("Disposed Parser");}}publicclassParserDecorator:IParser{privatereadonlyIParser_parser;publicParserDecorator(IParserparser){_parser=parser;}publicvoidParse(){
Console.WriteLine("Before Parse");
_parser.Parse();
Console.WriteLine("After Parse");}publicvoidDispose(){
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
The text was updated successfully, but these errors were encountered:
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
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 implementsIDisposable
Steps to Reproduce
Expected Behavior
Decorators follows "externally owned" state on "thing" it decorates.
Dependency Versions
Autofac: 7.1.0
Tags
autofac decorator externally owned idisposable
The text was updated successfully, but these errors were encountered: