小编Dus*_*_00的帖子

如何删除StyleCop警告“此异步方法缺少'await'运算符,将同步运行”,而不会从签名中删除异步

父对象和大多数子对象具有异步功能并使用await。StyleCop正在观察,并针对一个儿童班缺乏等待的情况提出了建议。

当您无法删除异步签名时,使StyleCop开心的最佳方法是什么?

例如:

class Program
{
  static void Main(string[] args)
  {
     var t = DownloadSomethingAsync();

     Console.WriteLine(t.Result);
  }

  public delegate Task<string> TheDelegate(string page);

  static async Task<string> DownloadSomethingAsync()
  {
     string page = "http://en.wikipedia.org/";

     var content = await GetPageContentAsync(page);

     return content;
  }

  static async Task<string> GetPageContentAsync(string page)
  {
     string result;

     TheDelegate getContent = GetNotOrgContentAsync;
     if (page.EndsWith(".org"))
     {
        getContent = GetOrgContentAsync;
     }

     result = await getContent(page);

     return result;
  }

  static async Task<string> GetOrgContentAsync(string page)
  {
     string result;

     using (HttpClient client = new HttpClient())
     using …
Run Code Online (Sandbox Code Playgroud)

c# asynchronous stylecop async-await

0
推荐指数
1
解决办法
122
查看次数

标签 统计

async-await ×1

asynchronous ×1

c# ×1

stylecop ×1