thedersen / UnityConfiguration

Convention based configuration API for the Unity IoC container.
thedersen.com/projects/unityconfiguration
MIT License
38 stars 15 forks source link

AssemblyScanner not bin folder friendly for web apps #6

Closed ghost closed 12 years ago

ghost commented 12 years ago

I tried to incorporate the UnityConifguration library in a MVC3 project, and was getting errors that my mappings weren't found. I used the following code, which is very similar to what I use from StructureMap.

Scan(scan =>
{
                scan.AssembliesInBaseDirectory();
                scan.WithNamingConvention();
});

When running the app, the Unity container object didn't have any registrations. After digging into it, and only by comparing StructureMap's source for their AssembliesFromApplicationBaseDirectory() method, I discovered you are only looking in the AppDomain.CurrentDomain.BaseDirectory directory (which is great for desktop apps), but does no good for web apps. The missing piece is additionally looking in AppDomain.CurrentDomain.SetupInformation.PrivateBinPath which is where the assemblies are for web apps.

The work-around is to do the following:

Scan(scan =>
{
                scan.AssembliesInDirectory(AppDomain.CurrentDomain.SetupInformation.PrivateBinPath);
                scan.WithNamingConvention();
});

I'm sure others have run into this and it might be nice to bake this into the UnityConfiguration.AssemblyScanner class.

thedersen commented 12 years ago

Thanks for a very detailed issue report. Will put up a new version on NuGet in just a moment.