wcf使用task实现异步调用

private async void btnGetEmployees_Click(object sender, RoutedEventArgs e)
{
txtInfo.Text = "Data is Not Received Yet....";
MyRef.ServiceClient Proxy = new MyRef.ServiceClient();
var Result = await Proxy.GetEmployeesAsync();
dgEmp.ItemsSource = Result;
txtInfo.Text = "Data Received....";
}

自定义客户

If you open the proxy class, the method ‘GetEmployeeAsync’ is generated as follows:

public System.Threading.Tasks.Task<WPF_Client.MyRef.EmployeeInfo[]>
GetEmployeesAsync()
{
return base.Channel.GetEmployeesAsync();
}

或参考

http://www.codeproject.com/Articles/613678/Task-based-Asynchronous-Operation-in-WCF

class Program
 {
    static void Main(string[] args)
    {
       GetResult();
       Console.ReadLine();
    }

    private async static void GetResult()
    {
       var client = new Proxy("BasicHttpBinding_IMessage");
       var task = Task.Factory.StartNew(() => client.GetMessages("Hello"));
       var str = await task;
       str.ContinueWith(e =>
       {
          if (e.IsCompleted)
           {
              Console.WriteLine(str.Result);
           }
       });
      Console.WriteLine("Waiting for the result");
    }
 }


public Task<string> GetMessages(string msg)
    {
      return Channel.GetMessages(msg);
    }
原文地址:https://www.cnblogs.com/zeroone/p/5218498.html