如何在异步任务中返回结果

时间:2021-05-10 14:21:31

标签: c# asynchronous return task

我想将异步任务的值作为字符串返回,当我实现它时发生错误,错误消息 System.NullReferenceException was unhandled 消息:mscorlib.dll 中发生类型为“System.NullReferenceException”的未处理异常 附加信息:未将对象引用设置为对象的实例。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Http;
using System.Net;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;

namespace USCInventoryService.CLASS
{
    public static class RestFulHelper
    {
        private static readonly string baseURL = Properties.Settings.Default.Uri;

        public static string BeautifyJson(string jsonStr)
        {
            JToken parseJson = JToken.Parse(jsonStr);
            return parseJson.ToString(Formatting.Indented);
        }

        public static async Task<string> PostRFID(string token, string RFID, string status)
        {
            ServicePointManager.SecurityProtocol = (SecurityProtocolType)768 | (SecurityProtocolType)3072;
            var inputData = new Dictionary<string, string>
            {
                {"token",token },
                {"RFID",RFID },
                {"status", status}
            };

            var input = new FormUrlEncodedContent(inputData);

            using (HttpClient client = new HttpClient())
            {
                using (HttpResponseMessage res = await client.PostAsync(baseURL + "transaction/api", input))
                {
                    using (HttpContent content = res.Content)
                    {
                        string data = await content.ReadAsStringAsync();
                        if (data != null)
                        {
                            return data;
                        }
                    }
                }
            }
            return string.Empty;
        }

    }
}

Call in Service.asmx

    [WebMethod]
    public async Task<string> PostChkAccessPoint(string _token, string _RFID, string _status)
    {
        var response = await CLASS.RestFulHelper.PostRFID(_token, _RFID, _status);
        string _res = CLASS.RestFulHelper.BeautifyJson(response);
        return await Task.FromResult(_res);
    }

1 个答案:

答案 0 :(得分:0)

为什么不试试这个

....

var formFields = new List<KeyValuePair<string,string>> {
        new KeyValuePair<string,string>("token",token)),
        new KeyValuePair<string,string>("RFID",RFID )),
         new KeyValuePair<string,string>("status", status));

        var formContent = new FormUrlEncodedContent(formFields);


using (HttpClient client = new HttpClient())
            {
               var response = await client.PostAsync(baseURL + "/transaction/api", formContent ))
               
                    if (response.IsSuccessStatusCode)
                    {
                        return await response.content.ReadAsStringAsync();
                       
                    }
               
            }

或者你可以试试:

 var formFields  = new Dictionary<string, string>(){
     {"token",token },
                {"RFID",RFID },
                {"status", status}
            };

   
   var formContent = new StringContent(JsonConvert.SerializeObject(formFields), UnicodeEncoding.UTF8, "application/json");

还有这个

[WebMethod]
 public async Task<string> PostChkAccessPoint(string _token, string _RFID, string _status)
    {
   var responseString = await CLASS.RestFulHelper.PostRFID(_token, _RFID, _status);

       return CLASS.RestFulHelper.BeautifyJson(responseString);
}