多个协定”示例演示如何在一个服务上实现多个协定,以及如何配置终结点以便与实现的每个协定进行通信

1.服务端代码如下(服务实现了两个协定,增加了黄色所示代码):

   class Program
{
static void Main(string[] args)
{
//创建一个ServiceHost
using (ServiceHost serviceHost = new ServiceHost(typeof(CalculatorService)))
{
// Open the ServiceHost to create listeners
serviceHost.Open();
Console.WriteLine("服务已经开启!");
Console.WriteLine("按回车键结束服务!");
Console.WriteLine();
Console.ReadLine();
}
} }
[ServiceContract]//定义服务协定完成
public interface ICalculator
{
[OperationContract]
double Add(double n1, double n2);
[OperationContract]
double Subtract(double n1, double n2);
[OperationContract]
double Multiply(double n1, double n2);
[OperationContract]
double Divide(double n1, double n2);
} [ServiceContract]
public interface ICalculatorSession
{
[OperationContract]
string test(string s);
} public class CalculatorService : ICalculator, ICalculatorSession
{
public double Add(double n1, double n2)
{
return n1 + n2;
} public double Subtract(double n1, double n2)
{
return n1 - n2;
} public double Multiply(double n1, double n2)
{
return n1 * n2;
} public double Divide(double n1, double n2)
{
return n1 / n2;
} public string test(string s)
{
return s;
}
}

2.服务端的配置(增加了黄色所示的终结点):

 <system.serviceModel>
<services>
<service name="Microsoft.ServiceModel.Samples.CalculatorService" behaviorConfiguration="returnFaults">
<endpoint address="http://localhost:8000/GIX4" binding ="customBinding"
bindingConfiguration="compactBindingConfig" contract="Microsoft.ServiceModel.Samples.ICalculator"/>
<endpoint address="basic" binding="basicHttpBinding" contract="Microsoft.ServiceModel.Samples.ICalculator"></endpoint>
<endpoint address="secure" binding="wsHttpBinding" contract="Microsoft.ServiceModel.Samples.ICalculator"></endpoint>
<endpoint address="session" binding="wsHttpBinding" contract="Microsoft.ServiceModel.Samples.ICalculatorSession"></endpoint>
<host>
<baseAddresses>
<add baseAddress="http://localhost:8000/GIX4"/>
</baseAddresses>
</host>
</service>
</services>
<bindings>
<customBinding>
<binding name="compactBindingConfig" receiveTimeout="00:20:00" sendTimeout="00:30:00">
<binaryMessageEncoding>
<readerQuotas maxDepth="" maxStringContentLength="" maxArrayLength="" maxBytesPerRead="" maxNameTableCharCount=""/>
</binaryMessageEncoding>
<httpTransport maxReceivedMessageSize="" />
</binding>
</customBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="returnFaults">
<serviceDebug includeExceptionDetailInFaults="true"/>
<!-- 为避免泄漏元数据信息,请在部署前将以下值设置为 false 并删除上面的元数据终节点-->
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>

3.客户端更新以下服务,然后对配置做适当的修改,如下黄色所示:

   <system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_ICalculator" />
</basicHttpBinding>
<customBinding>
<binding name="CustomBinding_ICalculator">
<binaryMessageEncoding />
<httpTransport />
</binding>
</customBinding>
<wsHttpBinding>
<binding name="WSHttpBinding_ICalculator" />
<binding name="WSHttpBinding_ICalculatorSession" />
</wsHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:8000/GIX4" binding="customBinding"
bindingConfiguration="CustomBinding_ICalculator" contract="Calculator.Service.ICalculator"
name="CustomBinding_ICalculator" />
<endpoint address="http://localhost:8000/GIX4/basic" binding="basicHttpBinding"
contract="Calculator.Service.ICalculator" name="basic" />
<endpoint address="http://localhost:8000/GIX4/secure" binding="wsHttpBinding"
contract="Calculator.Service.ICalculator" name="secure">
<identity>
<userPrincipalName value="chenlh@huawei" />
</identity>
</endpoint>
<endpoint address="http://localhost:8000/GIX4/session" binding="wsHttpBinding"
contract="Calculator.Service.ICalculatorSession" name="session">
<identity>
<userPrincipalName value="chenlh@huawei" />
</identity>
</endpoint>
</client>
</system.serviceModel>

4.客户端调用:

  class Program
{
static void Main(string[] args)
{
CalculatorClient client = new CalculatorClient("secure");
double n1 = 5.6;
double n2 = 7.3;
double result; result = client.Add(n2,n1);
Console.WriteLine("执行加法后的结果为:{0}", result.ToString()); result = client.Subtract(n2, n1);
Console.WriteLine("执行减法后的结果为:{0}", result.ToString()); result = client.Multiply(n1, n2);
Console.WriteLine("执行乘法后的结果为:{0}", result.ToString()); result = client.Divide(n1, n2);
Console.WriteLine("执行除法后的结果为:{0}", result.ToString()); CalculatorSessionClient clientSeesion = new CalculatorSessionClient();
string s = clientSeesion.test("你好我做一个测试!");
Console.WriteLine(s); Console.ReadLine(); }
}

