繁体   English   中英

HttpClient.PostAsync终止程序,退出代码为0

[英]HttpClient.PostAsync terminates program with exit code 0

环顾四周,没有找到任何答案。.我确实在这里找到了与我相同的问题的帖子,但是它不能解决我的问题。 (HttpClient.PostAsync退出退出代码为0的应用程序)

当我运行此代码时,vendorAddress的帖子起作用。 但是当我要发布PaymentTerms时,该程序将终止于postAsync函数,而不会出现错误消息,代码或任何其他内容。 我不明白为什么它对一个有用,但对另一个不起作用。

我已经使用了相同的网址和json文本,并使用邮差插件在Chrome中进行了发布。 这两个电话都可以工作,我可以得到结果。

我已将帖子更改为使用WebClient,并且都可以正常工作并且可以得到结果。 但我需要在代码中保留HTTPClient服务。

有什么建议么?

static void Main(string[] args)
    {
       RunAsync().Wait();
    }

    static async Task RunAsync()
    {

        try
        {
            // works
            var result = await enconPostData("vendorAddress", JsonVendorAdd);

            // does not work.  fails on postAsync
            var result1 = enconPostData("PaymentTerms", null);
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }           
    }

    static public async Task<string> enconPostData(string datatype, Object[] jsObject)
    {
        ////jsObject is a json string/object////

        string jsonString = null, URIaddress = null;

        switch (datatype)
        {                
            case "vendorAddress":
                // Create Json Object to post
                //EnVendors enconvend = new EnVendors();
                EnVendors envend = new EnVendors();

                envend.data = (Vendor[])jsObject;

                URIaddress = baseUrl + "api/CONTACTS/UpdateXXXXXX";

                // Serialize to a JsonString
                jsonString = JsonConvert.SerializeObject(enconvend);                 
                break;

            case "PaymentTerms":                   
                ContractInput entermdate = new ContractInput();
                //Set JsonObject here with dates
                entermdate.DateFrom = new DateTime(2016, 10, 1);
                entermdate.DateTo = new DateTime(2016, 10, 30);


                URIaddress = baseUrl + "api/PaymentTerms/ActiveXXXXXX";

                // Serialize to a JsonString
                jsonString = JsonConvert.SerializeObject(entermdate);

                break;
        }

        return await PostAsync(URIaddress, jsonString);
    }

    static public async Task<string> PostAsync(string uri, string jsonString)
    {

        // Post to API Call
        using (var Client = new HttpClient())
        {                   
        /////////
        /// program aborts here at PostAsync on PaymentTerms Call.   works fine for vendorAddress
       ////////
            var response = await Client.PostAsync(uri, new StringContent(jsonString, Encoding.UTF8, "application/json"));
            //will throw an exception if not successful
            response.EnsureSuccessStatusCode();

            string content = await response.Content.ReadAsStringAsync();
            return await Task.Run(() => content);

        }
    }

好吧,我在这里查看我的帖子时发现了我的问题。 我设置了一个断点,所以断点的红色使我很难看到问题。

在我的示例的第22行上,var result1 = enconPostData(“ PaymentTerms”,null);

缺少等待命令var result1 =等待enconPostData(“ PaymentTerms”,null);

一旦我添加了..我得到了我的结果,并且程序没有终止。

同步呼叫与异步呼叫

谢谢大家..我只需要一个新的视角。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM