码迷,mamicode.com
首页 > 其他好文 > 详细

WCF的异步调用

时间:2014-10-30 00:09:15      阅读:231      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   color   os   ar   sp   文件   

1.服务契约

namespace Contracts
{
    [ServiceContract]
   public interface ICalculator
    {
        [OperationContract]
       double Add(double x,double y);
    }
}

2.服务实现

namespace Services
{
   public class CalculatorService:ICalculator
    {
       public double Add(double x, double y)
       {
           return x + y;
       }
    }
}

3.宿主程序

namespace Hosting
{
    class Program
    {
        static void Main(string[] args)
        {

                //通过配置文件获取相应的信息
                using (ServiceHost host = new ServiceHost(typeof(CalculatorService)))
                {
                    
                    host.Opened += delegate
                    {
                        Console.WriteLine("服务已经启动,按任意键停止");
                    };
                    host.Open();
                    Console.Read();
                }
            
        }
    }
}

4.服务配置文件

<configuration>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="metadataBehavior">
          <serviceMetadata httpGetEnabled="true" httpGetUrl="http://127.0.0.1:6666/calculatorservice/metadata"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service behaviorConfiguration="metadataBehavior" name="Services.CalculatorService">
        <endpoint address="http://127.0.0.1:6666/calculatorservice" binding="wsHttpBinding" contract="Contracts.ICalculator"  ></endpoint>
      </service>
    </services>
  </system.serviceModel>
</configuration>

5.添加服务引用

bubuko.com,布布扣

bubuko.com,布布扣

6.客户端调用(说明:利用异步方式执行服务操作,使得服务在执行过程中不会阻塞主线程,当方法执行完成后,通过AsyncCallback回调对应的方法,可以通知客户端服务执行完毕,从而使proxy.EndAdd()方法不会处于阻塞等待状态,而是等阻塞proxy.BeginAdd()执行完了再执行

namespace Client
{
    class Program
    {
        static void Main(string[] args)
        {
            //通过回调的方式进行异步服务调用
            //通过上面的方式进行异步调用有一个不好的地方,就是当EndAdd方法被执行的时候
            //如果异步执行的方法Add没有执行结束的话,该方法将会阻塞当前线程并等待异步
            //方法的结束,这样往往不能起到多线程并发执行应有的作用。我么真正希望的是在
            //异步执行结束后自动回调设定的操作,这样就可以采用回调的方式来实现这样的机制。
            ServiceCalculator.CalculatorClient proxy = new Client.ServiceCalculator.CalculatorClient();
            proxy.BeginAdd(1, 2, delegate(IAsyncResult asyncResult)
            {double result = proxy.EndAdd(asyncResult);
                proxy.Close();
                Console.WriteLine("x+y={2} when x={0} and y={1}", operands[0], operands[1], result);
            }, null);
            Console.Read();
        }
    }
}

 

WCF的异步调用

标签:style   blog   http   io   color   os   ar   sp   文件   

原文地址:http://www.cnblogs.com/yingger/p/4060762.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!