This is a migrated thread and some comments may be shown as answers.

Style Sheet Registrar giving: "Cannot write to a closed TextWriter. " when it is inside of a partial view or template

3 Answers 198 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Mike
Top achievements
Rank 1
Mike asked on 01 Nov 2011, 04:46 AM
I am getting "Cannot write to a closed TextWriter. " when it is inside of a partial view or template. I just upgraded to the latest Telerik package and started getting this on code that was there prior. If I take the same stuff and put it directly in the head of the Master Page, then it works. I like to keep the stylesheet registrar in its own control due to some of the site's complexities.

Thanks,
Mike

3 Answers, 1 is accepted

Sort by
0
Mike
Top achievements
Rank 1
answered on 17 Nov 2011, 04:13 PM
Hey Telerik, is there anyone out there? It seems like I have posted quite a few times on this site and not one has been answered by you guys. Pretty sad since I am a proponent of your tools and I usually put them in my client's solutions. Would be nice if you guys answered every once in a while. Thanks.

Mike
0
Georgi Tunev
Telerik team
answered on 18 Nov 2011, 04:17 PM
Hi Mike,

The forums are a community resource and although we monitor them, we don't guarantee a timely response here. If you need a faster reply, you should use the support ticketing system. It assigns response time (according to the purchased license) to every issue that is submitted and forwards it to the corresponding teams.

As for the problem itself, if you still experience it, please provide a sample project so we can investigate further. We will check it and get back to you right away.

All the best,
Georgi Tunev
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the Telerik Extensions for ASP.MET MVC, subscribe to their blog feed now
0
jian
Top achievements
Rank 1
answered on 03 Oct 2012, 02:52 PM
I have a razor layout file that calls the partial view _MallCommonScripts.cshtml, 
I setup the ScriptRegistrar as following:

    var scripts = Html.Telerik().ScriptRegistrar()
        .Globalization(true)
        .jQuery(false).jQueryValidation(false) //disable the jQuery shipped with Telerik, which is of version 1.7.1
        .CombinedComponentFile(true)
        .DefaultGroup(
            g =>
            {
                g.Add("~/Scripts/jquery-1.8.2.min.js");
                g.Add("~/Scripts/jquery.validate.min.js");
                g.Add("~/Scripts/jquery.validate.unobtrusive.min.js");
 
                g.Combined(true);
                g.Compress(false);
            });
    scripts.Render(); //will cause "Cannot write to a closed TextWriter" exception
}


And I get the exception "Cannot write to a closed TextWriter".


[ObjectDisposedException: Cannot write to a closed TextWriter.]
   System.IO.StringWriter.Write(Char[] buffer, Int32 index, Int32 count) +14301681
   System.IO.TextWriter.WriteLine(String value) +204
   Telerik.Web.Mvc.UI.ScriptRegistrar.WriteScriptSources(TextWriter writer) +172
   Telerik.Web.Mvc.UI.ScriptRegistrar.Write(TextWriter writer) +31
   Telerik.Web.Mvc.UI.ScriptRegistrar.Render() +104
   ASP._Page_Views_Shared__MallCommonScripts_cshtml.Execute() in d:\Projects\EMall\devel\solution\src\Halcyon.Mall.Web\Views\Shared\_MallCommonScripts.cshtml:18
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +280
   System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +125
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +196
   System.Web.Mvc.Html.PartialExtensions.Partial(HtmlHelper htmlHelper, String partialViewName, Object model, ViewDataDictionary viewData) +147
   ASP._Page_Views_Shared__DbdLayout_cshtml.Execute() in d:\Projects\EMall\devel\solution\src\Halcyon.Mall.Web\Views\Shared\_DbdLayout.cshtml:37
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +280
   System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +125
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +196
   System.Web.WebPages.WebPageBase.Write(HelperResult result) +89
   System.Web.WebPages.WebPageBase.RenderSurrounding(String partialViewName, Action`1 body) +233
   System.Web.WebPages.WebPageBase.PopContext() +291
   System.Web.WebPages.WebPageBase.Write(HelperResult result) +89
   System.Web.WebPages.WebPageBase.RenderSurrounding(String partialViewName, Action`1 body) +233
   System.Web.WebPages.WebPageBase.PopContext() +291
   System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +380
   System.Web.Mvc.<>c__DisplayClass1a.<InvokeActionResultWithFilters>b__17() +33
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +613
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +263
   System.Web.Mvc.Async.<>c__DisplayClass25.<BeginInvokeAction>b__22(IAsyncResult asyncResult) +240
   System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__18(IAsyncResult asyncResult) +28
   System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +15
   System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +53
   System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +15
   System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__3(IAsyncResult asyncResult) +42
   System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +15
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +606
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +288

The exception is thrown on SOME pages ONLY, which I cannot tell the differences between the pages that throws or not.

The workaround is to change the last line of the ScriptRegistrar setup code from scripts.Render() to "@script":

//scripts.Render(); //will cause "Cannot write to a closed TextWriter" exception on SOME pages
@scripts    //This works for all pages

Wish this helps.
Tags
General Discussions
Asked by
Mike
Top achievements
Rank 1
Answers by
Mike
Top achievements
Rank 1
Georgi Tunev
Telerik team
jian
Top achievements
Rank 1
Share this question
or