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

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. chkconfig命令

    chkconfig --list                  #列出系统所有的服务启动情况chkconfig --add xxx           #增加xxx服务chkconfig --de ...

  2. linux 常用命令;

    bc 计算器 :quit 退出 top 任务管理器 q退出 who 显示在线用户 whoami 当前操作用户 kill id 结束当前进程 ifconfig 显示当前网络状态 clear 清屏 vim ...

  3. qt 提高ui响应度

    如何使Qt 平台中的GUI保持响应流畅?一般来说耗时较长的操作,分为计算密集型操作和IO密集型操作,对于这两类操作如何提高响应速度. 而从操作的本质上来说,操作又可分为不可分解操作,如在第三方库中耗时 ...

  4. 如何在Hadoop的MapReduce程序中处理JSON文件

    简介: 最近在写MapReduce程序处理日志时,需要解析JSON配置文件,简化Java程序和处理逻辑.但是Hadoop本身似乎没有内置对JSON文件的解析功能,我们不得不求助于第三方JSON工具包. ...

  5. 小记:获取post和get请求。

    package com.lixu.httpget_post; import java.io.ByteArrayOutputStream; import java.io.IOException; imp ...

  6. excel的变量

    因需要定制游戏的公式,公式是以一个系数乘以等级,我想达到修改系数,每个等级对应的值就立即显示出来, 但把系数写在一个单元格,一拉,系数单元格也会跟着增长行数--不是我想要的: 但只要把系数单元格改成变 ...

  7. IT公司100题-12-求1+2+…+n

    问题描述: 求1+2+…+n,要求不能使用乘除法.for.while.if.else.switch.case等关键字以及条件判断语句(A?B:C).   分析: 利用类的静态变量实现: new一含有n ...

  8. include指令和<jsp:include>标准动作

    利用JSP的包含机制,可以有效的避免重复,把可重用的部分独立出去,使用include把它们包含到当前文件.JSP有两种包含机制:include指令和<jsp:include>标准动作. 1 ...

  9. 端午小长假--前端基础学起来04CSS选择器

    定义: 选择器{ 样式: } 选择器指明{}中的样式的作用对象,即作用于网页中的哪些元素 <head><meta http-equiv="Content-Type" ...

  10. C++,1....n中随机等概率的输出m个不重复的数(假设n远大于m)。

    #include <stdlib.h> #include <time.h> knuth(int n, int m) { srand((unsigned )); ; i < ...