2

I register services with the following code (used Autofac MVC integration package):

// IoC
var builder = new ContainerBuilder();

builder.RegisterControllers(Assembly.GetExecutingAssembly()).PropertiesAutowired();
builder.RegisterModelBinders(Assembly.GetExecutingAssembly()).PropertiesAutowired();
builder.RegisterModelBinderProvider();
builder.RegisterFilterProvider();

builder.RegisterAssemblyTypes(myAssembly)
      .AssignableTo(typeof(IMyInterface))
      .AsImplementedInterfaces()
      .InstancePerLifetimeScope();    

var container = builder.Build();

DependencyResolver.SetResolver(new AutofacDependencyResolver(container));

Now I want to inject a collection of IMyInterface implementations:

public MyClass(Lazy<IEnumerable<IMyInterface>>> myInterfaces)
{
}

The problem is that myInterfaces.Value contains two different instances of the same implementation. I mean:

public MyServ: IMyInterface 
{
}

There are two instances of MyServ in the myInterfaces.Value collection.

I have version 4 of Autofac, downloaded in May 2014. Everything worked fine until the last AutoFac update (via nuget). Has anything changed in Autofac so my snippet does not work? What I am doing wrong?

Kalle
  • 2,282
  • 1
  • 24
  • 30
  • Are two different versions of your assemblies present? Maybe one in the GAC? – Brannon Oct 14 '14 at 13:36
  • Assembly.GetExecutingAssembly() returns one assembly. How it could be possible that two version of the same assembly would be scanned? Any way I don't think it could be a problem. I've cleand any temporary files, event chceked project on different machine - the same problem. –  Oct 14 '14 at 13:38
  • Have you tried if the same thing happens manually registering the implementations of `IMyInterface` as in `builder.RegisterType().As();`? – Daniel J.G. Oct 14 '14 at 13:57
  • I'll check that, I need still to register it a way I've presented. –  Oct 14 '14 at 18:02
  • @DanielJ.G. When I register implementation explicity, everything works fine. –  Oct 15 '14 at 07:44
  • I have created [a fiddle](https://dotnetfiddle.net/xaUXZ6) using Autofac latest version (3.5.2) and registering your interface that way seems to be fine. Maybe there are side effects from the rest of your configuration code or the code registering MVC components? – Daniel J.G. Oct 15 '14 at 08:49
  • @DanielJ.G. Hmm maybe there is sth wrong with MVC intergration package? I'll create sample project and try. –  Oct 16 '14 at 13:13
  • I'd be curious to see what happens if you executed the Build() and then container.Resolve>> *before* your existing RegisterAssemblyTypes() call (to see if those initial registrations are inadvertantly picking it up). If you have a type that is both IMyInterface and is say a controller or filter type for instance, it will be registered by the first four registrations, then again during the RegisterAssemblyTypes() call. – ScheuNZ Apr 16 '15 at 00:59
  • This is interesting, I cannot get *AssignableTo* to work at all. Changing it to what Autofac doc recommends *Where(t => t.GetInterfaces().Contains(typeof(IMyInterface)))* works as expected, I only get one lazy loaded instance. – Ruskin Sep 06 '15 at 21:41

0 Answers0