0

Is it possible to use an assembly wild card when calling container.RegisterTypes?

i.e. If I want to just register classes from Assemblies that start with Foo.Bar, is there a way to do that. So if I had Foo.Bar.Test1 and then Foo.Bar.Test2 assemblies, it would grab both of those without having to explicitly reference those names?

g.t.w.d
  • 601
  • 1
  • 10
  • 30

1 Answers1

7

Yes you can do that. See below code to register types from an assembly with in given namespace:

var container = new UnityContainer();

container.AddNewExtension<Interception>();
container.RegisterTypes(
    AllClasses.FromLoadedAssemblies().Where(
      t => t.Namespace == "Foo.Bar"),
    WithMappings.MatchingInterface,
    WithName.Default);

Check if all types are registered from that Assembly under specific namespace:

Console.WriteLine("Container has {0} Registrations:",
  container.Registrations.Count());
foreach (ContainerRegistration item in container.Registrations)
{
  Console.WriteLine(item.GetMappingAsString());
}
vendettamit
  • 14,315
  • 2
  • 32
  • 54
  • Thanks. But for some reason I now get this: is an interface and cannot be constructed. Are you missing a type mapping And it only happens intermittently. Any ideas? – g.t.w.d May 04 '15 at 17:44
  • similar problem http://stackoverflow.com/questions/14549156/exception-is-invalidoperationexception-the-current-type-is-an-interface-and see if you're not doing something like this. – vendettamit May 04 '15 at 18:26
  • Not doing that. It blows up when I call container.BuildUp(this as T); The class I am passing into that method has these two properties: [Dependency] public IBusObjectA BusinessObjectA { get; set; } [Dependency] public IBusObjectB BusinessObjectB { get; set; } If I clean the solution and run it, it is fine. But if I reload the page that's being pushed into BuildUp, it throws that error. – g.t.w.d May 04 '15 at 18:31
  • where are you calling the container.BuildUp() method? – vendettamit May 04 '15 at 18:34
  • In a base class that all my pages inherit from. So whenever a page is rendered, it calls that. Like I said, it works the first time or so and then breaks. It's really odd. – g.t.w.d May 04 '15 at 19:09
  • No!!, You shouldn't be registering the dependencies there unless it's an special case like 'Run time registration'. Either the registration should be placed in Global.asax or in Startup.cs file. Make sure your Container instance remain public and singleton to your application. – vendettamit May 04 '15 at 19:21
  • The container lives in the global.asax. I just do the build up from the base page. but the code to actually build my container is in the global.asax. I was trying to avoid container.Resolve all over the place. – g.t.w.d May 04 '15 at 19:23
  • Can you add few more code sections where you're facing problems? Because at this moment the problem is becoming something else. May be you can put a new question for this issue. – vendettamit May 04 '15 at 19:29
  • Well, my originally problem didn't have code sections. But I will start a new question. – g.t.w.d May 04 '15 at 19:31