简体   繁体   English

异步调用Web服务方法

[英]Calling a webservice method asynchronously

I need to call the Register() method on a Web service asynchronously (using C#). 我需要异步调用Web服务上的Register()方法(使用C#)。 The implementation of the actual Register method in the Web service does not return any value. Web服务中实际Register方法的实现不返回任何值。 But it throws an exception if the method fails. 但是如果方法失败,它会引发异常。 Given this, does the following code look ok please? 鉴于此,以下代码看起来好吗?

public void RegisterProduct(string productName)
{
    await RegisterProductAsync();
}


private async static Task RegisterProductAsync(string productName)
{
    try
    { 
         await myWebService.Register(productName);   
    }
    catch(Exception ex)
    {
        LogException(ex);
    }
}

Thanks for your help. 谢谢你的帮助。

I will probably write the following code: 我可能会写下面的代码:

    private async static Task<int> RegisterProductAsync(string productName)
    {
        try
        {
            await Task.Run(() => myWebService.Register(productName));
            return 0;
        }
        catch (Exception ex)
        {
            LogException();
            return 1;
        }
    }

    public async Task<int> RegisterProduct(string productName)
    {
        var result = await RegisterProductAsync(productName);
        return result;
    }

Following the rule 'async' all the way. 一直遵循规则'异步'。

After adding the service, I guess you know a proxy file gets generated. 添加服务后,我猜您知道生成了代理文件。

In the proxy file you can make a note of associated async function, which returns System.Threading.Tasks.Task<xxxxxx> for return values. 在代理文件中,您可以记下关联的async函数,该函数返回System.Threading.Tasks.Task<xxxxxx>以获取返回值。

So in your case for Register , you can make a note of function RegisterAsync(string) in the proxy file. 因此,对于Register ,您可以在代理文件中记下函数RegisterAsync(string)

You can invoke that function (in your case: RegisterAsync(string )) for asynchronous call. 您可以调用该函数(在您的情况下: RegisterAsync(string ))进行异步调用。

myWebService.RegisterAsync(productName);

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

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