我正在尝试让 ASP.NET MVC 3 站点在 IIS 6.0 上运行。
当前,当我从服务器请求页面时,会出现以下错误:
解析器错误消息:在应用程序的预启动初始化阶段无法调用此方法。
在这一行:
<add name="MyMembershipProvider" type="NS.MyMembershipProvider" connectionStringName="MyDatabase" applicationName="/MySite"/>
我完全被难住了,对 ASP.NET 应用程序生命周期没有太多了解,更不用说 6.0 和 7.0 之间的差异了。阅读 MSDN 页面似乎没有多大帮助。
有人有任何见解或任何好的调查链接吗? :)
将其添加到您的 web.config 中(在 appSettings 部分):
<add key="enableSimpleMembership" value="false"/>
<add key="autoFormsAuthentication" value="false"/>
编辑:
对于那些问为什么的人,这是 mvc 3 发行说明中描述的已知问题 更多详情这里
将我的一些应用程序从 ASP.NET MVC3 升级到 MVC4 后,我收到此错误。它是 WebMatrix 程序集(WebMatrix.WebData.dll 和 WebMatrix.Data.dll)的结果。我从 /bin 目录中删除了这些引用和程序集,这解决了这个问题。
MembershipProvider
迁移到新的 ExtendedMembershipProvider
,使我们能够利用 WebMatrix 命名空间中提供的一些新功能。默认情况下,由于某种原因启用了简单会员资格,因此我们必须明确禁用它,因为我们不想使用 SimpleMembershipProvider
。
我们所要做的就是将其添加到 web.config 中:
<add key="enableSimpleMembership" value="false"/>
启用简单成员资格会导致提供程序初始化代码在 Application_Start 处理程序之前执行。我们的应用程序结构要求 App_Start 成为第一个执行的事情。就我个人而言,我总是希望如此,但简单会员资格改变了这种行为。当心。
好吧,我刚刚收到此错误,这是由于不小心将 .cshtml 复制到我的项目的根目录中造成的。它甚至没有包含在该项目中。删除它,错误就消失了。这是 IIS7 上的 MVC3。我想有些遇到这个问题的人也有同样的遭遇。
这是由于应用程序中过早进行的许多反射调用中的任何一个引起的。碰巧其他答案中的 Web.Config 建议阻止了此类反射调用的进行。但就我而言:
我正在使用实体框架,并运行
update-database
。我得到:
在应用程序的预启动初始化阶段无法调用此方法。
事实证明,我们的代码使用了最近修改的库,以获取所有命名空间/项目中的所有代码。具体来说,它称为:
System.Web.Compilation.BuildManager.GetReferencedAssemblies()
轰隆隆。这导致了这个不起眼的错误。 EF 迁移在一个奇怪的区域中运行,其中应用程序一半运行,一半不运行,这意味着迁移将调用的任何代码永远无法调用上述方法。
确保
<configSection>
文件中的 Web.config
元素是完全限定的类型名称 包括程序集名称。
例如,我有这个:
<section name="mySection" type="System.Configuration.DictionarySectionHandler" />
...但是一旦我将其更改为(如下),问题就解决了:
<section name="mySection" type="System.Configuration.DictionarySectionHandler, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
此问题的另一个原因是具有
<configSection><section name="mySection" type="MySectionHandler">
元素,使得 type="MySectionHandler"
属性不是包含程序集名称的完全限定类型名称。
例如,这个
Web.config
文件将在启动时导致第一次机会异常(截图如下):
<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform" xdt:Transform="Replace">
<configSections>
<section name="mySection" type="System.Configuration.DictionarySectionHandler" />
</configSections>
<appSettings>
</appSettings>
<mySection>
<add key="foo" value="bar" />
</mySection>
</configuration>
堆栈跟踪是:
System.Web.dll!System.Web.Compilation.BuildManager.EnsureTopLevelFilesCompiled()
System.Web.dll!System.Web.Compilation.BuildManager.GetType(string typeName = "System.Configuration.DictionarySectionHandler", bool throwOnError = true, bool ignoreCase = false)
System.Web.dll!System.Web.Compilation.BuildManager.GetType(string typeName = "System.Configuration.DictionarySectionHandler", bool throwOnError = true)
System.Web.dll!System.Web.Configuration.WebConfigurationHost.GetConfigType(string typeName = "System.Configuration.DictionarySectionHandler", bool throwOnError = true)
System.Configuration.dll!System.Configuration.TypeUtil.GetTypeWithReflectionPermission(System.Configuration.Internal.IInternalConfigHost host = {System.Web.Configuration.WebConfigurationHost}, string typeString = "System.Configuration.DictionarySectionHandler", bool throwOnError = true)
System.Configuration.dll!System.Configuration.RuntimeConfigurationRecord.RuntimeConfigurationFactory.Init(System.Configuration.RuntimeConfigurationRecord configRecord = ConfigPath = "machine/webroot/2", System.Configuration.FactoryRecord factoryRecord = FactoryRecord "mySection")
System.Configuration.dll!System.Configuration.RuntimeConfigurationRecord.RuntimeConfigurationFactory.InitWithRestrictedPermissions(System.Configuration.RuntimeConfigurationRecord configRecord = ConfigPath = "machine/webroot/2", System.Configuration.FactoryRecord factoryRecord = FactoryRecord "mySection")
System.Configuration.dll!System.Configuration.RuntimeConfigurationRecord.RuntimeConfigurationFactory.RuntimeConfigurationFactory(System.Configuration.RuntimeConfigurationRecord configRecord = ConfigPath = "machine/webroot/2", System.Configuration.FactoryRecord factoryRecord = FactoryRecord "mySection")
System.Configuration.dll!System.Configuration.RuntimeConfigurationRecord.CreateSectionFactory(System.Configuration.FactoryRecord factoryRecord = FactoryRecord "mySection")
System.Configuration.dll!System.Configuration.BaseConfigurationRecord.FindAndEnsureFactoryRecord(string configKey = "mySection", out bool isRootDeclaredHere = false)
System.Configuration.dll!System.Configuration.BaseConfigurationRecord.GetSectionRecursive(string configKey = "mySection", bool getLkg = false, bool checkPermission = true, bool getRuntimeObject = true, bool requestIsHere = true, out object result = null, out object resultRuntimeObject = null)
System.Configuration.dll!System.Configuration.BaseConfigurationRecord.GetSection(string configKey = "mySection", bool getLkg = false, bool checkPermission = true)
System.Configuration.dll!System.Configuration.BaseConfigurationRecord.GetSection(string configKey = "mySection")
System.Web.dll!System.Web.Configuration.HttpConfigurationSystem.GetApplicationSection(string sectionName = "mySection")
System.Web.dll!System.Web.Configuration.HttpConfigurationSystem.GetSection(string sectionName = "mySection")
System.Web.dll!System.Web.Configuration.WebConfigurationManager.GetSection(string sectionName = "mySection")
我查看了调用方法,
System.Web.dll!System.Web.Compilation.BuildManager.GetType
,我发现 BuildManager.GetType(...)
调用是因为它正在加载 <configSection>
类型,并且只有在 EnsureTopLevelFilesCompiled()
属性不包含程序集名称 - 因此它假设类型名称是对 type=""
中定义的类的引用(此时没有程序集名称)。App_Code
方法非常简单地说明了这一点
:
GetType
所以解决方法很简单:只需确保
class BuildManager
{
public static Type GetType(string typeName, bool throwOnError, bool ignoreCase) {
// If it contains an assembly name, just call Type.GetType(). Do this before even trying
// to initialize the BuildManager, so that if InitializeBuildManager has errors, it doesn't
// affect us when the type string can be resolved via Type.GetType().
Type type = null;
if (Util.TypeNameContainsAssembly(typeName)) {
type = Type.GetType(typeName, throwOnError, ignoreCase);
if (type != null) {
return type;
}
}
// [snip]
_theBuildManager.EnsureTopLevelFilesCompiled(); // <-- Throws the exception
}
是
Util.TypeNameContainsAssembly(typeName)
,通过更改:true
对此:
<section name="mySection" type="System.Configuration.DictionarySectionHandler" />