天天看点

构建一个简单的WCF应用

买了《WCF技术剖析》,按着书本的例子进行操作,写下我的操作过程。

构建一个简单的WCF应用

using System;  

using System.Collections.Generic;  

using System.Linq;  

using System.Text;  

using System.ServiceModel;  

namespace xuwei.WcfServices.Contracts//服务契约  

{  

    [ServiceContract(Name="CalculatorService",Namespace="http://blog.csdn.net/xw13106209")]//将接口定义成服务契约  

    public interface ICalculator  

    {  

        [OperationContract]  

        double Add(double x,double y);  

        double Subtract(double x,double y);  

        double Multiply(double x,double y);  

        double Divide(double x, double y);  

    }  

}  

using xuwei.WcfServices.Contracts;  

namespace xuwei.WcfServices.Services//实现服务契约  

    public class CalculatorService:ICalculator  

        public double Add(double x,double y)  

        {  

            return x + y;  

        }  

        public double Subtract(double x, double y)  

            return x - y;  

        public double Multiply(double x, double y)  

            return x * y;  

        public double Divide(double x, double y)  

            return x / y;  

using xuwei.WcfServices.Services;  

using System.ServiceModel.Description;  

namespace xuwei.WcfServices.Hosting//自主服务寄宿  

    class Program  

        static void Main(string[] args)  

            using (ServiceHost host=new ServiceHost(typeof(CalculatorService)))  

            {  

                host.AddServiceEndpoint(typeof(ICalculator), new WSHttpBinding(), "http://127.0.0.1:9999/calculatorservice");  

                if (host.Description.Behaviors.Find<ServiceMetadataBehavior>() == null)  

                {  

                    ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();  

                    behavior.HttpGetEnabled = true;  

                    behavior.HttpGetUrl = new Uri("http://127.0.0.1:9999/calculatorservice/metadata");  

                    host.Description.Behaviors.Add(behavior);  

                }  

                host.Opened += delegate  

                    Console.WriteLine("CalculatorService已经启动,按任意键终止服务!");  

                };  

                host.Open();  

                Console.Read();  

            }  

完成以后需要编译Hosting下的program.cs。但是在通过Ctrl+F5执行(其实可以通过右键解决方案->生成解决方案完成,不需要通过Ctrl+F5执行)的时候可能报错:

无法直接启动带有“类库输出类型”的项目,如下图所示。

构建一个简单的WCF应用

这时我们需要右键Hosting,然后选择“设为启动项目”,再次执行就不会报错了。

在进行真正的WCF应用开发时,一般不会直接通过编码的方式进行终结点的添加和服务行为的定义,而是通过配置的方式进行。上面添加终结点和定义服务行为的代码可以通过如下方法进行。首先在Hosting项目中创建应用程序配置文件App.config,在App.config中添加如下配置:

<?xml version="1.0" encoding="utf-8" ?>  

<configuration>  

  <system.serviceModel>  

    <behaviors>  

      <serviceBehaviors>  

        <behavior name="metadataBehavior">  

          <serviceMetadata httpGetEnabled="true" httpGetUrl="http://127.0.0.1:9999/calculatorservice/metadata" />  

        </behavior>  

      </serviceBehaviors>  

    </behaviors>  

    <services>  

      <service behaviorConfiguration="metadataBehavior" name="xuwei.WcfServices.Services.CalculatorService">  

        <endpoint address="http://127.0.0.1:9999/calculatorservice" binding="wsHttpBinding" contract="xuwei.WcfServices.Contracts.ICalculator" />  

      </service>  

    </services>  

  </system.serviceModel>  

</configuration>  

如果采用了上诉的配置,服务寄宿代码将会得到极大的精简,只需包含下面几行代码:

                //host.AddServiceEndpoint(typeof(ICalculator), new WSHttpBinding(), "http://127.0.0.1:9999/calculatorservice");  

                //if (host.Description.Behaviors.Find<ServiceMetadataBehavior>() == null)  

                //{  

                //    ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();  

                //    behavior.HttpGetEnabled = true;  

                //    behavior.HttpGetUrl = new Uri("http://127.0.0.1:9999/calculatorservice/metadata");  

                //    host.Description.Behaviors.Add(behavior);  

                //}  

using xuwei.WcfService.Client.CalculatorServices;  

namespace xuwei.WcfService.Client  

            using (CalculatorServiceClient proxy = new CalculatorServiceClient())  

                Console.WriteLine("x+y={2} when x={0} and y={1}",1,2,proxy.Add(1,2));  

                Console.WriteLine("x-y={2} when x={0} and y={1}", 1, 2, proxy.Subtract(1, 2));  

                Console.WriteLine("x*y={2} when x={0} and y={1}", 1, 2, proxy.Multiply(1, 2));  

                Console.WriteLine("x/y={2} when x={0} and y={1}", 1, 2, proxy.Divide(1, 2));  

在执行步骤四以后E:/ms_workplace/WCF1/Hosting/bin/Debug目录下会有一个“Hosting.exe”的应用程序,双击打开该应用程序:

构建一个简单的WCF应用

然后右键Client项目,选择“添加服务引用”

构建一个简单的WCF应用

点击确定即可完成服务引用的添加,这时Client下就会多出一个Service Reference

构建一个简单的WCF应用

双击CalculatorServices,在对象浏览器中能够看到如下视图

构建一个简单的WCF应用

编译Client,会在E:/ms_workplace/WCF1/Client/bin/Debug有Client.exe,双击打开这个应用程序,会有如下结果:

构建一个简单的WCF应用

本文转自xwdreamer博客园博客,原文链接:http://www.cnblogs.com/xwdreamer/archive/2011/04/25/2297025.html,如需转载请自行联系原作者