dongfo / autofac

Automatically exported from code.google.com/p/autofac
0 stars 0 forks source link

AsImplementedInterfaces should not be available on delegate registrations #338

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
I'm not sure if the issue lies with Autofac or Moq.

What steps will reproduce the problem? I've attached some unit tests that 
demonstrate the issue.

What is the expected output? What do you see instead? I expect both tests to 
pass. Instead, the second throws a ComponentNotRegisteredException.

What version of Autofac are you using? On what version of .NET/Silverlight? 
.NET 4.0

<packages>
  <package id="Autofac" version="2.5.1.827" />
  <package id="Moq" version="4.0.10827" />
</packages>

Please provide any additional information below.

Original issue reported on code.google.com by neonta...@gmail.com on 20 Jul 2011 at 4:49

Attachments:

GoogleCodeExporter commented 8 years ago
I worked around the issue with the following method:

        /// <summary>
        /// Returns a component context with the mock as its only registration.
        /// </summary>
        /// <typeparam name="T">The type of item mocked by the mock.</typeparam>
        /// <param name="mock">The mock.</param>
        /// <returns>A ComponentContext suitable for use as an Autofac container.</returns>
        public static IComponentContext AsComponentContext<T>(this Mock<T> mock) where T : class
        {
            var builder = new ContainerBuilder();
            foreach (var _interface in mock.Object.GetType().GetInterfaces())
            {
                if (_interface.Namespace == null 
                    || _interface.Namespace.Contains("Moq") 
                    || _interface.Namespace.Contains("Castle.DynamicProxy")) 
                    continue;

                if (_interface.Name == "ISerializable")
                    continue;

                builder.Register(x => mock.Object).As(_interface);
            }
            return builder.Build();
        }

Original comment by neonta...@gmail.com on 20 Jul 2011 at 4:52

GoogleCodeExporter commented 8 years ago
The AsImplementedInterfaces() method relies on the implementation type of the 
component to determine the available interfaces. This works fine for reflective 
(RegisterType()-style) components, but in the case of delegates as you've 
pointed out, Autofac does not know the implementation type in advance so can't 
make the registration correctly. I think the best thing to do here is to make 
AsImplementedInterfaces() available only for components with 
ReflectionActivatorData. I'll look into making this change in the next release.

Thanks for the information and follow-up!

Nick

Original comment by nicholas...@gmail.com on 25 Jul 2011 at 10:28

GoogleCodeExporter commented 8 years ago
Thank you for letting me know!

Original comment by neonta...@gmail.com on 3 Aug 2011 at 4:40

GoogleCodeExporter commented 8 years ago
I've looked into this - disabling these methods for delegates involve quite a 
refactor so I won't take any action on it now. If others hit the same issue I 
will revisit it.

Thanks again,

Nick

Original comment by nicholas...@gmail.com on 12 Aug 2011 at 3:19