【问题标题】:"Cannot implicitly convert type void..." getting this error from async method“无法隐式转换类型 void ...”从异步方法中获取此错误
【发布时间】:2016-07-21 17:52:51
【问题描述】:

当我尝试运行下面的代码时,我收到一条错误消息,提示“无法将类型 void 隐式转换为”Program.trainDataResult”。我只是想从异步方法返回我的 trainDataResult 对象,以便我可以使用它我的应用程序中的其他方法。有什么建议吗?当我指定它应该返回包装在任务中的 trainDataResult 对象时,我很困惑为什么异步方法返回 void。

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Xml.Serialization;
using System.Xml;
using System.IO;

namespace ctaapijim
{
   public class Program
   {
       [XmlRoot(ElementName = "ctatt", DataType = "string", IsNullable = true)]
       public class trainDataResult
       {
           [XmlElement(ElementName = "tmst")]
           public string timeStamp { get; set; }

           [XmlElement(ElementName = "errCd")]
           public byte errorCode { get; set; }

           [XmlElement(ElementName = "errNm")]
           public string errorName { get; set; }

           [XmlElement(ElementName = "eta")]
           public trainData eta { get; set; }
       }

       [Serializable()]
       public class trainData
       {
           [XmlElement(ElementName = "staId")]
           public ushort stationId { get; set; }

           [XmlElement(ElementName = "stpId")]
           public ushort stopId { get; set; }

           [XmlElement(ElementName = "staNm")]
           public string stationName { get; set; }

           [XmlElement(ElementName = "stpDe")]
           public string stopDesc { get; set; }

           [XmlElement(ElementName = "rn")]
           public ushort runNum { get; set; }

           [XmlElement(ElementName = "rt")]
           public string routeName { get; set; }

           [XmlElement(ElementName = "destSt")]
           public ushort destStation { get; set; }

           [XmlElement(ElementName = "destNm")]
           public string destName { get; set; }

           [XmlElement(ElementName = "trDr")]
           public byte trainDir { get; set; }

           [XmlElement(ElementName = "prdt")]
           public string prdTime { get; set; }

           [XmlElement(ElementName = "arrT")]
           public string arrTime { get; set; }

           [XmlElement(ElementName = "isApp")]
           public ushort isApp { get; set; }

           [XmlElement(ElementName = "isSch")]
           public ushort isSch { get; set; }

           [XmlElement(ElementName = "isDly")]
           public ushort isDly { get; set; }

           [XmlElement(ElementName = "isFlt")]
           public ushort isFlt { get; set; }

           [XmlElement(ElementName = "flags")]
           public string flags { get; set; }

           [XmlElement(ElementName = "lat")]
           public double lat { get; set; }

           [XmlElement(ElementName = "lon")]
           public double lon { get; set; }

           [XmlElement(ElementName = "heading")]
           public ushort heading { get; set; }
      }

      private const string URL = "http://lapi.transitchicago.com/api/1.0/ttarrivals.aspx?key=d94463bf32094ac6bb1417cd8f850a23&mapid=41300&max=1";

      static void Main(string[] args)
      {
            trainDataResult td = RunAsync().Wait();
      }
      static async Task<trainDataResult> RunAsync()
      {
          using (var client = new HttpClient())
          {                                  
              var response = await client.GetAsync(URL);
              if (response.IsSuccessStatusCode)
              {
                    //get data from CTA API
                  var xml = await response.Content.ReadAsStringAsync();
                  var ds = new XmlSerializer(typeof(trainDataResult), new XmlRootAttribute("ctatt"));
                  using (StringReader sr = new StringReader(xml))
                  {
                      using (XmlReader xr = XmlReader.Create(sr))
                      {
                          var trainDataResult = (trainDataResult)ds.Deserialize(xr);

                            return trainDataResult;
                            //calculate the difference in time between arrival time and current time
                           /* trainDataResult.eta.arrTime = trainDataResult.eta.arrTime.Substring(9);
                            TimeSpan trainTime = TimeSpan.Parse(trainDataResult.eta.arrTime);
                            var timeOfDay = DateTime.Now.TimeOfDay;
                            TimeSpan arrMins = (trainTime - timeOfDay);

                          Console.WriteLine("Station Name: {0}\nRoute Name: {1}\nArrival Time: {2}\nRun Number: {3}\nDirection: {4}", trainDataResult.eta.stationName, trainDataResult.eta.routeName, arrMins, trainDataResult.eta.runNum, trainDataResult.eta.stopDesc);
                          */                                               
                      }
                  }
              }
              else
              {
                  Console.WriteLine("There was an error!");
              }
           }
        }
    }
}

【问题讨论】:

  • 请显示您在哪一行出现错误,同时显示您的函数的其余部分。
  • 请提供minimal reproducible example。我无法理解发生了什么
  • @Shachaf.Gortler OP 在阅读有关 Task,.Wait 的 MSDN 文档时需要帮助,您建议的副本与帖子中的问题无关(不幸的是,代码显示不是 minimal reproducible example会更容易看到)。
  • Wait 不返回结果。你想要trainDataResult td = RunAsync().Result

标签: c# asynchronous return


【解决方案1】:

你不能有一个与其类型同名的局部变量;局部变量将“隐藏”类型的名称。

将您的班级名称从 trainDataResult 更改为 TrainDataResult,错误应该会清除。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-12
    • 2014-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-22
    • 1970-01-01
    相关资源
    最近更新 更多