<configuration> <system.serviceModel> <behaviors> <serviceBehaviors> <behavior name="metadataBehavior"> <serviceMetadata httpGetEnabled="true" httpGetUrl="http://127.0.0.1:3721/calculatorservice/metadata"/> </behavior> </serviceBehaviors> </behaviors> <services> <service name="Service.CalculatorService" behaviorConfiguration="metadataBehavior"> <endpoint address="http://127.0.0.1:3721/calculatorservice" binding="wsHttpBinding" contract="Service.Interface.ICalculator"/> </service> </services> </system.serviceModel> </configuration>
五、客户端调用服务
点击OK之后会出现一系列的服务调用代码和配置信息。
在Client项目中直接通过添加的代理类调用寄宿的CalculatorService服务
并执行相应的方法调用服务操作
控制台输出结果
还有几种服务调用方式:
1.通过ChannelFactory<ICalculator>,调用CreateChannel方法穿件服务代理对象完成服务调用
using(ChannelFactory<ICalculator> channelFactory=new ChannelFactory<ICaiculator>(new WSHttpBinding(),"http://127.0.0.1:3721/calculatorservice")) { ICalculator icalculator=channelFactory.CreateChannel(); Console.WriteLine("x + y = {2} when x = {0} and y={1}",1,2,icalculator.Add(1,2)); //相关服务操作 }
2.在配置文件中配置相应的信息,通过ChannelFactory<ICalculator> calculatorChannel=new ChannelFactory<ICalculator>("calculatorservice"),读取结点为calculatorservice的终结点
<configuration> <system.serviceModel> <client> <endpoint name="calculatorservice" address="http://127.0.0.1:3721/calculatorservice" binding="wsHttpBinding" contract="Service.Interface.ICalculator"/> </client> </system.serviceModel> </configuration>
六、通过IIS寄宿服务,