Need help troubleshooting TypeLoadException at .NET webapi2 application startup

122 views Asked by At

I have a working webapi2 application, with references to some common/shared library assemblies we own. Those shared libraries have a different repository and they get published as nuget packages during the build process. Recently, I updated the nuget package references for the webapi2 application to more recent versions for those common packages. The newer version of one of the assemblies, Microsoft.Icm.Common.Web, no longer has an attribute, WebApiExceptionHandlerAttribute. So, when building the webapi2 application, I got rid of all references to that attribute in our webapi2 controllers classes. When I rebuilt the application and deployed it one of our staging environments, I see the following typeload exception:

Message: Could not load type 'Microsoft.Icm.Common.Web.WebApiExceptionHandlerAttribute' from assembly 'Microsoft.Icm.Common.Web, Version=2.4.0.0, Culture=neutral, PublicKeyToken=null'.

StackTrace: at System.ModuleHandle.ResolveType(RuntimeModule module, Int32 typeToken, IntPtr* typeInstArgs, Int32 typeInstCount, IntPtr* methodInstArgs, Int32 methodInstCount, ObjectHandleOnStack type) at System.ModuleHandle.ResolveTypeHandleInternal(RuntimeModule module, Int32 typeToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext) at System.Reflection.RuntimeModule.ResolveType(Int32 metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments) at System.Reflection.CustomAttribute.FilterCustomAttributeRecord(CustomAttributeRecord caRecord, MetadataImport scope, Assembly& lastAptcaOkAssembly, RuntimeModule decoratedModule, MetadataToken decoratedToken, RuntimeType attributeFilterType, Boolean mustBeInheritable, Object[] attributes, IList derivedAttributes, RuntimeType& attributeType, IRuntimeMethodInfo& ctor, Boolean& ctorHasParameters, Boolean& isVarArg) at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeModule decoratedModule, Int32 decoratedMetadataToken, Int32 pcaCount, RuntimeType attributeFilterType, Boolean mustBeInheritable, IList derivedAttributes, Boolean isDecoratedTargetSecurityTransparent) at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeType type, RuntimeType caType, Boolean inherit) at System.Web.Http.Controllers.HttpControllerDescriptor.InvokeAttributesOnControllerType(HttpControllerDescriptor controllerDescriptor, Type type) at System.Web.Http.Controllers.HttpControllerDescriptor..ctor(HttpConfiguration configuration, String controllerName, Type controllerType) at System.Web.Http.Dispatcher.DefaultHttpControllerSelector.InitializeControllerInfoCache() at System.Lazy1.CreateValue() at System.Lazy1.LazyInitValue() at System.Web.Http.Dispatcher.DefaultHttpControllerSelector.GetControllerMapping() at System.Web.Http.Routing.AttributeRoutingMapper.AddRouteEntries(SubRouteCollection collector, HttpConfiguration configuration, IInlineConstraintResolver constraintResolver, IDirectRouteProvider directRouteProvider) at System.Web.Http.Routing.AttributeRoutingMapper.<>c__DisplayClass2.<>c__DisplayClass4.b__1() at System.Web.Http.Routing.RouteCollectionRoute.EnsureInitialized(Func`1 initializer) at System.Web.Http.Routing.AttributeRoutingMapper.<>c__DisplayClass2.b__0(HttpConfiguration config) at System.Web.OData.Routing.Conventions.AttributeRoutingConvention.<>c__DisplayClass1.<.ctor>b__0(HttpConfiguration config) at System.Web.OData.Routing.Conventions.AttributeRoutingConvention.<>c__DisplayClass1.<.ctor>b__0(HttpConfiguration config) at System.Web.OData.Routing.Conventions.AttributeRoutingConvention.<>c__DisplayClass1.<.ctor>b__0(HttpConfiguration config) at System.Web.OData.Routing.Conventions.AttributeRoutingConvention.<>c__DisplayClass1.<.ctor>b__0(HttpConfiguration config) at System.Web.OData.Routing.Conventions.AttributeRoutingConvention.<>c__DisplayClass1.<.ctor>b__0(HttpConfiguration config) at System.Web.OData.Routing.Conventions.AttributeRoutingConvention.<>c__DisplayClass1.<.ctor>b__0(HttpConfiguration config) at System.Web.OData.Routing.Conventions.AttributeRoutingConvention.<>c__DisplayClass1.<.ctor>b__0(HttpConfiguration config) at System.Web.OData.Routing.Conventions.AttributeRoutingConvention.<>c__DisplayClass1.<.ctor>b__0(HttpConfiguration config) at System.Web.OData.Routing.Conventions.AttributeRoutingConvention.<>c__DisplayClass1.<.ctor>b__0(HttpConfiguration config) at System.Web.OData.Routing.Conventions.AttributeRoutingConvention.<>c__DisplayClass1.<.ctor>b__0(HttpConfiguration config) at System.Web.OData.Routing.Conventions.AttributeRoutingConvention.<>c__DisplayClass1.<.ctor>b__0(HttpConfiguration config) at System.Web.OData.Routing.Conventions.AttributeRoutingConvention.<>c__DisplayClass1.<.ctor>b__0(HttpConfiguration config) at System.Web.OData.Routing.Conventions.AttributeRoutingConvention.<>c__DisplayClass1.<.ctor>b__0(HttpConfiguration config) at System.Web.OData.Routing.Conventions.AttributeRoutingConvention.<>c__DisplayClass1.<.ctor>b__0(HttpConfiguration config) at System.Web.OData.Routing.Conventions.AttributeRoutingConvention.<>c__DisplayClass1.<.ctor>b__0(HttpConfiguration config) at System.Web.OData.Routing.Conventions.AttributeRoutingConvention.<>c__DisplayClass1.<.ctor>b__0(HttpConfiguration config) at System.Web.OData.Routing.Conventions.AttributeRoutingConvention.<>c__DisplayClass1.<.ctor>b__0(HttpConfiguration config) at System.Web.OData.Routing.Conventions.AttributeRoutingConvention.<>c__DisplayClass1.<.ctor>b__0(HttpConfiguration config) at System.Web.OData.Routing.Conventions.AttributeRoutingConvention.<>c__DisplayClass1.<.ctor>b__0(HttpConfiguration config) at System.Web.OData.Routing.Conventions.AttributeRoutingConvention.<>c__DisplayClass1.<.ctor>b__0(HttpConfiguration config) at System.Web.OData.Routing.Conventions.AttributeRoutingConvention.<>c__DisplayClass1.<.ctor>b__0(HttpConfiguration config) at System.Web.OData.Routing.Conventions.AttributeRoutingConvention.<>c__DisplayClass1.<.ctor>b__0(HttpConfiguration config) at System.Web.OData.Routing.Conventions.AttributeRoutingConvention.<>c__DisplayClass1.<.ctor>b__0(HttpConfiguration config) at System.Web.OData.Routing.Conventions.AttributeRoutingConvention.<>c__DisplayClass1.<.ctor>b__0(HttpConfiguration config) at System.Web.OData.Routing.Conventions.AttributeRoutingConvention.<>c__DisplayClass1.<.ctor>b__0(HttpConfiguration config) at System.Web.OData.Routing.Conventions.AttributeRoutingConvention.<>c__DisplayClass1.<.ctor>b__0(HttpConfiguration config) at System.Web.OData.Routing.Conventions.AttributeRoutingConvention.<>c__DisplayClass1.<.ctor>b__0(HttpConfiguration config) at System.Web.OData.Routing.Conventions.AttributeRoutingConvention.<>c__DisplayClass1.<.ctor>b__0(HttpConfiguration config) at System.Web.OData.Routing.Conventions.AttributeRoutingConvention.<>c__DisplayClass1.<.ctor>b__0(HttpConfiguration config) at System.Web.OData.Routing.Conventions.AttributeRoutingConvention.<>c__DisplayClass1.<.ctor>b__0(HttpConfiguration config) at System.Web.OData.Routing.Conventions.AttributeRoutingConvention.<>c__DisplayClass1.<.ctor>b__0(HttpConfiguration config) at System.Web.OData.Routing.Conventions.AttributeRoutingConvention.<>c__DisplayClass1.<.ctor>b__0(HttpConfiguration config) at System.Web.OData.Routing.Conventions.AttributeRoutingConvention.<>c__DisplayClass1.<.ctor>b__0(HttpConfiguration config) at System.Web.OData.Routing.Conventions.AttributeRoutingConvention.<>c__DisplayClass1.<.ctor>b__0(HttpConfiguration config) at System.Web.OData.Routing.Conventions.AttributeRoutingConvention.<>c__DisplayClass1.<.ctor>b__0(HttpConfiguration config) at System.Web.OData.Routing.Conventions.AttributeRoutingConvention.<>c__DisplayClass1.<.ctor>b__0(HttpConfiguration config) at System.Web.OData.Routing.Conventions.AttributeRoutingConvention.<>c__DisplayClass1.<.ctor>b__0(HttpConfiguration config) at System.Web.OData.Routing.Conventions.AttributeRoutingConvention.<>c__DisplayClass1.<.ctor>b__0(HttpConfiguration config) at System.Web.OData.Routing.Conventions.AttributeRoutingConvention.<>c__DisplayClass1.<.ctor>b__0(HttpConfiguration config) at System.Web.OData.Routing.Conventions.AttributeRoutingConvention.<>c__DisplayClass1.<.ctor>b__0(HttpConfiguration config) at System.Web.OData.Routing.Conventions.AttributeRoutingConvention.<>c__DisplayClass1.<.ctor>b__0(HttpConfiguration config) at Microsoft.AzureAd.Icm.IcmV3OData.Global.Initialize() in E:\bt\787852\repo\src\OData\ICMV3OData\Global.asax.cs:line 66 at Microsoft.AzureAd.Icm.IcmV3OData.PreLoadInitializationClient.Preload(String[] parameters) in E:\bt\787852\repo\src\OData\ICMV3OData\App_Start\PreLoadInitializationClient.cs:line 40 at System.Web.Hosting.PreloadHost.CreateIProcessHostPreloadClientInstanceAndCallPreload(String preloadObjTypeName, String[] paramsForStartupObj) at System.Web.Hosting.PreloadHost.CreateIProcessHostPreloadClientInstanceAndCallPreload(String preloadObjTypeName, String[] paramsForStartupObj) at System.Web.Hosting.ProcessHost.PreloadApplicationIfRequired(String appId, IApplicationHost appHostParameter, HostingEnvironmentParameters hostingParameters, LockableAppDomainContext ac)'. The worker process will be marked unhealthy and be shutdown. The data field contains the error code.

Even though my recently built webapi2 application should no longer have references to the unavailable WebApiExceptionHandlerAttribute (in the newer Microsoft.Icm.Common.Web assembly), when running the application, looks like somehow the unavailable attribute is being used. I have used tools like Resharper's dotnetpeek and searched for the term "WebApiExceptionHandlerAttribute" in the binplaced assemblies and I don't see any matches for that attribute. So, how do I figure out why is the web application still trying to locate that type at application startup?

1

There are 1 answers

0
Deepak Vishwanathan On

I have identified the root cause of this issue. Looks like I had another stale assembly in the bin folder for webapi2 application, which had been deprecated for almost 3 months and was no longer generated when the application was built. That stale assembly had references to the now defunct attribute. Once I removed the stale assembly from the iis webapi2 application folder, things started working fine.