首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >异步功能尚未完成,但没有收到错误消息。

异步功能尚未完成,但没有收到错误消息。
EN

Stack Overflow用户
提问于 2017-03-30 12:53:29
回答 1查看 322关注 0票数 1

背景

目前正在创建一个web,它需要与我创建的另一个web对话。这个有问题的web已经过时了,我唯一修改的mad是启用内核,并在其中添加了两个类。

问题

目前,这个web一直在执行到我的集成点,但是当它到达HttpResponseMessage hrm = await hc.GetAsync(requestUrl);时,它就停止了,不会给我一个错误。我试着在这个函数上添加一个try catch,但是它也忽略了这个tried。下面的代码将显示我如何构建这个web。

控制器

代码语言:javascript
运行
复制
    [Route("api/WorkingPaperServices/CreateWorkingPaper")]
    public IHttpActionResult CreateWorkingPaper()
    {
        try
        {
            log4net.Config.XmlConfigurator.Configure();
            bool complete = WorkingPaperCreator.StartWPCreation().Result; 
            return Ok("Finished");
        }
        catch(Exception ex)
        {
            log4net.LogManager.GetLogger("EmailLogger").Error(JsonConvert.SerializeObject(ex));
            return ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "Not Laoded"));
        }              
    }

WorkingPaperCreator - class

代码语言:javascript
运行
复制
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;

namespace CWAppServices.Services
{
    public class WorkingPaperCreator
    {
        public static async Task<bool> StartWPCreation()
        {
            try
            {
                var testing = await IntergrationPoints.GetClientInformation();
                return true;
            }
            catch(Exception ex)
            {
                return false;
            }
        } 
    }
}

IntergrationPoints - class (我的问题所在)

代码语言:javascript
运行
复制
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web;

namespace CWAppServices.Services
{
    public class IntergrationPoints
    {
        public static async Task<bool> GetClientInformation()
        {
            List<string> uniqueID = new List<string>();
            try
            {              
                string requestUrl = "http://appdev.tenant.com/caseware32/api/DataServices/GetAllClients";

                var action = new HttpMethod("GET");

                HttpClient hc = new HttpClient();

                HttpResponseMessage hrm = await hc.GetAsync(requestUrl);

                if (hrm.IsSuccessStatusCode)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                // TODO Log 
                return false;
            }
        }
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-03-30 12:58:06

这个阻塞调用导致问题:bool complete = WorkingPaperCreator.StartWPCreation().Result;。你会死锁。为了避免这种情况,可以使用aync\await无处不在(也使您的控制器异步)或向所有等待调用添加.ConfigureAwait(false)

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43118461

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档