1

我有一个网络服务,我在其中异步发布到 URL

public Response uploadXMLData(string destinationUrl, string requestXml,Request req)
{
    try
    {
        Response resp=new Response();
        System.Uri uri = new System.Uri(destinationUrl);
        using (WebClient client = new WebClient())
        {

            client.UploadStringCompleted
       += new UploadStringCompletedEventHandler(UploadStringCallback); 

            client.UploadStringAsync(uri, "POST",requestXml,req);
        }
    }

    catch (Exception ex)
    {}

    return resp;
}

public void UploadStringCallback(object sender, UploadStringCompletedEventArgs e)
{
    Response resp=new Response();
    Request req = new Request();
    try 
    {
        if (e.Error != null)
        {
            object objException = e.Error.GetBaseException();

            Type _type = typeof(WebException);
            if (_type != null)
            {
                WebException objErr = (WebException)e.Error.GetBaseException();
                WebResponse rsp = objErr.Response;
                using (Stream respStream = rsp.GetResponseStream())
                {
                   req= (Request)e.UserState;
                   resp=Utilities.ParseWithoutSoapEnv(respStream);

                }

            }
            else
            {
                Exception objErr = (Exception)e.Error.GetBaseException();
                throw objErr;
            }
        }
        else
        { 
        //Parse e.Result
        }
    }
    catch (Exception ex)
    {}
}

被调用的函数Utilities.ParseWithoutSoapEnv(respStream);返回类型Response

我想要做的是从该函数获取响应,并将其作为返回值uploadXMLData

但是我无法更改回调函数的返回类型,所以我不知道该怎么做。

当有人调用我的 webservice 函数时,他们希望它返回一个类型Response,并且Response我需要的类被接收到 CallBack 函数..

希望我把我的问题说清楚了

任何帮助,将不胜感激

4

1 回答 1

0

如果您使用任务异步模式HttpClient. 您需要修改方法以返回Task<Response>

public async Task<Response> UploadXmlDataAsync(
                 string destinationUrl, 
                 string requestXml,
                 Request req)
{
    try
    {
        Response resp=new Response();
        System.Uri uri = new System.Uri(destinationUrl);

        var httpClient = new HttpClient();
        var response = await httpClient.PostAsync(Uri, new StringContent(requestxml))
                               .ConfigureAwait(false);
        var responeStream = response.Content.ReadAsStreamAsync().ConfigureAwait(false);

        return Utilities.ParseWithoutSoapEnv(responseStream);
    }
}

当你在调用堆栈中调用它时,你也需await要这样做:

public async Task FooAsync()
{
    var parsedResponse = await UploadXmlDataAsync(url, xml, request);
    // Do something with response.
}

如果你想要一个同步的替代方案,你也可以这样做:

public Response UploadXmlData(string destinationUrl, string requestXml,Request req)
{
    Response resp=new Response();
    System.Uri uri = new System.Uri(destinationUrl);
    using (WebClient client = new WebClient())
    {
        var response = client.UploadString(uri, "POST", requestXml, req);
        using (var responseStream = new MemoryStream(Encoding.UTF8.GetBytes(response))
        {
            return Utilities.ParseWithoutSoapEnv(responseStream);
        }
    }
}
于 2015-07-16T07:49:29.823 回答