我想弄清楚我的应用程序在哪里使用时间。
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()大大提高了性能!