Skip to content

Commit e0d274e

Browse files
Handle sync exceptions within async context
1 parent 55c8dd2 commit e0d274e

File tree

8 files changed

+52
-1
lines changed

8 files changed

+52
-1
lines changed

src/Mvc/Mvc.ViewFeatures/src/ViewExecutor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ protected async Task ExecuteAsync(
233233

234234
OnExecuting(viewContext);
235235

236-
using (var writer = WriterFactory.CreateWriter(response.Body, resolvedContentTypeEncoding))
236+
await using (var writer = WriterFactory.CreateWriter(response.Body, resolvedContentTypeEncoding))
237237
{
238238
var view = viewContext.View;
239239

src/Mvc/test/Mvc.FunctionalTests/TagHelpersTest.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,16 @@ public async Task CanRenderViewsWithTagHelpers(string action)
6464
#endif
6565
}
6666

67+
[Fact]
68+
public async Task GivesCorrectCallstackForSyncronousCalls()
69+
{
70+
// Arrange, Act and Assert
71+
var exception = await Assert.ThrowsAsync<HttpRequestException>(async () => await Client.GetAsync("http://localhost/Home/MyHtml"));
72+
73+
// Assert
74+
Assert.Equal("Should be visible", exception.InnerException.InnerException.Message);
75+
}
76+
6777
[Fact]
6878
public async Task CanRenderViewsWithTagHelpersAndUnboundDynamicAttributes_Encoded()
6979
{

src/Mvc/test/WebSites/TagHelpersWebSite/Controllers/HomeController.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ public IActionResult Help()
3535
return View();
3636
}
3737

38+
public IActionResult MyHtml()
39+
{
40+
return View();
41+
}
42+
3843
public IActionResult ViewComponentTagHelpers()
3944
{
4045
return View();
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System.IO;
5+
using System.Text.Encodings.Web;
6+
using Microsoft.AspNetCore.Html;
7+
using Microsoft.AspNetCore.Mvc.Rendering;
8+
9+
namespace TagHelpersWebSite
10+
{
11+
public class MyHtmlContent : IHtmlContent
12+
{
13+
private IHtmlHelper Html { get; }
14+
15+
public MyHtmlContent(IHtmlHelper html)
16+
{
17+
Html = html;
18+
}
19+
20+
public void WriteTo(TextWriter writer, HtmlEncoder encoder)
21+
{
22+
#pragma warning disable MVC1000 // Use of IHtmlHelper.{0} should be avoided.
23+
Html.Partial("_Test").WriteTo(writer, encoder);
24+
#pragma warning restore MVC1000 // Use of IHtmlHelper.{0} should be avoided.
25+
}
26+
}
27+
}

src/Mvc/test/WebSites/TagHelpersWebSite/Startup.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System.IO;
55
using Microsoft.AspNetCore.Builder;
6+
using Microsoft.AspNetCore.Diagnostics;
67
using Microsoft.AspNetCore.Hosting;
78
using Microsoft.AspNetCore.Mvc;
89
using Microsoft.Extensions.DependencyInjection;
@@ -20,6 +21,7 @@ public void ConfigureServices(IServiceCollection services)
2021

2122
public void Configure(IApplicationBuilder app)
2223
{
24+
app.UseDeveloperExceptionPage();
2325
app.UseRouting();
2426
app.UseStaticFiles();
2527
app.UseEndpoints(endpoints =>

src/Mvc/test/WebSites/TagHelpersWebSite/TagHelpersWebSite.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@
1212

1313
<ItemGroup>
1414
<Reference Include="Microsoft.AspNetCore.Mvc" />
15+
<Reference Include="Microsoft.AspNetCore.Diagnostics" />
1516
<Reference Include="Microsoft.AspNetCore.Server.IISIntegration" />
1617
<Reference Include="Microsoft.AspNetCore.Server.Kestrel" />
1718
<Reference Include="Microsoft.AspNetCore.StaticFiles" />
19+
<Reference Include="Microsoft.Extensions.Logging.Console" />
20+
<Reference Include="Microsoft.Extensions.Logging.Debug" />
1821
</ItemGroup>
1922
</Project>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@(new TagHelpersWebSite.MyHtmlContent(Html))
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@{
2+
throw new Exception("Should be visible");
3+
}

0 commit comments

Comments
 (0)