为什么我不需要添加<%@ Import namespace =“System.Linq”%>在我的aspx页面中使用Linq之前

时间:2012-05-24 10:50:22

标签: asp.net linq

客户在我们的网络应用程序中遇到错误 - 完整堆栈跟踪位于底部,但异常消息是

   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。所以我可以看到异常的来源,但是(在经典的开发人员风格中)它可以在我的机器上运行!它也适用于已安装该软件的所有其他服务器。

我使用system.web / pages / namespaces元素仔细检查了我的web.config它没有包含System.Linq(我还检查了applicationHost.config)。两个文件中都没有提到.Linq

编辑#1

作为一个快速检查,我在VisualStudio中创建了一个新的.Net 4 Web应用程序 vanilla web.config没有提到System.LinqSystem.Core.dll。 (我的applicationHost.config或machine.config文件都没有。)

我将这段linq代码添加到Default.aspx

<%= new[] {"harp", "plinth", "uncanny", "donkey"}.ToArray().Count() %>

不添加任何Import指令。我在屏幕上显示'4' - 这意味着正确的linq代码正在运行。

所以不知何故,System.Core.dll被包含作为参考,并且在编译此页面时正在导入System.Linq命名空间(显然是魔术!)

编辑#2

我们的支持人员刚刚报道了添加

 <%@ Import namespace="System.Linq" %>

进入错误页面修复了问题。 因此,我得出结论,问题仅限于System.Linq命名空间未在客户机器上自动导入(因为它在我的机器和其他所有机器上),而与System.Core没有任何关系。在页面编译期间无法使用。

所以我的问题是双重的 - 有人可以解释为什么它在我的机器上运行以及为什么它可能会对客户失败。

    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)

0 个答案:

没有答案
相关问题