繁体   English   中英

如果我想返回WebClient的结果该怎么用

[英]What to use if i want to return the result of a webclient

采取以下代码:

    public async Task<string> AuthenticatedGetData(string url, string token)
    {
        WebClient client = new WebClient();
        client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(WebClient_DownloadStringCompleted);
        client.DownloadStringAsync(new Uri(url + "?oauth_token=" + token));
    }

    private void WebClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        string response = e.Result;    
    }

WebClient_DownloadStringCompleted被调用...并且response =我想要的响应...很好。 完善...

现在考虑我如何调用此AuthenticatedGetData方法:

它是从一种存储库中调用的...存储库需要一个字符串,以便它可以序列化并对生成的对象进行处理...

因此,所有内容都从存储库开始异步运行...对该身份验证的getdata进行了调用,然后发出了一个请求... ...但是,因为downloadstringasync没有.Result()方法,并且因为downloadstringcompleted需要调用void方法。 ..我无法将结果字符串返回到调用存储库。

关于必须做什么才能获得client.DownloadStringAsync以在完成时返回响应字符串的任何想法?

是我只需要将我的数据访问操作紧密地耦合到该特定应用程序即可。.似乎是如此..可重复使用的:(我真的想使我的整个身份验证工作与即将发生的事情完全分开。不想为每个存储库重复上面的代码,因为无论如何它都一样!

编辑:://

我在我的类中创建了一个抽象方法来处理上述请求...,然后用我的存储库扩展该类并实现抽象方法。 听起来不错?

按要求编辑://呼叫代码:

public class OrganisationRepository
{
    PostRequest postRequest;

    public OrganisationRepository()
    {
        this.postRequest = new PostRequest();
    }
    public IEnumerable<Organisation> GetAll()
    {
        string requestUrl = BlaBla.APIURL + "/org/";
        string response = postRequest.AuthenticatedGetData(requestUrl, BlaBla.Contract.AccessToken).Result;
    }
}

public class PostRequest
{

    public Task<string> AuthenticatedGetData(string url, string token)
    {

        TaskCompletionSource<string> tcs = new TaskCompletionSource<string>();

        WebClient client = new WebClient();
        client.DownloadStringCompleted += (sender, e) =>
        {
            if (e.Error != null)
            {
                tcs.TrySetException(e.Error);
            }
            else if (e.Cancelled)
            {
                tcs.TrySetCanceled();
            }
            else
            {
                tcs.TrySetResult(e.Result);
            }
        };
        client.DownloadStringAsync(new Uri(url + "?oauth_token=" + token));
        return tcs.Task;
    }
}

我不确定Windows Phone 8对此有何限制。 但是我认为这应该可行。

public Task<string> AuthenticatedGetData(string url, string token)
    {
        TaskCompletionSource<string> tcs = new TaskCompletionSource<string>();

        WebClient client = new WebClient();
        client.DownloadStringCompleted += (sender, e) =>
        {
            if (e.Error != null)
            {
                tcs.TrySetException(e.Error);
            }
            else if (e.Cancelled)
            {
                tcs.TrySetCanceled();
            }
            else
            {
                tcs.TrySetResult(e.Result);
            }            
        };
        client.DownloadStringAsync(new Uri(url + "?oauth_token=" + token));
        return tcs.Task;
    }

您也许也可以解决这个问题(不确定它是否可以在Windows Phone 8上运行)

public Task<string> AuthenticatedGetData(string url, string token)
    {
        WebClient client = new WebClient();
        return client.DownloadStringTaskAsync(new Uri(url + "?oauth_token=" + token));

    }

一个解决方案是根本不使用WebClient库。 问题出在这里:您正在等待异步的AuthenticatedGetData任务,但是它没有任何作用,因为该任务中没有等待调用,这意味着它将立即完成。 在任务上调用.result始终为null,因为您永远无法在该方法中返回值。 触发DownloadCompleted事件后,WebClient依赖于调用函数调用。 但是,除非有人也订阅那个DownloadCompleted事件处理程序,这是很愚蠢的,否则任何人都无法确切知道何时发生这种情况。 我建议使用HttpWebRequest http://blogs.msdn.com/b/andy_wigley/archive/2013/02/07/async-and-await-for-http-networking-on-windows-phone制作真正的异步Web请求服务.aspx祝您好运。

暂无
暂无

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

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