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

Decorator does not work if class implements multiple interfaces #1051

Closed
dsempi opened this issue Nov 22, 2019 · 1 comment
Closed

Decorator does not work if class implements multiple interfaces #1051

dsempi opened this issue Nov 22, 2019 · 1 comment

Comments

@dsempi
Copy link

dsempi commented Nov 22, 2019

If there is a class that implements multiple interfaces and you want to decorate one of them, resolution of the other interfaces ends up with an InvalidCastException. If there are decorators for all implemented interfaces, resolution of any interface ends up in StackOverflowException.

See the code to reproduce the issue (using Autofac 4.9.4 and .Net Framework 4.8):

using System;

namespace Autofac
{
    interface I1
    {
        void Method1();
    }

    interface I2
    {
        void Method2();
    }

    class C : I1, I2
    {
        public void Method1()
        {
            Console.WriteLine("C.Method1");
        }

        public void Method2()
        {
            Console.WriteLine("C.Method2");
        }
    }

    class D1 : I1
    {
        private readonly I1 _inner;

        public D1(I1 inner)
        {
            _inner = inner;
        }

        public void Method1()
        {
            Console.WriteLine("D1.Method1");
            _inner.Method1();
        }
    }

    class D2 : I2
    {
        private readonly I2 _inner;

        public D2(I2 inner)
        {
            _inner = inner;
        }

        public void Method2()
        {
            Console.WriteLine("D2.Method2");
            _inner.Method2();
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var builder = new ContainerBuilder();
            builder.RegisterType<C>().AsImplementedInterfaces();

            builder.RegisterDecorator<D1, I1>();
            //builder.RegisterDecorator<D2, I2>(); if out-commented, container.Resolve<I1>() ends up in StackOverflowException

            var container = builder.Build();
            container.Resolve<I1>().Method1();
            container.Resolve<I2>().Method2(); // ends up in InvalidCastException
        }
    }
}
@tillig
Copy link
Member

tillig commented Nov 22, 2019

Duplicate #963

@tillig tillig closed this as completed Nov 22, 2019
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

2 participants