Could not load type 'Microsoft.Practices.Enter

2020-04-20 21:16发布

问题:

I recently tried to implement authentication using asp.net identity with 4.6.1 framework. After installing all the required packages I am getting the following error when running the application.

"Could not load type 'Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ContainerModel.ITypeRegistrationsProvider"

The same application works fine when all the packages related to identity are uninstalled.

Full stack trace is as follows

[TypeLoadException: Could not load type 'Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ContainerModel.ITypeRegistrationsProvider' from assembly 'Microsoft.Practices.EnterpriseLibrary.Common, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.]
System.Reflection.CustomAttribute._CreateCaObject(RuntimeModule pModule, IRuntimeMethodInfo pCtor, Byte** ppBlob, Byte* pEndBlob, Int32* pcNamedArgs) +0
System.Reflection.CustomAttribute.CreateCaObject(RuntimeModule module, IRuntimeMethodInfo ctor, IntPtr& blob, IntPtr blobEnd, Int32& namedArgs) +38
System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeModule decoratedModule, Int32 decoratedMetadataToken, Int32 pcaCount, RuntimeType attributeFilterType, Boolean mustBeInheritable, IList derivedAttributes, Boolean isDecoratedTargetSecurityTransparent) +571 System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeAssembly assembly, RuntimeType caType) +103
System.Reflection.RuntimeAssembly.GetCustomAttributes(Boolean inherit) +37 Owin.Loader.DefaultLoader.SearchForStartupAttribute(String friendlyName, IList1 errors, Boolean& conflict) +294
Owin.Loader.DefaultLoader.GetDefaultConfiguration(String friendlyName, IList
1 errors) +105
Owin.Loader.DefaultLoader.LoadImplementation(String startupName, IList1 errorDetails) +161 Owin.Loader.DefaultLoader.Load(String startupName, IList1 errorDetails) +51
Microsoft.Owin.Host.SystemWeb.OwinBuilder.GetAppStartup() +238
Microsoft.Owin.Host.SystemWeb.OwinHttpModule.InitializeBlueprint() +103 System.Threading.LazyInitializer.EnsureInitializedCore(T& target, Boolean& initialized, Object& syncLock, Func1 valueFactory) +115 System.Threading.LazyInitializer.EnsureInitialized(T& target, Boolean& initialized, Object& syncLock, Func1 valueFactory) +72
Microsoft.Owin.Host.SystemWeb.OwinHttpModule.Init(HttpApplication context) +104
System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers) +534
System.Web.HttpApplication.InitSpecial(HttpApplicationState state, MethodInfo[] handlers, IntPtr appContext, HttpContext context) +172
System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext, HttpContext context) +352
System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext) +296

[HttpException (0x80004005): Could not load type 'Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ContainerModel.ITypeRegistrationsProvider' from assembly 'Microsoft.Practices.EnterpriseLibrary.Common, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.]
System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +9947380 System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +101 System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +261

It would be a great help if some one helps, I am struck for a week.

please let me know if any more information is required

Thanks in advance.

回答1:

TL;DR

Configure the OWIN startup class in your web.config / app.config appSettings like this:

<add key="owin:AppStartup" value="-- your startup class --" />     

The whole story

The problem has to do with the way that OWIN looks for the Startup class. For some strange reason, it finds ITypeRegistrationsProvider and tries to load it.

OWIN has three ways to look for the application startup class:

  • Naming Convention: Katana looks for a class named Startup in namespace matching the assembly name or the global namespace.
  • OwinStartup Attribute: This is the approach most developers will take to specify the startup class. The following attribute will set the startup class to the TestStartup class in the StartupDemo namespace.
  • The appSetting element in the Configuration file <add key="owin:AppStartup" value="..." />

If you look carefully the call stack, you'll see that the problem is related to the second one, looking for the attribute: Owin.Loader.DefaultLoader.SearchForStartupAttribute. For some reason it finds ITypeRegistrationsProvider is suitable as an Startup class, and tries to instance it. Apart from being incorrect, it fails because you cannot instance an interface.

Once you know what the problem is, if you read OWIN source code, in particular OwinBuilder.GetAppStartup, you'll see that the first option to find the startup class is to use the one specified in appSettings:

string appStartup = ConfigurationManager.AppSettings[Constants.OwinAppStartup];

If you specify yous startup class in web.config you'll prevent the application from looking and trying to instance the wrong class. So, to solve the problem, you simply have to specify the app startup class in your web.config or app.config appSettings, like this:

<appSettings>  
  <add key="owin:AppStartup" value="-- your startup class --" />       
</appSettings>

Notes:

  • in the linked document you'll see owin:appStartup, but if you check the source code you'll see that the right syntax is owin:AppStartup (OwinAppStartup constant, as used in the code snippet above)
  • you can specify yous startup class in servera ways, but I recommend you to specify it like this "namespace.ClassName, assemblyName" which is the safest one.

If your configuration is right, I warranty that the Owin loader will take your real startup class, and the error will not happen.