In my codebase, I have created a container sucessfully, but then run into an exception when trying to configure it:
_container = new UnityContainer();
var unityConfigurationSection = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
if (unityConfigurationSection != null)
{
try
{
unityConfigurationSection.Configure(_container);
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
the unityConfigurationSection.Configure(_container);
line is what does me in. I get:
The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047)
It looks like there's a type that can't be resolved from its name, judging from the stack trace.
But how do I figure out what type?
The exception indicates that the name of the assembly that was requested is in some way not able to be parsed and looked up. If the assembly name was valid but it just wasn't found you'd get a different error.
Usually the message of the exception includes the assembly that failed to resolve. For instance, this code throws the exact exception:
var asm = Assembly.Load("test,,;''");
This is obviously not a valid assembly name, so you get:
System.IO.FileLoadException: 'Could not load file or assembly '"test\,\,;''"' or one of its dependencies. The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047)'
I'd debug the code and see if you can get to the message of the exception. One thing I'd try is to go into Tools -> Options -> Debugging -> Enable Just My Code and remove the check mark. This should let you see the unwrapped exception that gets thrown when Unity tries to resolve the dependency.
Another quick approach would be to handle the AssemblyResolve Event. Whenever an assembly that was looked for by name fails to resolve for whatever reason that Event is invoked. The args parameter contain the name of the assembly that was tried to be resolved.
Put this as the first line in your Program:
AppDomain.CurrentDomain.AssemblyResolve += Resolver;
and then put this method into the same class:
private static Assembly Resolver(object sender, ResolveEventArgs args)
{
string assemblyName = args.Name;
throw new Exception(args.Name);
}
Now you can set a breakpoint and check out the name of the assembly that tried to get resolved.
User contributions licensed under CC BY-SA 3.0