Wpf Desktop Api调用持续时间比实际api响应时间长约8-10秒

问题描述 投票:0回答:1

这里是案例:我在约80个客户端上有wpf应用程序,它们通信单个.net框架api进行数据处理。我有一些秒表跟踪器,用于跟踪Wpf和api应用程序的持续时间。代码示例:

我的api属性:

public class DurationControlLoggerAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(HttpActionContext actionContext)
    {
        var controller = actionContext.ActionDescriptor.ControllerDescriptor.ControllerName;
        var action = actionContext.ActionDescriptor.ActionName;

        actionContext.ActionArguments.Add("_stopwatch_", Stopwatch.StartNew());
    }

    public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
    {
        if (ConfigurationManager.AppSettings["ApiLogger"].ToLower().Trim() != "true")
            return;

        var controller = actionExecutedContext.ActionContext.ActionDescriptor.ControllerDescriptor.ControllerName;
        var action = actionExecutedContext.ActionContext.ActionDescriptor.ActionName;

        var stopWatch = (Stopwatch)actionExecutedContext.ActionContext.ActionArguments["_stopwatch_"];

        stopWatch.Stop();

        var scope = actionExecutedContext.ActionContext.Request.GetDependencyScope();
        var commonService = (ICommonService)scope.GetService(typeof(ICommonService));
        commonService.InsertLog(new Model.Common.LogModel
        {
            InsertDate = DateTime.Now.ToString(),
            LogLevel = "API",
            MachineName = "API",
            Message = $"Controller : {controller} - Action : {action} - TotalSeconds : {stopWatch.Elapsed.TotalSeconds}",
            StackTrace = string.Empty
        });
    }
}

动作示例:

    [HttpPost]
    [DurationControlLogger]
    public bool InsertProduct(ProductModel model)
    {
        return _mainService.TracingService.InsertProduct(model);
    }

此操作过程的持续时间约为0,03秒。另一方面,wpf api调用持续时间约为10秒。 WPF代码块如下:

            var Stopwatch = Stopwatch.StartNew();
            var isSuccess = DataHelper.InsertProduct(Product);
            Stopwatch.Stop();
            if (Stopwatch.Elapsed.TotalSeconds > 2)
                DataHelper.InsertTraceLog($"ProducrtBusiness - InsertProduct TotalSecond : {Stopwatch.Elapsed.TotalSeconds}");

DataHelper.InsertProduct方法执行基本的http发布请求。代码在这里:

public static class HttpClientHelper
{
    public static T Post<T>(object model, string url)
    {
        var resultStatus = false;
        T resultData = default(T);

        using (var client = new HttpClient())
        {

            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));


            var content = new StringContent(JsonConvert.SerializeObject(model), Encoding.UTF8, "application/json");

            HttpResponseMessage response = client.PostAsync(url, content).Result;

            if (response.IsSuccessStatusCode)
            {
                string data = response.Content.ReadAsStringAsync().Result;
                var result = JsonConvert.DeserializeObject<T>(data);
                resultStatus = response.StatusCode == System.Net.HttpStatusCode.OK;
                resultData = result;
            }
        }
        return resultStatus ? resultData : default(T);
    }
    .....

有人对此情况有任何想法吗?

c# wpf api request duration
1个回答
0
投票

嗯,很可能(从我所看到的来看)您正在主消息泵(UI线程)上运行此代码。在此方法调用期间,您将执行异步调用(PostAsync)并调用“结果”-使它同步运行。但是,当您的UI线程正在等待时,其他一些消息(UI更新?)在消息泵上排队,并在您调用结束秒表(更高的DispatcherPriority?)之前被处理-延迟计时器结束。这纯粹是推测,但我建议您在异步代码中使用一些async / await,并尝试在单独的线程上执行这些数据访问任务]

© www.soinside.com 2019 - 2024. All rights reserved.