WCF之多个协定的更多相关文章

  1. WCF基础之消息协定

    通常定义消息的架构,使用数据协定就够了,但是有时必须将类型精确映射到soap消息,方法两种:1.插入自定义soap标头:2.另一种是定义消息的头和正文的安全属性.消息协定通过MessageContra ...

  2. WCF基础之数据协定

    数据协定最重要的当然就是DataContract和DataMember.这两个特性能应用到类.结构和枚举.这个两个特性跟服务契约的特点是一样的,只有被DataContract标记的类和类中被标记Dat ...

  3. WCF初探-16:WCF数据协定之基础知识

    数据协定概念 “数据协定”是在服务与客户端之间达成的正式协议,用于以抽象方式描述要交换的数据. 也就是说,为了进行通信,客户端和服务不必共享相同的类型,而只需共享相同的数据协定. 数据协定为每一个做数 ...

  4. WCF学习心得------(六)数据协定

    --前言 最近各种事忙的把之前的WCF学习给耽误了一些,今天抽时间把之前的学习内容给总结了一下,因为知识点比较细碎没有做太多的练习示例,只是对其中关键的知识点做了总结,希望可以对大家有所帮助. 第六章 ...

  5. WCF系列教程之WCF服务协定

    本文参考自:http://www.cnblogs.com/wangweimutou/p/4422883.html,纯属读书笔记,加深记忆 一.服务协定简介: 1.WCF所有的服务协定层里面的服务接口, ...

  6. WCF入门, 到创建一个简单的WCF应用程序

    什么是WCF?  WCF, 英文全称(windows Communication Foundation) , 即为windows通讯平台. windows想到这里大家都知道了 , WCF也正是由微软公 ...

  7. 【WCF系列】(三)如何配置和承载服务

    如何配置和承载服务 配置绑定 配置服务:任务 为什么要配置服务:在设计和实现服务协定后,即可配置服务. 在其中可以定义和自定义如何向客户端公开服务指定可以找到服务的地址.服务用于发送和接收消息的传输和 ...

  8. WCF服务端开发和客户端引用小结

    1.服务端开发 1.1 WCF服务创建方式 创建一个WCF服务,总是会创建一个服务接口和一个服务接口实现.通常根据服务宿主的不同,有两种创建方式. (1)创建WCF应用程序 通过创建WCF服务应用程序 ...

  9. 实战MEF(1):一种不错的扩展方式

    在过去,我们完成一套应用程序后,如果后面对其功能进行了扩展或修整,往往需要重新编译代码生成新的应用程序,然后再覆盖原来的程序.这样的扩展方式对于较小的或者不经常扩展和更新的应用程序来说是可以接受的,而 ...

随机推荐

  1. java中的if-Switch选择结构

    字随笔走,笔随心走,随笔,随心.纯属个人学习分析总结,如有观者还请不啬领教. 1.if选择结构 什么是if结构:if选择结构是根据判断结果再做处理的一种语法结构. 起语法是: if(判断条件){ 操作 ...

  2. mysql 无法远程访问(授权也没办法,确切的说是因为只绑定了127IP)

    默认状况下,出于安全考虑,mysql数据库屏蔽了远程访问功能. 然而在许多状况下,你需要在家或者从web程序去访问远端数据库服务器,这就相当麻烦了. 第一步: 激活网络设置你需要编辑mysql配置文件 ...

  3. ANGULAR JS PROMISE使用

    Promise是一种模式,以同步操作的流程形式来操作异步事件,避免了层层嵌套,可以链式操作异步事件. 我们知道,在编写javascript异步代码时,callback是最最简单的机制,可是用这种机制的 ...

  4. dancing link 学习资源导航+心得

    dancing link简直是求解数独的神器,NOIP2009最后一题靶形数独,DFS 各种改变搜索顺序 都没法过,最后还是用了卡时过得.用dancing link写,秒杀所有数据,总时间才400ms ...

  5. 一模 (4) day2

    第一题: 题目大意:二进制数 n mod m 的结果是多少?  n 的长度(二进制数的位数)<=200 000:  m 的长度(二进制数的位数)<=20. 解题过程: 1.我的算法是直接高 ...

  6. php base64_decode 解码方法

    <?php header('Content-Type:text/html;charset=utf-8'); function encode_file_contents($filename) { ...

  7. 数列F[19] + F[13]的值

    已知数列如下:F[1]=1, F[2]=1, F[3]=5,......,F[n] =F[n-1] + 2*F[n-2],求F[19] + F[13]? #include <stdio.h> ...

  8. 推荐可以代替Visio的HTML开发的作图工具:ProcessOn

    过去作图的时候一直都是在用visio,每一次换了电脑使用都要重新安装,这大家都知道,最头疼的就是激活问题,曾经因为激活问题我“找遍了”正个互联网,最后还没找到...从08年开始到现在,visio用了这 ...

  9. JS获取上传文件的名称、格式、大小

    <input id="File1" type="file" onchange="checkFile(this)" /> 方式一) ...

  10. MapReduce数据流(一)

    在上一篇文章中我们讲解了一个基本的MapReduce作业由那些基本组件组成,从高层来看,所有的组件在一起工作时如下图所示: 图4.4高层MapReduce工作流水线 MapReduce的输入一般来自H ...