3 Approach to achieve Async call in WCF

Approach One: using proxy client)

http://msdn.microsoft.com/en-us/library/ms730059.aspx

event-driven asynchronous calling model

Service Side

 [ServiceContract]
    public interface IService
    {
        [OperationContract]
        double Add(double a, double b);
    }

    public class Service : IService
    {
        public double Add(double a, double b)
        {
            return a + b;
        }
    }


Generate Asyn Proxy
C:\windows\system32>svcutil http://localhost:8000/mex /a /tcv:Version35

Client Side

   static void Main(string[] args)
        {
            Console.WriteLine("Press <ENTER> to terminate client once the output is displayed.");
            Console.WriteLine();

            // Create a client
            ServiceClient client = new ServiceClient();

            // AddAsync
            double value1 = 100.00D;
            double value2 = 15.99D;
            client.AddCompleted += new EventHandler<AddCompletedEventArgs>(AddCallback);
            client.AddAsync(value1, value2);
            Console.WriteLine("Add({0},{1})", value1, value2);
            Console.ReadLine();
        }

        // Asynchronous callbacks for displaying results.
        static void AddCallback(object sender, AddCompletedEventArgs e)
        {
            Console.WriteLine("Add Result: {0}", e.Result);
        }

Approach Two: Using Channel Factory

http://msdn.microsoft.com/en-us/library/bb885132.aspx

Service Side

 [ServiceContract]
    public interface IService
    {
        [OperationContract]
        double Add(double a, double b);
    }

    public class Service : IService
    {
        public double Add(double a, double b)
        {
            return a + b;
        }
    }


Generate Asyn Proxy
C:\windows\system32>svcutil http://localhost:8000/mex /a


Client Side

     static void Main(string[] args)
        {
            ChannelFactory<IServiceChannel> factory = new ChannelFactory<IServiceChannel>("BasicHttpBinding_IService");
            IServiceChannel channelClient = factory.CreateChannel();

            // BeginAdd
            double value1 = 100.00D;
            double value2 = 15.99D;

            IAsyncResult arAdd = channelClient.BeginAdd(value1, value2, AddCallback, channelClient);
            Console.WriteLine("Add({0},{1})", value1, value2);
            Console.ReadLine();
        }

        static void AddCallback(IAsyncResult ar)
        {
            double result = ((IServiceChannel)ar.AsyncState).EndAdd(ar);
            Console.WriteLine("Add Result: {0}", result);
        }
    }

Approach Three: Add Service Reference


Client Code:

 static void Main(string[] args)
        {
            ChannelFactory<IService> factory = new ChannelFactory<IService>("BasicHttpBinding_IService");
            IService channelClient = factory.CreateChannel();

            // BeginAdd
            double value1 = 100.00D;
            double value2 = 15.99D;

            IAsyncResult arAdd = channelClient.BeginAdd(value1, value2, AddCallback, channelClient);

            Console.WriteLine("Add({0},{1})", value1, value2);
            Console.ReadLine();
        }
        static void AddCallback(IAsyncResult ar)
        {
            double result = ((IService)ar.AsyncState).EndAdd(ar);
            Console.WriteLine("Add Result: {0}", result);
        }

OR

 static void Main(string[] args)
        {
            Console.WriteLine("Press <ENTER> to terminate client once the output is displayed.");
            Console.WriteLine();

            // Create a client
            ServiceClient client = new ServiceClient();

            // AddAsync
            double value1 = 100.00D;
            double value2 = 15.99D;
            client.AddCompleted += new EventHandler<AddCompletedEventArgs>(AddCallback);
            client.AddAsync(value1, value2);
            Console.WriteLine("Add({0},{1})", value1, value2);
            Console.ReadLine();
        }

        // Asynchronous callbacks for displaying results.
        static void AddCallback(object sender, AddCompletedEventArgs e)
        {
            Console.WriteLine("Add Result: {0}", e.Result);
        }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值