zcz527 / autofac

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

Common Language Runtime detected an invalid program when using reflection registration + webforms integration #473

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1.Create a new Web Application project in Visual Studio 2012
2.NuGet Autofac 3.1.1 and Autofac.Web 3.0.0
3.Register components. In my case, the registration looks something like this:

builder.Register(context => new 
SessionFactoryProvider().GetSessionFactory()).As<ISessionFactory>().SingleInstan
ce();
builder.Register(context => new 
FoobarPersister(context.Resolve<ISessionFactory>()))
                   .As<ISimplePersister<Foobar>>();
//Will explode the CLR!!!
//builder.RegisterType<FoobarPersister>().As<ISimplePersister<Foobar>>();

The FoobarPersister has a single constructor with this signature:
public FoobarPersister(ISessionFactory sessionFactory)

Then use AttributedInjection in Autofac.Web to inject a control's properties.

What is the expected output? What do you see instead?
The control should have its component injected properly. Instead, we get this 
beauty:
[InvalidProgramException: Common Language Runtime detected an invalid program.]
   Autofac.Core.Activators.Reflection.ConstructorParameterBinding..ctor(ConstructorInfo ci, IEnumerable`1 availableParameters, IComponentContext context) +0
   Autofac.Core.Activators.Reflection.<>c__DisplayClass5.<GetConstructorBindings>b__4(ConstructorInfo ci) +258
   System.Linq.WhereSelectArrayIterator`2.MoveNext() +329
   System.Linq.WhereEnumerableIterator`1.MoveNext() +510
   System.Linq.Buffer`1..ctor(IEnumerable`1 source) +932
   System.Linq.Enumerable.ToArray(IEnumerable`1 source) +451
   Autofac.Core.Activators.Reflection.ReflectionActivator.ActivateInstance(IComponentContext context, IEnumerable`1 parameters) +691
   Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable`1 parameters) +287
   Autofac.Core.Resolving.InstanceLookup.Execute() +297
   Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, IComponentRegistration registration, IEnumerable`1 parameters) +540
   Autofac.Core.Resolving.ResolveOperation.ResolveComponent(IComponentRegistration registration, IEnumerable`1 parameters) +287
   Autofac.Core.Resolving.ResolveOperation.Execute(IComponentRegistration registration, IEnumerable`1 parameters) +293

[DependencyResolutionException: An exception was thrown while executing a 
resolve operation. See the InnerException for details. ---> Common Language 
Runtime detected an invalid program. (See inner exception for details.)]
   Autofac.Core.Resolving.ResolveOperation.Execute(IComponentRegistration registration, IEnumerable`1 parameters) +397
   Autofac.Core.Lifetime.LifetimeScope.ResolveComponent(IComponentRegistration registration, IEnumerable`1 parameters) +498
   Autofac.ResolutionExtensions.TryResolveService(IComponentContext context, Service service, IEnumerable`1 parameters, Object& instance) +560
   Autofac.ResolutionExtensions.ResolveService(IComponentContext context, Service service, IEnumerable`1 parameters) +500
   Autofac.ResolutionExtensions.Resolve(IComponentContext context, Type serviceType, IEnumerable`1 parameters) +330
   Autofac.ResolutionExtensions.Resolve(IComponentContext context, Type serviceType) +271
   Autofac.Core.Activators.Reflection.AutowiringPropertyInjector.InjectProperties(IComponentContext context, Object instance, Boolean overrideSetValues) +1094
   Autofac.ResolutionExtensions.InjectProperties(IComponentContext context, TService instance) +479
   Autofac.Integration.Web.Forms.PageInjectionBehavior.InjectUserControls(Func`2 injector, Control parent) +541
   Autofac.Integration.Web.Forms.PageInjectionBehavior.InjectUserControls(Func`2 injector, Control parent) +557
   Autofac.Integration.Web.Forms.<>c__DisplayClass1.<DoInjection>b__0(Object s, EventArgs e) +276
   System.EventHandler.Invoke(Object sender, EventArgs e) +0
   System.Web.UI.Page.OnPreLoad(EventArgs e) +344
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3765

Note that if I use the expression registration instead of the reflection one, 
everything works as expected.

What version of Autofac are you using? On what version of .NET/Silverlight?
Autofac 3.1.1, Autofac.Web 3.0.0, .NET 4.5

Original issue reported on code.google.com by guillaum...@gmail.com on 25 Nov 2013 at 9:50

GoogleCodeExporter commented 8 years ago
I should also note that the following unit test works fine using either 
reflection or expression registration. This to me indicates a problem with the 
Asp.Net part.
        [Test]
        public void BuilderRegistersFoobarPersister()
        {
            var builder = new ContainerBuilder();
            builder.Register(context => new SessionFactoryProvider().GetSessionFactory()).As<ISessionFactory>().SingleInstance();
            //Either way will work in a unit test...
            //builder.Register(context => new FoobarPersister(context.Resolve<ISessionFactory>())).As<ISimplePersister<Foobar>>();
            builder.RegisterType<FoobarPersister>().As<ISimplePersister<Foobar>>();
            var container = builder.Build();
            var persister = container.Resolve<ISimplePersister<Foobar>>();
            Assert.IsInstanceOf<FoobarPersister>(persister, "FoobarPersister was not properly registered as ISimplePersister<Foobar>");
        }

Original comment by guillaum...@gmail.com on 25 Nov 2013 at 9:57

GoogleCodeExporter commented 8 years ago

Original comment by travis.illig on 2 Dec 2013 at 11:46

GoogleCodeExporter commented 8 years ago
Moved issue to GitHub: https://github.com/autofac/Autofac/issues/473

Subsequent issue management will be held there; closing the issue on Google 
Code as "WontFix" because we will handle issue resolution on GitHub.

Original comment by travis.illig on 11 Feb 2014 at 12:02