質問

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.

役に立ちましたか?

解決

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);

他のヒント

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

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top