26

Closed

Throw more informative exception when a type cannot be loaded

description

<p>The FileLoadException currently thrown when there is an invalid unity configuration for a type doesn&#39;t provide any information about which part of the configuration is wrong.</p> <p>&nbsp;</p> <p>[FileLoadException: The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047)]</p> <p> System.RuntimeTypeHandle.GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMarkHandle stackMark, Boolean loadTypeFromPartialName, ObjectHandleOnStack type) +0</p> <p> System.RuntimeTypeHandle.GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMark&amp; stackMark, Boolean loadTypeFromPartialName) +95</p> <p> System.RuntimeType.GetType(String typeName, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMark&amp; stackMark) +54</p> <p> System.Type.GetType(String typeName) +50</p> <p> Microsoft.Practices.Unity.Configuration.ConfigurationHelpers.TypeResolverImpl.SearchAssemblies(String typeNameOrAlias) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity.Configuration\Src\ConfigurationHelpers\TypeResolverImpl.cs:256</p> <p> Microsoft.Practices.Unity.Configuration.ConfigurationHelpers.TypeResolverImpl.ResolveTypeThroughSearch(String typeNameOrAlias) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity.Configuration\Src\ConfigurationHelpers\TypeResolverImpl.cs:192</p> <p> Microsoft.Practices.Unity.Configuration.ConfigurationHelpers.TypeResolverImpl.ResolveTypeInternal(String typeNameOrAlias) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity.Configuration\Src\ConfigurationHelpers\TypeResolverImpl.cs:119</p> <p> Microsoft.Practices.Unity.Configuration.ConfigurationHelpers.TypeResolverImpl.ResolveType(String typeNameOrAlias, Boolean throwIfResolveFails) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity.Configuration\Src\ConfigurationHelpers\TypeResolverImpl.cs:105</p> <p> Microsoft.Practices.Unity.Configuration.RegisterElement.GetRegisteringType() in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity.Configuration\Src\RegisterElement.cs:119</p> <p> Microsoft.Practices.Unity.Configuration.RegisterElement.ConfigureContainer(IUnityContainer container) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity.Configuration\Src\RegisterElement.cs:0</p> <p> Microsoft.Practices.Unity.Configuration.&lt;&gt;c__DisplayClass1.&lt;ConfigureContainer&gt;b__0(ContainerConfiguringElement element) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity.Configuration\Src\ContainerElement.cs:114</p> <p> Microsoft.Practices.ObjectBuilder2.EnumerableExtensions.ForEach(IEnumerable`1 sequence, Action`1 action) +135</p> <p> Microsoft.Practices.Unity.Configuration.ContainerElement.ConfigureContainer(IUnityContainer container) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity.Configuration\Src\ContainerElement.cs:110</p> <p> Microsoft.Practices.Unity.Configuration.UnityConfigurationSection.Configure(IUnityContainer container, String configuredContainerName) in e:\Builds\Unity\UnityTemp\Compile\Unity\Unity.Configuration\Src\UnityConfigurationSection.cs:151</p> <p> Neovolve.Toolkit.Unity.UnityContainerResolver.Resolve(IConfigurationStore configuration, String unitySectionName, String containerName) in D:\Codeplex\Neovolve\Neovolve.Toolkit\Neovolve.Toolkit.Unity\UnityContainerResolver.cs:94</p> <p> Neovolve.Toolkit.Unity.UnityContainerResolver.Resolve() in D:\Codeplex\Neovolve\Neovolve.Toolkit\Neovolve.Toolkit.Unity\UnityContainerResolver.cs:26</p> <p> Neovolve.Toolkit.Unity.UnityHttpModuleBase.AssignContainer(Func`1 getContainer, Boolean allowContainerReassignment) in D:\Codeplex\Neovolve\Neovolve.Toolkit\Neovolve.Toolkit.Unity\UnityHttpModuleBase.cs:73</p> <p> Neovolve.Toolkit.Unity.UnityHttpModuleBase.Init(HttpApplication context) in D:\Codeplex\Neovolve\Neovolve.Toolkit\Neovolve.Toolkit.Unity\UnityHttpModuleBase.cs:43</p> <p> Neovolve.Toolkit.Unity.UnityHttpModule.Init(HttpApplication context) in D:\Codeplex\Neovolve\Neovolve.Toolkit\Neovolve.Toolkit.Unity\UnityHttpModule.cs:126</p> <p> System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers) +431</p> <p> System.Web.HttpApplication.InitSpecial(HttpApplicationState state, MethodInfo[] handlers, IntPtr appContext, HttpContext context) +194</p> <p> System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext, HttpContext context) +339</p> <p> System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext) +253</p> <p>&nbsp;</p> <p>[HttpException (0x80004005): The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047)]</p> <p> System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +8946484</p> <p> System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +97</p> <p> System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +256</p> <p>&nbsp;</p> <p>Unity has progressed well in this area such as when a build up fails. The exception in such a circumstance tells you exactly what happened and where in the build up operation the failure was encountered. </p> <p>&nbsp;</p> <p>The above exception is unfortunately completely unhelpful. Please update type resolution support to include the resolution attempt information when a type fails to load.</p>
Closed Aug 7, 2012 at 2:06 AM by
Fixed

comments

ctavares wrote Aug 19, 2010 at 7:46 PM

Ideally, I'd like to point directly to which line in the config file is wrong, but the configuration system doesn't supply any way to get to that information, unfortunately. I agree that we can at least say which type we're trying to resolve in that case.

bmains wrote Oct 12, 2010 at 9:00 PM

I agree; at least as when its pulling the type name, put the name of the type erroring right there, instead of making us guess...

bartdk wrote Jan 18, 2011 at 10:34 AM

Why on earth is this not implemented by default ?

ErikS wrote Feb 17, 2011 at 2:14 PM

To figure out the failing config I needed to download Unity src, compile, reference new dlls in my project, then step through everything... a better exception handling here could save a few hours debugging .. :P

Microsoft.Practices.Unity.Configuration.RegisterElement.GetRegisteringType - get the TypeName into exception message, mabye?

gmelnik wrote Mar 5, 2011 at 8:01 PM

This is already on the backlog for the next version of Unity.

FredN wrote Dec 20, 2011 at 6:18 PM

I think this is a bug that should be easy to fix.

Looking at the code in TypeResolverImpl.cs at the method SearchAssemblies(string typeNameOrAlias)

typeNameorAlias already contains the "classname, assemblyname" from the config file.

The MakeAssembleQualifiedName() method just concatenates the typeNameOrAlias + ", " + asm which creates a name of "classname, assemblyname, asm"
which Type.GetType() does not like and throws an exception.

I think a check for ", assemblyname" and removing it would then let the rest of code work and a more useful exception be thrown.

henryhbruce wrote Jul 5, 2012 at 5:24 PM

Please fix this unacceptably uninformative exception handling

gmelnik wrote Aug 2, 2012 at 10:50 PM

Fixed in Aug 2012 update (Unity 2.1.505.2). Get it via NuGet. See http://bit.ly/Ot1yLj