简体   繁体   English

将Restsharp调用转换为异步

[英]Transform Restsharp call to asynchronous

I've this function : 我有这个功能:

public string GetSecurityCrumb()
    {
        string uri = $"{Url}/crumbIssuer/api/xml";// URL where the XML is available
        var client = new RestClient(uri); // define it as the actual client
        var request = new RestRequest(Method.GET);
        byte[] ua = Encoding.ASCII.GetBytes(Username + ":" + ApiToken); // Encoding username and token in base 64

        request.AddHeader("authorization", "Basic " + Convert.ToBase64String(ua));// adding header to get the xml
        IRestResponse response = client.Execute(request);
        XDocument document = XDocument.Parse(response.Content);// parsing the content of the response in a document

        var crumb = document.Root.Element("crumb").Value;// retrieve the content of the crumb only

        return crumb;
    }

I tried a lot of things to do this aynchronous, but I just don't see how I can return string value if I change my Rest call to an aynschronous one. 我尝试了很多方法来异步执行此操作,但是如果我将Rest调用更改为异步调用,就看不到如何返回字符串值。

Maybe somebody already got this kind of problem and could help me. 也许有人已经遇到了此类问题,可以为我提供帮助。

EDIT 1 编辑1

I tried this : 我尝试了这个:

public async Task<string> GetSecurityCrumb()
    {
        string uri = $"{Url}/crumbIssuer/api/xml";// URL where the XML is available
        var client = new RestClient(uri); // define it as the actual client
        var request = new RestRequest(Method.GET);
        byte[] ua = Encoding.ASCII.GetBytes(Username + ":" + ApiToken); // Encoding username and token in base 64

        request.AddHeader("authorization", "Basic " + Convert.ToBase64String(ua));// adding header to get the xml
        IRestResponse response = await client.ExecuteTaskAsync(request);
        XDocument document = XDocument.Parse(response.Content);// parsing the content of the response in a document

        var crumb = document.Root.Element("crumb").Value;// retrieve the content of the crumb only

        return crumb;
    }

but it seems like I need to put await before all my calls on this method and use GetSecurityCrumb().Result to get the real content. 但是似乎我需要在所有对该方法的调用之前等待,并使用GetSecurityCrumb()。Result获取真实内容。 I don't know if it's the best way because I've totally 0 error handlers at the moment. 我不知道这是否是最好的方法,因为目前我的错误处理程序总数为0。 A lot of my methods depend of this one so I prefer having the best solution 我的许多方法都依赖于此方法,因此我更喜欢拥有最佳解决方案

it seems like I need to put await before all my calls on this method and use GetSecurityCrumb().Result to get the real content 似乎我需要在所有对该方法的调用之前等待,并使用GetSecurityCrumb()。Result获取真实内容

You shouldn't call .Result - that would block on the asynchronous method, removing all the benefits of asynchronous code in the first place. 您不应该调用.Result这将阻塞异步方法,从而一开始就消除了异步代码的所有好处。

It's normal to await the task returned from GetSecurityCrumb() . await GetSecurityCrumb()返回的任务是正常的。 And using that await means that the calling method should also be marked async and return a task. 使用await意味着调用方法也应标记为async并返回任务。 Which means that its callers should use await , so they should be made async , etc. This is all perfectly normal and is the correct way to use async / await . 这意味着调用方应使用await ,因此应将它们设置为async等。 这完全正常,并且是使用async / await的正确方法

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

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