【问题标题】:Why is this Typescript interface causing a null exception in runtime bundling?为什么这个 Typescript 接口会在运行时捆绑中导致空异常?
【发布时间】:2014-05-04 15:06:56
【问题描述】:

我在 VS 2013 中启动了一个新的 MVC Razor 测试 Web 项目。我将所有 NuGet 包更新到今天的最新版本,并开始包含来自另一个项目的我的 TypeScript 插件进行测试。其中一个插件导致了这个运行时错误:

堆栈跟踪错误:

Object reference not set to an instance of an object. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error: 
Line 39:     @Scripts.Render("~/bundles/jquery")
Line 40:     @Scripts.Render("~/bundles/bootstrap")
Line 41:     @Scripts.Render("~/bundles/ListView")
Line 42:     @RenderSection("scripts", required: false)
Line 43: </body> 
Stack Trace: 

[NullReferenceException: Object reference not set to an instance of an object.]
   Microsoft.Ajax.Utilities.JSParser.ParseUnaryExpression(Boolean& isLeftHandSideExpr, Boolean isMinus) +298
   Microsoft.Ajax.Utilities.JSParser.ParseExpressionStatement(Boolean fSourceElement) +125
   Microsoft.Ajax.Utilities.JSParser.ParseStatement(Boolean fSourceElement, Boolean skipImportantComment) +1552
   Microsoft.Ajax.Utilities.JSParser.ParseBlock() +164
   Microsoft.Ajax.Utilities.JSParser.ParseStatement(Boolean fSourceElement, Boolean skipImportantComment) +498
   Microsoft.Ajax.Utilities.JSParser.ParseLabeledStatement(Lookup lookup, Boolean fSourceElement) +543
   Microsoft.Ajax.Utilities.JSParser.ParseExpressionStatement(Boolean fSourceElement) +226
   Microsoft.Ajax.Utilities.JSParser.ParseStatement(Boolean fSourceElement, Boolean skipImportantComment) +1552
   Microsoft.Ajax.Utilities.JSParser.ParseBlock() +164
   Microsoft.Ajax.Utilities.JSParser.ParseStatement(Boolean fSourceElement, Boolean skipImportantComment) +498
   Microsoft.Ajax.Utilities.JSParser.ParseBlock() +164
   Microsoft.Ajax.Utilities.JSParser.ParseStatement(Boolean fSourceElement, Boolean skipImportantComment) +498
   Microsoft.Ajax.Utilities.JSParser.ParseStatements(Block block) +159
   Microsoft.Ajax.Utilities.JSParser.InternalParse() +1011
   Microsoft.Ajax.Utilities.JSParser.Parse(DocumentContext sourceContext) +123
   Microsoft.Ajax.Utilities.JSParser.Parse(DocumentContext sourceContext, CodeSettings settings) +54
   Microsoft.Ajax.Utilities.Minifier.MinifyJavaScript(String source, CodeSettings codeSettings) +987
   System.Web.Optimization.JsMinify.Process(BundleContext context, BundleResponse response) +310
   System.Web.Optimization.Bundle.ApplyTransforms(BundleContext context, String bundleContent, IEnumerable`1 bundleFiles) +263
   System.Web.Optimization.Bundle.GenerateBundleResponse(BundleContext context) +355
   System.Web.Optimization.Bundle.GetBundleResponse(BundleContext context) +104
   System.Web.Optimization.BundleResolver.GetBundleContents(String virtualPath) +254
   System.Web.Optimization.AssetManager.DeterminePathsToRender(IEnumerable`1 assets) +252
   System.Web.Optimization.AssetManager.RenderExplicit(String tagFormat, String[] paths) +75
   System.Web.Optimization.Scripts.RenderFormat(String tagFormat, String[] paths) +292
   System.Web.Optimization.Scripts.Render(String[] paths) +51
   ASP._Page_views_shared__LayoutHome_cshtml.Execute() in e:\HiTechMagic\iCollectIt\iCollectIt.CloudServices\iCollectit.Collector.WebApplication\Views\Shared\_LayoutHome.cshtml:41
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +271
   System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +120
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +145
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer) +41
   System.Web.WebPages.<>c__DisplayClass7.<RenderPageCore>b__6(TextWriter writer) +335
   System.Web.WebPages.HelperResult.WriteTo(TextWriter writer) +42
   System.Web.WebPages.WebPageExecutingBase.WriteTo(TextWriter writer, HelperResult content) +45
   System.Web.WebPages.WebPageBase.Write(HelperResult result) +53
   System.Web.WebPages.WebPageBase.RenderSurrounding(String partialViewName, Action`1 body) +178
   System.Web.WebPages.WebPageBase.PopContext() +347
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +154
   System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +695
   System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +382
   System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +431
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +39
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +116
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +529
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +106
   System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() +321
   System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +185
   System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +42
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +133
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +56
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +40
   System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +34
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +70
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +139
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40
   System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +44
   System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller) +39
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +62
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +139
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40
   System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +39
   System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +39
   System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +39
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +70
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +139
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +40
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +38
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9688704
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

我删除了所有代码和文件,直到我把它缩小到一个失败的例子:

BundleConfig.cs

bundles.Add(new ScriptBundle("~/bundles/ListView").Include(
           "~/Scripts/Vero/reorder.ts"));

重新排序.ts

module Plugins
{
    export interface IReorderOptions
    {
        onChange: { (): void; };         // Function to call after update
    }
}

母版页_LayoutHome.cshtml:

<!DOCTYPE html>
<html>
    [SNIP]
<body>
    [SNIP]

    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/bootstrap")
    @Scripts.Render("~/bundles/ListView")
    @RenderSection("scripts", required: false)
</body>
</html>

我的主页/索引引用母版页:

@{
    ViewBag.Title = "Home Page";
    Layout = "~/views/shared/_layoutHome.cshtml";
}

基本上,构建 JS 输出/捆绑包的代码似乎破坏了我的接口与函数的声明,但我找不到它的模式。

完整的插件在以前的 Web 应用程序中运行良好,所以我怀疑其中一个 NuGet/VS 工具已经改变。 reorder.js 输出文件明显是空的,所以看起来可能是 TypeScript 解析的。

我希望堆栈跟踪(上)和日志(下)提供线索。我希望这实际上是我遗漏了一些明显的东西,或者声明做错了。

输出日志(由于 SO 大小限制,最后一部分):

'C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\8d1fcf70\6d8beac0\App_Web_2rxc12vp.dll'. 
    'iisexpress.exe' (CLR v4.0.30319: /LM/W3SVC/44/ROOT-1-130403001817617541): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Dynamic\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Dynamic.dll'. Cannot find or open the PDB file.
    A first chance exception of type 'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException' occurred in Microsoft.CSharp.dll
    A first chance exception of type 'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException' occurred in Microsoft.CSharp.dll
    'iisexpress.exe' (CLR v4.0.30319: /LM/W3SVC/44/ROOT-1-130403001817617541): Loaded 'C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\8d1fcf70\6d8beac0\App_Web_fivluysz.dll'. 
    A first chance exception of type 'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException' occurred in Microsoft.CSharp.dll
    A first chance exception of type 'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException' occurred in Microsoft.CSharp.dll
    A first chance exception of type 'System.NullReferenceException' occurred in WebGrease.dll
    A first chance exception of type 'System.NullReferenceException' occurred in WebGrease.dll
    A first chance exception of type 'System.NullReferenceException' occurred in System.Web.Mvc.dll
    A first chance exception of type 'System.NullReferenceException' occurred in System.Web.Mvc.dll
    'iexplore.exe' (Script): Loaded 'Script Code (Windows Internet Explorer)'. 
    The program '[6296] iexplore.exe' has exited with code 0 (0x0).
    The program '[11616] iisexpress.exe' has exited with code 0 (0x0).

【问题讨论】:

    标签: visual-studio-2013 nuget typescript bundling-and-minification


    【解决方案1】:

    答:确保始终引用 .js 文件(而不是 .ts)!

    希望这个问题和答案可能对其他人有用犯了同样愚蠢的错误并得到同样无用的错误消息!

    我不小心引用了 .ts 文件,而不是捆绑包中的 .js 文件...仅在一个插件(损坏的插件)上。我所做的所有更改都无法解决现在非常明显的问题。

    天啊! :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-20
      • 1970-01-01
      • 1970-01-01
      • 2022-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多