title: Async/await call to asmx service
body: <p>I am consuming a asmx service and trying to do async/await while trying to call it</p>
  
  <p>Currently My controller is like below</p>
  
  <pre><code>Public ActionResult Index()
  {
     var data = _repository.GetDataFromAsmxService(somedata);
  }
  </code></pre>
  
  <p>//RepositoryClass Method</p>
  
  <pre><code>public List&lt;ObjectToReturn&gt; GetDataFromAsmxService(somedata)
  {
     var res = _asmxService.GetReportData(somedata);
     var result = process(res);
     return result;
  }
  </code></pre>
  
  <p>Upon further looking at refrence.cs file generated found there were void async and onCompleted methods for equivalent calls.</p>
  
  <p>So i changed it to</p>
  
  <pre><code>        private void GetReportDataCallBack(object sender, GetReportDataCompletedEventArgs e)
          {
              var res = e.Result;
              var result = process(res);
          }
  
          public List&lt;ObjectToReturn&gt; GetDataFromAsmxService(somedata)
  {
     _asmxService.GetReportDataCompleted +=GetReportDataCallBack;
     _asmxService.GetReportDataAsync(somedata);
  
  }
  </code></pre>
  
  <p>but now the callback happens to the Event delegate so my controller expecting a return data won’t get it. 
  <strong>what is a better way to call async/await from controller and get the data from asmx webservice</strong></p>
  
  <p>Code From Refenrece.cs
  //Generated code</p>
  
  <pre><code>[System.Web.Services.Protocols.SoapHeaderAttribute("ApiAuthenticationValue")]
          [System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://webservice.com/GetReportData", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Bare)]
          [return: System.Xml.Serialization.XmlElementAttribute(Namespace="http://webservice.com/")]
          public GetReportDataResponse GetReportData([System.Xml.Serialization.XmlElementAttribute(Namespace="http://webservice.com/")] GetReportDataRequest GetReportDataRequest) {
              object[] results = this.Invoke("GetReportData", new object[] {
                          GetReportDataRequest});
              return ((GetReportDataResponse)(results[0]));
          }
  
          /// &lt;remarks/&gt;
          public void GetReportDataAsync(GetReportDataRequest GetReportDataRequest) {
              this.GetReportDataAsync(GetReportDataRequest, null);
          }
  
          /// &lt;remarks/&gt;
          public void GetReportDataAsync(GetReportDataRequest GetReportDataRequest, object userState) {
              if ((this.GetReportDataOperationCompleted == null)) {
                  this.GetReportDataOperationCompleted = new System.Threading.SendOrPostCallback(this.OnGetReportDataOperationCompleted);
              }
              this.InvokeAsync("GetReportData", new object[] {
                          GetReportDataRequest}, this.GetReportDataOperationCompleted, userState);
          }
  
          private void OnGetReportDataOperationCompleted(object arg) {
              if ((this.GetReportDataCompleted != null)) {
                  System.Web.Services.Protocols.InvokeCompletedEventArgs invokeArgs = ((System.Web.Services.Protocols.InvokeCompletedEventArgs)(arg));
                  this.GetReportDataCompleted(this, new GetReportDataCompletedEventArgs(invokeArgs.Results, invokeArgs.Error, invokeArgs.Cancelled, invokeArgs.UserState));
              }
          }
  </code></pre>