如何在使用 ServiceFabric 和 HttpClient 时查找使用时间的位置

How to find where time is used when using ServiceFabric and HttpClient

我想弄清楚我的应用程序中在哪里使用了时间。

ServerA 使用以下函数发送到 serverB:

protected async Task<T> SendRequest<T>(HttpRequestMessage request)
{
    using (var telemetryRequest = _telemetry.ExternalRequest("Outgoing call -> ", request.RequestUri.OriginalString, "", false))        // Creates a DependencyTelemetry
    {
        using (var response = await Client.SendAsync(request))
        {
            var data = await response.Content.ReadAsStringAsync();
            var returnObject = JsonConvert.DeserializeObject<T>(data, new JsonSerializerSettings
                {
                    Error = HandleDeserializationError
                });

            return returnObject;    
        }
    }
}

然后使用自定义中间件在 serverB 中接听电话:

public class MyCustomkMiddleware
{
    public async Task InvokeAsync(HttpContext context)
    {
        using (var request = _telemetryClient.InCommingRequest($"Incoming request: {context.Request.Method})            // Creates a RequestTelemetry
        {
            await _next.Invoke(context).ConfigureAwait(false);
        }
    }
}

启动时配置了中间件:

public void Configure(IApplicationBuilder app, ContextInitializer contextInitializer, )
{
    app.UseMiddleware<MyCustomkMiddleware>();
}

如果你查看日志,你会发现调用花费了很多时间,但我不太明白为什么。我的猜测是因为我们做了一些愚蠢的事情,所以启动和关闭时间很长。我怎样才能缩小范围或找到问题?

我一直不知道如何调试它,但速度变慢是由 services.AddScoped 引起的。将其更改为 services.AddSingleton() 大大提高了性能!