4

I am trying to get this convention configuration working but I am having a problem in my ASP.NET MVC5 Project..

I have added the following in my Application_Start method and hooked it up to DependencyResolver

 public static IUnityContainer CreateContainer()
    {
        IUnityContainer container = new UnityContainer();

        container.RegisterTypes(

            AllClasses.FromAssembliesInBasePath(),

            WithMappings.FromAllInterfaces,

            WithName.Default,

            WithLifetime.ContainerControlled);

        return container;
    }

But it fails to register any types, on closer inspection, when I see whats in AllClasses.FromAssembliesInBasePath() it always runs null or empty.

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

Thanks. Ste.

Steoates
  • 3,058
  • 5
  • 27
  • 43

2 Answers2

4

The reason might be that the domain base path is not what you thought.

Please try this see if it registers anything:

  container.RegisterTypes(

        AllClasses.FromAssemblies(Assembly.GetExecutingAssembly()),

        WithMappings.FromAllInterfaces,

        WithName.Default,

        WithLifetime.ContainerControlled);
Stephen Zeng
  • 2,748
  • 21
  • 18
3

I guess the suggestion above to use AllClasses.FromAssemblies(Assembly.GetExecutingAssembly()) will work only when you have the WebAPI as the only project, if your business logic and data access are present in different assemblies it might not work as expected.

I faced the same issue with AllClasses.FromAssembliesInBasePath() method in Unity.

Please refer to the code present in the following location:

Unity Framework Project :Unity.RegistrationByConvention

File: AllClasses.Desktop.cs

Method: GetAssembliesInBasePath

Line: 59

basePath = AppDomain.CurrentDomain.BaseDirectory;

This set the value of basePath 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 appropriately.

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

I am not sure if this requires a fix in Unity Framework, having said that, to get things working as expected, the relative search path was handy in my case.

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

Class Diagram of AllClasses custom implementation

  • This is correct: "... will work only when you have the WebAPI as the only project ...". Did you find a solution? If so, care to elaborate it so others can use it? – Charles Prakash Dasari Nov 24 '14 at 09:11
  • 1
    Hi Charles, I solved this writing an extension method by duplicating code from Unity, with the fix for basePath mentioned above. Though this is a nasty solution, I had some deadlines to meet. BTW, will someone from Unity team address this? If so we can discuss this, branch the code from GitHub and fix this. Will try to post an example in GitHub – Srihari Sridharan Dec 02 '14 at 06:52
  • 1
    I have posted the class diagram for AllClasses.cs above. The code is same as the one available in Unity + the fix mentioned above. – Srihari Sridharan Dec 02 '14 at 08:35