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

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. Tomcat数据库连接池的配置方法总结

    Tomcat数据库连接池的配置方法总结 数据库连接是一种关键的有限的昂贵的资源,这在多用户网页应用程序中体现的尤为突出.对数据库连接的管理能显著影响到整个应用程序的伸缩性和健壮性,影响到程序的性能指标 ...

  2. Windows Store App 应用程序存储空间

    与上面介绍的三种不同应用程序数据存储类型对应,应用程序有三种数据存储空间,分别为本地应用程序数据存储空间.漫游应用程序数据存储空间和临时应用程序数据存储空间.通过使用ApplicationData类的 ...

  3. Asp.Net MVC EF各版本区别

      2009年發行ASP.NET MVC 1.0版 2010年發行ASP.NET MVC 2.0版,VS2010 2011年發行ASP.NET MVC 3.0版+EF4,需要.Net4.0支持,VS2 ...

  4. JDE910笔记2--OMW项目建立及简单使用[转]

    1.打开JDE的OBJECT MANAGEMENT WORKBENCH.在工作区中选择ADD,建立项目并选择OMW PROJECT,添加相关信息,如下图所示 其中,ProjectID可以对应不同的数据 ...

  5. qml 一些知识点

    1.pagestack进行页面调整的时候,需要对页面状态做一些跟踪: Stack.onStatusChanged: { if (Stack.status == Stack.Active) { //可以 ...

  6. cf731E

    题意:一个游戏,由n张贴纸组成.贴纸排成一排,并且纸条上标有数字,玩家轮流揭下m张从左到右连续的纸条(m大等2),揭下后玩家得分累加这些纸条的sum,并且在剩下纸条最左边贴上新的纸条,数值为揭下纸条的 ...

  7. 把Angular中的$http变成jQuery.ajax()一样,可以让后台(php)轻松接收到参数

    最近接到一个手机项目,我决定用ionic + php + mysql来实现.ionic是一个前端框架,主要用于手机端,它融合了html5.css3.angularJS于一体,用起来很顺手. 开始构建项 ...

  8. NOIP2013 提高组day2 3 华容道 BFS

    描述 小 B 最近迷上了华容道,可是他总是要花很长的时间才能完成一次.于是,他想到用编程来完成华容道:给定一种局面,华容道是否根本就无法完成,如果能完成,最少需要多少时间. 小 B 玩的华容道与经典的 ...

  9. Linux-编译器gcc/g++编译步骤

    gcc和g++现在是gnu中最主要和最流行的c&c++编译器.g++是c++的命令,以.cpp为主:对于c语言后缀名一般为.c,这时候命令换做gcc即可.编译器是根据gcc还是g++来确定是按 ...

  10. C语言与MATLAB接口 编程与实例 李传军编着

    罗列一下以前自己学习C语言与MATLAB混编的笔记,顺便复习一遍. <C语言与MATLAB接口 编程与实例 李传军编着>(未看完,目前看到P106) 目录P4-8 ************ ...