客户在我们的 Web 应用程序中遇到错误 - 完整的堆栈跟踪位于底部,但异常消息是
System.Web.HttpParseException (0x80004005): e:\inetpub\wwwroot\Application\Mycontrol.ascx(19): error CS1061: 'System.Collections.Generic.IList<MyClass>' does not contain a definition for 'ToArray' and no extension method 'ToArray' accepting a first argument of type 'System.Collections.Generic.IList<MyClass>' could be found (are you missing a using directive or an assembly reference?)
果然,标记中有这样的代码
<%# MyList.ToArray() %>
并且页面顶部没有要导入的导入指令System.Linq
。所以我可以看到异常来自哪里,但是(以经典的开发人员风格)它可以在我的机器上运行!它也适用于已安装该软件的所有其他服务器。
我仔细检查了web.config
它是否不包括System.Linq
使用 system.web/pages/namespaces 元素(我还检查了 applicationHost.config)。.Linq
两个文件中 都没有提及。
编辑#1
作为快速检查,我在 VisualStudio 中创建了一个新的 .Net 4 Web 应用程序。
香草 web.config 没有提到System.Linq
或System.Core.dll
. (我的 applicationHost.config 或 machine.config 文件也没有)。
我将这段 linq 代码添加到 Default.aspx
<%= new[] {"harp", "plinth", "uncanny", "donkey"}.ToArray().Count() %>
不添加任何导入指令。我在屏幕上看到了“4”——这意味着正确的 linq 代码正在运行。
因此,不知何故 System.Core.dll 被包含作为参考,并且System.Linq
在此页面的编译期间正在导入命名空间(显然是魔术!)
编辑#2
我们的支持人员刚刚报告说,添加
<%@ Import namespace="System.Linq" %>
进入错误页面可以解决问题。所以我得出的结论是,问题仅限于System.Linq
命名空间没有在客户的机器上“自动”导入(因为它在我的和其他所有机器上),并且与 System.Core.dll 在期间不可用没有任何关系页面编译。
所以我的问题是双重的 - 有人可以解释为什么它可以在我的机器上运行以及为什么它可能会在客户的机器上失败。
System.Web.HttpParseException (0x80004005): e:\inetpub\wwwroot\Application\Mycontrol.ascx(19): error CS1061: 'System.Collections.Generic.IList<MyClass>' does not contain a definition for 'ToArray' and no extension method 'ToArray' accepting a first argument of type 'System.Collections.Generic.IList<MyClass>' could be found (are you missing a using directive or an assembly reference?) ---> System.Web.HttpCompileException (0x80004005): e:\inetpub\wwwroot\Application\Mycontrol.ascx(19): error CS1061: 'System.Collections.Generic.IList<MyClass>' does not contain a definition for 'ToArray' and no extension method 'ToArray' accepting a first argument of type 'System.Collections.Generic.IList<MyClass>' could be found (are you missing a using directive or an assembly reference?)
at System.Web.Compilation.BuildManager.PostProcessFoundBuildResult(BuildResult result, Boolean keyFromVPP, VirtualPath virtualPath)
at System.Web.Compilation.BuildManager.GetBuildResultFromCacheInternal(String cacheKey, Boolean keyFromVPP, VirtualPath virtualPath, Int64 hashCode, Boolean ensureIsUpToDate)
at System.Web.Compilation.BuildManager.GetVPathBuildResultFromCacheInternal(VirtualPath virtualPath, Boolean ensureIsUpToDate)
at System.Web.Compilation.BuildManager.GetVPathBuildResultInternal(VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile, Boolean throwIfNotFound, Boolean ensureIsUpToDate)
at System.Web.Compilation.BuildManager.GetVPathBuildResultWithNoAssert(HttpContext context, VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile, Boolean throwIfNotFound, Boolean ensureIsUpToDate)
at System.Web.Compilation.BuildManager.GetVPathBuildResult(HttpContext context, VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile, Boolean ensureIsUpToDate)
at System.Web.UI.BaseTemplateParser.GetReferencedType(VirtualPath virtualPath, Boolean allowNoCompile)
at System.Web.UI.BaseTemplateParser.GetUserControlType(VirtualPath virtualPath)
at System.Web.UI.MainTagNameToTypeMapper.ProcessUserControlRegistration(UserControlRegisterEntry ucRegisterEntry)
at System.Web.UI.BaseTemplateParser.ProcessDirective(String directiveName, IDictionary directive)
at System.Web.UI.TemplateParser.ParseStringInternal(String text, Encoding fileEncoding)
at System.Web.UI.TemplateParser.ProcessException(Exception ex)
at System.Web.UI.TemplateParser.ParseStringInternal(String text, Encoding fileEncoding)
at System.Web.UI.TemplateParser.ParseString(String text, VirtualPath virtualPath, Encoding fileEncoding)
at System.Web.UI.TemplateParser.ParseFile(String physicalPath, VirtualPath virtualPath)
at System.Web.UI.TemplateParser.Parse()
at System.Web.Compilation.BaseTemplateBuildProvider.get_CodeCompilerType()
at System.Web.Compilation.BuildProvider.GetCompilerTypeFromBuildProvider(BuildProvider buildProvider)
at System.Web.Compilation.BuildProvidersCompiler.ProcessBuildProviders()
at System.Web.Compilation.BuildProvidersCompiler.PerformBuild()
at System.Web.Compilation.BuildManager.CompileWebFile(VirtualPath virtualPath)
at System.Web.Compilation.BuildManager.GetVPathBuildResultInternal(VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile, Boolean throwIfNotFound, Boolean ensureIsUpToDate)
at System.Web.Compilation.BuildManager.GetVPathBuildResultWithNoAssert(HttpContext context, VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile, Boolean throwIfNotFound, Boolean ensureIsUpToDate)
at System.Web.Compilation.BuildManager.GetVirtualPathObjectFactory(VirtualPath virtualPath, HttpContext context, Boolean allowCrossApp, Boolean throwIfNotFound)
at System.Web.Compilation.BuildManager.CreateInstanceFromVirtualPath(VirtualPath virtualPath, Type requiredBaseType, HttpContext context, Boolean allowCrossApp)
at System.Web.UI.PageHandlerFactory.GetHandlerHelper(HttpContext context, String requestType, VirtualPath virtualPath, String physicalPath)
at System.Web.HttpApplication.MapHttpHandler(HttpContext context, String requestType, VirtualPath path, String pathTranslated, Boolean useAppConfig)
at System.Web.HttpApplication.MapHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)