费自托管WCF服务的整个过程,一步步地解释以及充足的编码和屏幕截图是非常有必要。
第1步:服务托管,现在我们需要实现的代理类客户端。创建代理的方式不同。
- 使用svcutil.exe,我们可以创建代理类和配置文件以及端点。
- 添加服务引用到客户端应用程序。
- 实现 ClientBase<T> 类
这三种方法,实现ClientBase<T>类是最好的做法。如果使用了两个rest方法,需要创建一个代理类,每一次当我们做出改变服务的实现。但是,这不是对ClientBase<T>类情况。这将创建代理只能在运行,所以它会打理一切。
为此,创建一个代理类,其中包括refrencesof System.ServiceModel和MyCalculatorService。


using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; using MyCalculatorService; namespace MyCalculatorServiceProxy { Public class MyCalculatorServiceProxy : //WCF create proxy for ISimpleCalculator using ClientBase ClientBase<ISimpleCalculator>, ISimpleCalculator { Public int Add(int num1, int num2) { //Call base to do funtion returnbase.Channel.Add(num1, num2); } } }
现在,创建一个控制台应用程序,其中包括System.ServiceModel和MyCalculatorServiceProxy的参考。


using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; using MyCalculatorServiceProxy; namespace MyCalculatorServiceClient { classProgram { Static void Main(string[] args) { MyCalculatorServiceProxy.MyCalculatorServiceProxy proxy = newMyCalculatorServiceProxy.MyCalculatorServiceProxy(); Console.WriteLine("Client is running at " + DateTime.Now.ToString()); Console.WriteLine("Sum of two numbers... 5+5 =" + proxy.Add(5, 5)); Console.ReadLine(); } } }
步骤2:结束点(相同服务)的信息应该被添加到客户端应用程序的配置文件。

1 <?xmlversion="1.0"encoding="utf-8" ?> 2 <configuration> 3 <system.serviceModel> 4 <client> 5 <endpoint address ="http://localhost:8090/MyCalculatorServiceProxy/ISimpleCalculator" 6 binding ="wsHttpBinding" 7 contract ="MyCalculatorServiceProxy.ISimpleCalculator"> 8 </endpoint> 9 </client> 10 </system.serviceModel> 11 </configuration>
步骤3:运行客户端应用程序之前,需要运行的服务。客户端应用程序的输出如下所示。
