c#从异步函数返回数据

时间:2016-04-19 06:00:27

标签: c# string asynchronous void

我有两种方法

public void GetEmp()
{
    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new System.Uri("http://sdw2629/empservice/EmployeeInfo.svc/Employee"));
    request.Method = "GET";
    request.ContentType = "application/json; charset=utf-8";

    request.BeginGetResponse(new AsyncCallback(ReadWebRequestCallback), request);  

}

private void ReadWebRequestCallback(IAsyncResult callbackResult)
{
    HttpWebRequest myRequest = (HttpWebRequest)callbackResult.AsyncState;
    using (HttpWebResponse myResponse = (HttpWebResponse)myRequest.EndGetResponse(callbackResult))
    {
        using (StreamReader httpwebStreamReader = new StreamReader(myResponse.GetResponseStream()))
        {
            string results = httpwebStreamReader.ReadToEnd();
            //execute UI stuff on UI thread.              
        }
    }
}  

这里我想将一个字符串“results”返回给其他类似的方法

string data= obj1.GetEmp()

我怎样才能实现这一目标..任何帮助都会受到影响..谢谢

2 个答案:

答案 0 :(得分:5)

最简单的方法是使用async重写方法,如下所示:

public async Task<string> GetEmpAsync()
{
    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new System.Uri("http://sdw2629/empservice/EmployeeInfo.svc/Employee"));
    request.Method = "GET";
    request.ContentType = "application/json; charset=utf-8";
    var response = await request.GetResponseAsync();
    using (StreamReader httpwebStreamReader = new StreamReader(response.GetResponseStream()))
    {
        string results = await httpwebStreamReader.ReadToEndAsync();
        //execute UI stuff on UI thread.
        return results;
    }

}

然后你可以用这样的代码得到结果:

var results = await GetEmpAsync();

如果您使用的是旧版本且没有async,则可以通过阻止方式获取结果:

public string GetEmpBlocking()
{
    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new System.Uri("http://sdw2629/empservice/EmployeeInfo.svc/Employee"));
    request.Method = "GET";
    request.ContentType = "application/json; charset=utf-8";
    var response = request.GetResponse();
    using (StreamReader httpwebStreamReader = new StreamReader(response.GetResponseStream()))
    {
        string results = httpwebStreamReader.ReadToEnd();
        //execute UI stuff on UI thread.
        return results;
    }
}

得到这样的结果:

var results = GetResultBlocking();

Р.S。您也可以考虑在早期版本中使用Microsoft.Bcl.Async来支持async

答案 1 :(得分:2)

只需使用

即可
request.BeginGetResponse(...);

而不是

public string GetEmp()
{
    var request = (HttpWebRequest)HttpWebRequest.Create(new System.Uri(@"http://.../data.json"));
    request.Method = "GET";
    request.ContentType = "application/json; charset=utf-8";

    var resp = request.GetResponse();

    using (var httpWebStreamReader = new StreamReader(resp.GetResponseStream()))
    {
        var result = httpWebStreamReader.ReadToEnd();
        return result;
    }
}

使用同步GetResonse,您可以直接返回字符串。

Route::post('companyLogin','Auth\AuthController@companyLogin');


Route::group(['middleware' => 'auth'], function () {

Route::get('dashboard',['uses' => 'PageController@dashboard' ,  'as' => 'dashboard']);

});