Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

${aspnet-request-posted-body}: Use Async reading to prevent "Synchronous operations are disallowed" #549

Merged
merged 3 commits into from
May 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ public IActionResult Privacy()
return View();
}

[HttpPost]
public IActionResult AddItem(ItemModel model)
{
_logger.LogInformation("Added item {@Model}", model);
return View("Index");
}

[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ASP.NET_Core_3___VS2019.Models
{
public class ItemModel
{
public string Name { get; set; }
public string Value { get; set; }

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

<!-- another file log, only own logs. Uses some ASP.NET core renderers -->
<target xsi:type="File" name="ownFile-web" fileName="c:\temp\nlog-AspNetCore3-own-${shortdate}.log"
layout="${longdate}|${event-properties:item=EventId_Id:whenEmpty=0}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}|${callsite}" />
layout="${longdate}|${event-properties:item=EventId_Id:whenEmpty=0}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}|${callsite}| body: ${aspnet-request-posted-body}" />
</targets>

<!-- rules to map from logger name to target -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,10 @@
<div class="text-center">
<h1 class="display-4">Welcome</h1>
<p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>

<form asp-controller="Home" asp-action="AddItem" method="post">
<input name="Name" value="myName"/>
<input name="Value" value="myValue"/>
<button type="submit">Add</button>
</form>
</div>
7 changes: 7 additions & 0 deletions src/Shared/LayoutRenderers/AspNetRequestPostedBody.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.IO;
using System.Text;
using System.Threading;
using NLog.Common;
using NLog.Config;
using NLog.LayoutRenderers;
Expand All @@ -10,6 +11,7 @@
using System.Web;
using HttpRequest = System.Web.HttpRequestBase;
#else
using System.Threading.Tasks;
using HttpRequest = Microsoft.AspNetCore.Http.HttpRequest;
#endif

Expand Down Expand Up @@ -70,7 +72,12 @@ private static string BodyToString(Stream body)
// Note: don't dispose the StreamReader, it will close the stream and that's unwanted. You could pass that that
// to the StreamReader in some platforms, but then the dispose will be a NOOP, so for platform compat just don't dispose
var bodyReader = new StreamReader(body);

#if ASP_NET_CORE
var content = bodyReader.ReadToEndAsync().ConfigureAwait(false).GetAwaiter().GetResult();
#else
var content = bodyReader.ReadToEnd();
#endif
return content;
}
finally
Expand Down