构建一个简单的WCF应用

简介:

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

参考博客:http://www.cnblogs.com/artech/archive/2007/02/26/656901.html

步骤一 构建整个解决方案

 

步骤二 创建服务契约:ICalculator.cs

[c-sharp]  view plain copy print ?
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.ServiceModel;  
  6.   
  7. namespace xuwei.WcfServices.Contracts//服务契约  
  8. {  
  9.     [ServiceContract(Name="CalculatorService",Namespace="http://blog.csdn.net/xw13106209")]//将接口定义成服务契约  
  10.     public interface ICalculator  
  11.     {  
  12.         [OperationContract]  
  13.         double Add(double x,double y);  
  14.   
  15.         [OperationContract]  
  16.         double Subtract(double x,double y);  
  17.   
  18.         [OperationContract]  
  19.         double Multiply(double x,double y);  
  20.   
  21.         [OperationContract]  
  22.         double Divide(double x, double y);  
  23.   
  24.     }  
  25. }  

步骤三 创建服务:CalculatorService.cs

[c-sharp]  view plain copy print ?
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using xuwei.WcfServices.Contracts;  
  6.   
  7. namespace xuwei.WcfServices.Services//实现服务契约  
  8. {  
  9.     public class CalculatorService:ICalculator  
  10.     {  
  11.         public double Add(double x,double y)  
  12.         {  
  13.             return x + y;  
  14.         }  
  15.   
  16.         public double Subtract(double x, double y)  
  17.         {  
  18.             return x - y;  
  19.         }  
  20.   
  21.         public double Multiply(double x, double y)  
  22.         {  
  23.             return x * y;  
  24.         }  
  25.   
  26.         public double Divide(double x, double y)  
  27.         {  
  28.             return x / y;  
  29.         }  
  30.     }  
  31. }  

步骤四 通过自我寄宿的方式寄宿服务:Hosting控制台中的program.cs

[c-sharp]  view plain copy print ?
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using xuwei.WcfServices.Contracts;  
  6. using xuwei.WcfServices.Services;  
  7. using System.ServiceModel.Description;  
  8. using System.ServiceModel;  
  9.   
  10. namespace xuwei.WcfServices.Hosting//自主服务寄宿  
  11. {  
  12.     class Program  
  13.     {  
  14.         static void Main(string[] args)  
  15.         {  
  16.             using (ServiceHost host=new ServiceHost(typeof(CalculatorService)))  
  17.             {  
  18.                 host.AddServiceEndpoint(typeof(ICalculator), new WSHttpBinding(), "http://127.0.0.1:9999/calculatorservice");  
  19.                 if (host.Description.Behaviors.Find<ServiceMetadataBehavior>() == null)  
  20.                 {  
  21.                     ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();  
  22.                     behavior.HttpGetEnabled = true;  
  23.                     behavior.HttpGetUrl = new Uri("http://127.0.0.1:9999/calculatorservice/metadata");  
  24.                     host.Description.Behaviors.Add(behavior);  
  25.                 }  
  26.   
  27.                 host.Opened += delegate  
  28.                 {  
  29.                     Console.WriteLine("CalculatorService已经启动,按任意键终止服务!");  
  30.                 };  
  31.   
  32.                 host.Open();  
  33.                 Console.Read();  
  34.             }  
  35.         }  
  36.     }  
  37. }  

注意1:

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

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

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

注意2

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

[c-sharp]  view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <configuration>  
  3.   <system.serviceModel>  
  4.     <behaviors>  
  5.       <serviceBehaviors>  
  6.         <behavior name="metadataBehavior">  
  7.           <serviceMetadata httpGetEnabled="true" httpGetUrl="http://127.0.0.1:9999/calculatorservice/metadata" />  
  8.         </behavior>  
  9.       </serviceBehaviors>  
  10.     </behaviors>  
  11.     <services>  
  12.       <service behaviorConfiguration="metadataBehavior" name="xuwei.WcfServices.Services.CalculatorService">  
  13.         <endpoint address="http://127.0.0.1:9999/calculatorservice" binding="wsHttpBinding" contract="xuwei.WcfServices.Contracts.ICalculator" />  
  14.       </service>  
  15.     </services>  
  16.   </system.serviceModel>  
  17. </configuration>  

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

[c-sharp]  view plain copy print ?
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using xuwei.WcfServices.Contracts;  
  6. using xuwei.WcfServices.Services;  
  7. using System.ServiceModel.Description;  
  8. using System.ServiceModel;  
  9.   
  10. namespace xuwei.WcfServices.Hosting//自主服务寄宿  
  11. {  
  12.     class Program  
  13.     {  
  14.         static void Main(string[] args)  
  15.         {  
  16.             using (ServiceHost host=new ServiceHost(typeof(CalculatorService)))  
  17.             {  
  18.                 //host.AddServiceEndpoint(typeof(ICalculator), new WSHttpBinding(), "http://127.0.0.1:9999/calculatorservice");  
  19.                 //if (host.Description.Behaviors.Find<ServiceMetadataBehavior>() == null)  
  20.                 //{  
  21.                 //    ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();  
  22.                 //    behavior.HttpGetEnabled = true;  
  23.                 //    behavior.HttpGetUrl = new Uri("http://127.0.0.1:9999/calculatorservice/metadata");  
  24.                 //    host.Description.Behaviors.Add(behavior);  
  25.                 //}  
  26.   
  27.                 host.Opened += delegate  
  28.                 {  
  29.                     Console.WriteLine("CalculatorService已经启动,按任意键终止服务!");  
  30.                 };  
  31.   
  32.                 host.Open();  
  33.                 Console.Read();  
  34.             }  
  35.         }  
  36.     }  
  37. }  

步骤五 创建客户端调用服务:Client中的program.cs

[c-sharp]  view plain copy print ?
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using xuwei.WcfService.Client.CalculatorServices;  
  6. namespace xuwei.WcfService.Client  
  7. {  
  8.     class Program  
  9.     {  
  10.         static void Main(string[] args)  
  11.         {  
  12.             using (CalculatorServiceClient proxy = new CalculatorServiceClient())  
  13.             {  
  14.                 Console.WriteLine("x+y={2} when x={0} and y={1}",1,2,proxy.Add(1,2));  
  15.                 Console.WriteLine("x-y={2} when x={0} and y={1}", 1, 2, proxy.Subtract(1, 2));  
  16.                 Console.WriteLine("x*y={2} when x={0} and y={1}", 1, 2, proxy.Multiply(1, 2));  
  17.                 Console.WriteLine("x/y={2} when x={0} and y={1}", 1, 2, proxy.Divide(1, 2));  
  18.                 Console.Read();  
  19.             }  
  20.         }  
  21.     }  
  22. }  

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

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

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

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

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




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


目录
相关文章
|
数据库 数据安全/隐私保护
|
开发框架 数据安全/隐私保护
|
Web App开发 测试技术