从webservice调用异步方法

时间:2014-02-27 19:45:06

标签: c# web-services asynchronous async-await

我是处理Web服务的新手,我正在尝试实现一种方法,但我遇到了麻烦。这是我试图从中得到回复的方法:

(awaitable) Task<getIbuttonDataResponse> SpecSrvClient.getIbuttonDataAsync(GetIbuttonDataInput getIbuttonDataInput)

Usage:
getIbuttonDataResponse x = await getIbuttonDataAsync(...);

这是“GetIbuttonDataInput”

的WSDL生成代码
public partial class GetIbuttonDataInput : object, INotifyPropertyChanged {

    private string ibuttonIdField;

    private string testerNameField;

    [SoapElementAttribute(IsNullable=true)]
    public string ibuttonId {
        get {
            return this.ibuttonIdField;
        }
        set {
            this.ibuttonIdField = value;
            this.RaisePropertyChanged("ibuttonId");
        }
    }

    [SoapElementAttribute(IsNullable=true)]
    public string testerName {
        get {
            return this.testerNameField;
        }
        set {
            this.testerNameField = value;
            this.RaisePropertyChanged("testerName");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void RaisePropertyChanged(string propertyName) {
        System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
        if ((propertyChanged != null)) {
            propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
        }
    }
}

编辑:

对不起,我没有意识到我还没有发布图片,所以这让我搞砸了。

我的问题是,当我尝试从网络服务获得响应时,如何传递ibuttonIDField字符串?这是获得回复所需的价值。

编辑2:

好的,这就是我到目前为止所写的内容:

 static class Program
        {
        //...
        static void Main()
           {
            //...
            getIbuttonDataResponse x = new getIbuttonDataResponse();
            Response(serial, x);
            //...
            }
    static async void Response(string[] serial, getIbuttonDataResponse x)
          {
                iButtonDB_Service.SpecSrvClient testrequest = new SpecSrvClient();
                GetIbuttonDataInput input = new GetIbuttonDataInput { ibuttonId = serial[0], testerName = "me" };
                x = await testrequest.getIbuttonDataAsync(input);
           }
    }

我只获得null返回,但这可能是因为它还没有创建它试图访问的数据库值。

1 个答案:

答案 0 :(得分:1)

这一部分不会因为该方法被称为异步而改变。您仍然需要创建输入对象的实例,填充它,然后将其传递给服务调用:

GetIbuttonDataInput input = new GetIbuttonDataInput {ibuttonId ="x", testerName ="me"};
getIbuttonDataResponse x = await getIbuttonDataAsync(input);