0

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?

2
  • Are you sure all assemblies are referenced in the project where you have this code? May 20, 2018 at 18:46
  • I can't say I see something wrong... May 20, 2018 at 19:04

1 Answer 1

0

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.

1
  • This helped me solve my issues, some project references were missing Jan 27, 2021 at 8:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.