Unity 3 configuration by convention does not find types in web project

I am trying to configure this convention configuration, but I am having a problem in my ASP.NET MVC5 project ..

I added the following to my Application_Start method and connected it to DependencyResolver

public static IUnityContainer CreateContainer() { IUnityContainer container = new UnityContainer(); container.RegisterTypes( AllClasses.FromAssembliesInBasePath(), WithMappings.FromAllInterfaces, WithName.Default, WithLifetime.ContainerControlled); return container; } 

But it cannot register any types upon closer inspection, when I see that in AllClasses.FromAssembliesInBasePath () it is always null or empty.

Am I doing something wrong? Is there a better place where I should put this?

Thank. Ste.

+3
c # dependency-injection asp.net-mvc-5 unity-container
Apr 03 '14 at 7:11
source share
2 answers

The reason may be that the base domain path is not the one you were thinking about.

Please try this if it logs something:

  container.RegisterTypes( AllClasses.FromAssemblies(Assembly.GetExecutingAssembly()), WithMappings.FromAllInterfaces, WithName.Default, WithLifetime.ContainerControlled); 
+4
Apr 03 '14 at 8:01
source share

I think that the proposal AllClasses.FromAssemblies(Assembly.GetExecutingAssembly()) proposed above will work only when you have WebAPI as the only project, if your business logic and data access are present in different assemblies, this may not work. as was expected.

I ran into the same problem with the AllClasses.FromAssembliesInBasePath() method in Unity.

Refer to the code provided in the following location:

Unity Framework Project: Unity.RegistrationByConvention

File: AllClasses.Desktop.cs

Method: GetAssembliesInBasePath

Line: 59

basePath = AppDomain.CurrentDomain.BaseDirectory;

This sets the basePath value to the root of the project \ WebAPIProject \ instead of pointing to the bin folder.

If we set basePath as shown below, it will return the path accordingly.

basePath = AppDomain.CurrentDomain.RelativeSearchPath ?? AppDomain.CurrentDomain.BaseDirectory;

I'm not sure if this requires a fix in the Unity Framework, saying that in order to get the job to work properly, the relative search path was convenient in my case.

I distracted the code from Unity to rewrite the AllClasses class with the fix mentioned above.

Class Diagram of AllClasses custom implementation

+3
Oct 29 '14 at 7:14
source share



All Articles