小编Dar*_*agh的帖子

如何正确测量 ASP.NET Core 请求持续时间?

给出以下最基本的 ASP.NET Core 应用程序(注意 Thread.Sleep):

public class Program
{
    public static void Main(string[] args)
    {
        CreateHostBuilder(args).Build().Run();
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.Configure(appBuilder =>
                    appBuilder.Run(async context =>
                    {
                        var stopwatch = Stopwatch.StartNew();
                        Thread.Sleep(1000);
                        await context.Response.WriteAsync($"Finished in {stopwatch.ElapsedMilliseconds} milliseconds.");
                    }));
            });
}
Run Code Online (Sandbox Code Playgroud)

以及以下 appsettings.json

{
  "Logging": {
    "LogLevel": {
      "Default": "None",
      "Microsoft.AspNetCore.Hosting.Diagnostics" : "Information",
    }
  },
  "AllowedHosts": "*"
}
Run Code Online (Sandbox Code Playgroud)

如果我运行中等负载测试(100 个请求,在我的例子中使用庞巴迪),我会看到大约 5 秒的延迟。

~/go/bin/bombardier http://localhost:5000 -l -n 100 -t 60s
Bombarding http://localhost:51568 with 100 request(s) using …
Run Code Online (Sandbox Code Playgroud)

asp.net asp.net-core

7
推荐指数
1
解决办法
3099
查看次数

为什么AsyncLocal <T>传播到子线程,而ThreadLocal <T>不传播?

我不得不在我的代码中使用AsyncLocal替换ThreadLocal的使用,以便在等待异步操作时保持"环境状态".

但是,AsyncLocal令人讨厌的行为是它"流动"到子线程.这与ThreadLocal不同.反正有没有阻止这个?

class Program
{
    private static readonly ThreadLocal<string> ThreadState = new ThreadLocal<string>();
    private static readonly AsyncLocal<string> AsyncState = new AsyncLocal<string>();

    static async Task MainAsync()
    {
        ThreadState.Value = "thread";
        AsyncState.Value = "async";

        await Task.Yield();

        WriteLine("After await: " + ThreadState.Value); 
        WriteLine("After await: " + AsyncState.Value); // <- I want this behaviour

        Task.Run(() => WriteLine("Inside child task: " + ThreadState.Value)).Wait(); // <- I want this behaviour
        Task.Run(() => WriteLine("Inside child task: " + AsyncState.Value)).Wait();
Run Code Online (Sandbox Code Playgroud)

.net c# multithreading asynchronous

6
推荐指数
1
解决办法
1722
查看次数

在控制台应用程序中使用WindowsFormsSynchronizationContext时,异步/等待死锁

作为一项学习练习,我试图重现以常规Windows形式但使用控制台应用程序发生的异步/等待死锁。我希望下面的代码会导致这种情况的发生,的确如此。但是使用await时,死锁也会意外发生。

using System;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
static class Program
{
    static async Task Main(string[] args)
    {
        // no deadlocks when this line is commented out (as expected)
        SynchronizationContext.SetSynchronizationContext(new WindowsFormsSynchronizationContext()); 
        Console.WriteLine("before");
        //DoAsync().Wait(); // deadlock expected...and occurs
        await DoAsync(); // deadlock not expected...but also occurs???
        Console.WriteLine("after");
    }
    static async Task DoAsync()
    {
        await Task.Delay(100);
    }
}
Run Code Online (Sandbox Code Playgroud)

我最好奇是否有人知道为什么会这样?

c# async-await

6
推荐指数
1
解决办法
101
查看次数

如何在 Deno 中发送 UDP 数据?

我尝试在我的measure.ts脚本中使用以下代码:

Deno.DatagramConn.send(...)
Run Code Online (Sandbox Code Playgroud)

当我像这样运行我的脚本时:deno run --unstable --allow-all measure.ts我收到以下错误:

Property 'DatagramConn' does not exist on type 'typeof Deno'. 'Deno.DatagramConn' is an 
unstable API. Did you forget to run with the '--unstable' flag?
Run Code Online (Sandbox Code Playgroud)

Deno.DatagramConn这个错误似乎同时否认和确认了API的存在

同样我也尝试过

Deno.connect({transport : 'udp'})
Run Code Online (Sandbox Code Playgroud)

但这给了我以下错误(这可能是有意义的,因为 UDP 是“无连接”):

Type '"udp"' is not assignable to type '"tcp"
Run Code Online (Sandbox Code Playgroud)

udp deno

6
推荐指数
1
解决办法
1160
查看次数

是否有与xsd.exe等效的JSON?

我可以使用xsd.exe为MyDataContract.dll程序集生成XSD

xsd.exe MyDataContract.dll
Run Code Online (Sandbox Code Playgroud)

这将生成一个schema0.xsd,其中包含程序集中所有类型的定义.

是否有JSON的等价物?

我看过Newtonsoft Json.NET Schema,但这似乎只提供了一个API,而不是一个可执行文件.此外,它似乎在"类型"级别工作,无法为程序集中的所有类型生成JSON模式.

我想生成JSON模式作为我的CI构建的一部分.

我想我可以使用带有反射等的Json.Net Schema来构建一个控制台应用程序来实现这一点,但令人惊讶的是没有这样的程序已经存在.

.net schema json xsd.exe json.net

5
推荐指数
1
解决办法
362
查看次数

如何使用WebApi将POSTHttpRoute POST到自定义操作?

我试图弄清楚Web API路由背后的疯狂.

当我尝试发布这样的数据时:

curl -v -d "test" http://localhost:8088/services/SendData
Run Code Online (Sandbox Code Playgroud)

我收到404,并出现以下错误消息:

{"Message":"No HTTP resource was found that matches the request URI 'http://localhost:8088/services/SendData'.","MessageDetail":"No action was found on the controller 'Test' that matches the request."}
Run Code Online (Sandbox Code Playgroud)

这是我的测试服务器的代码.

public class TestController : ApiController
{

    [HttpPost]
    public void SendData(string data)
    {
        Console.WriteLine(data);
    }
}

class Program
{
    static void Main(string[] args)
    {

        var config = new HttpSelfHostConfiguration("http://localhost:8088");

        config.Routes.MapHttpRoute(
            name: "API Default",
            routeTemplate:"services/SendData",
            defaults: new { controller = "Test", action = "SendData"},
            constraints: null);

        using (var server = …
Run Code Online (Sandbox Code Playgroud)

asp.net asp.net-web-api

4
推荐指数
1
解决办法
6747
查看次数