消费自托管WCF服务的整个过程,一步步地解释以及充足的编码和屏幕截图是非常有必要。
第1步:服务托管,现在我们需要实现的代理类客户端。创建代理的方式不同。
这三种方法,实现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); |
} |
} |
} |
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(); |
} |
} |
} |
<?xmlversion="1.0"encoding="utf-8" ?> |
<configuration> |
<system.serviceModel> |
<client> |
<endpoint address ="http://localhost:8090/MyCalculatorServiceProxy/ISimpleCalculator" |
binding ="wsHttpBinding" |
contract ="MyCalculatorServiceProxy.ISimpleCalculator"> |
</endpoint> |
</client> |
</system.serviceModel> |
</configuration> |