转眼wcf技术已经出现很多年了,也在.net界混的风生水起,同时.net也是一个高度封装的框架,作为在wcf食物链最顶端的我们所能做的任务已经简单的不能再简单了,

再简单的话马路上的大妈也能写wcf了,好了,wcf最基本的概念我们放在后面慢慢分析,下面我们来看看神奇的3个binding如何KO我们实际场景中的80%的业务场景。

一:basicHttpBinding

  作为入门第一篇,也就不深入谈谈basic中的信道栈中那些啥东西了,你只需要知道有ABC三个要素,注意不是姨妈巾哦,如果需要详细了解,可以观赏我以前的系列。在

这里我就不多说了,太简单的东西没意思,先看个例子简单感受了,你只需知道的是basic走的是http协议就好了,传输消息为soap。

1. 契约

 using System.Runtime.Serialization;
using System.ServiceModel; namespace MyService
{
[ServiceContract]
public interface IHomeService
{
[OperationContract]
int GetLength(string name);
}
}

2. 实现类

 using System;
using System.Messaging;
using System.Threading; namespace MyService
{
public class HomeService : IHomeService
{
public int GetLength(string name)
{
return name.Length;
}
}
}

3. 服务启动

 using System;
using System.ServiceModel; namespace MyService
{
class Program
{
static void Main(string[] args)
{
using (ServiceHost host = new ServiceHost(typeof(HomeService)))
{
try
{
host.Open(); Console.WriteLine("服务开启!"); Console.Read();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}
}

4. 配置config文件

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="IHomeServiceBinding" />
</netTcpBinding>
</bindings> <behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors> <services>
<service name="MyService.HomeService">
<endpoint address="http://127.0.0.1:1920/HomeService" binding="basicHttpBinding" contract="MyService.IHomeService">
<identity>
<dns value="localhost" />
</identity>
</endpoint> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://127.0.0.1:1920"/>
</baseAddresses>
</host>
</service>
</services> </system.serviceModel>
</configuration>

5. 然后通过 servicehost 启动服务端

using System;
using System.ServiceModel; namespace MyService
{
class Program
{
static void Main(string[] args)
{
using (ServiceHost host = new ServiceHost(typeof(HomeService)))
{
try
{
host.Open(); Console.WriteLine("服务开启!"); Console.Read();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}
}

好了,到现在为止,服务端全部开启完毕,接下来我们通过“添加服务引用”,来添加对客户端的引用

 using System;

 namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
HomeServiceReference.HomeServiceClient client = new HomeServiceReference.HomeServiceClient(); var s = client.GetLength(""); Console.WriteLine("长度为:{0}", s); Console.Read();
}
}
}

麻蛋,就这么简单,是的,就这样简单的五步,基于http的通信就这样被不小心的完成了,真不好意思。

二:netTcpBinding

  有了basic的代码,现在我们要改成tcp通信,这会通信走的是字节流,很简单,改一下服务端的config文件就好了,大家也知道这种性能要比basic好。

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="mxbehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors> <services>
<service name="MyService.HomeService" behaviorConfiguration="mxbehavior">
<endpoint address="net.tcp://localhost:19200/HomeService" binding="netTcpBinding" contract="MyService.IHomeService">
<identity>
<dns value="localhost" />
</identity>
</endpoint> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
<host>
<baseAddresses>
<add baseAddress="http://localhost:1920/HomeService"/>
</baseAddresses>
</host>
</service>
</services> </system.serviceModel>
</configuration>

三:netMsmqBinding

  msmq这个玩意,我想大家都清楚,一个物理上的文件,好处呢,你也明白,就是client和service的所有通信都要经过它的手,这样任何一方出了问题,只要

它在就没问题了。同样我们把tcp改成msmq也是非常简单的,不过要注意,msmqbinding中是不可以让契约方法有返回值的。所以我们加上isoneway就好了。

using System.Runtime.Serialization;
using System.ServiceModel; namespace MyService
{
[ServiceContract]
public interface IHomeService
{
[OperationContract(IsOneWay = true)]
void GetLength(string name);
}
}

然后我在mmc上新建一个消息队列,如下:

然后我们再改动以下配置文件

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="mxbehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<netMsmqBinding>
<binding name="msmqbinding">
<security mode="None"/>
</binding>
</netMsmqBinding>
</bindings>
<services>
<service name="MyService.HomeService" behaviorConfiguration="mxbehavior">
<endpoint address="net.msmq://localhost/private/homequeue" binding="netMsmqBinding"
contract="MyService.IHomeService" bindingConfiguration="msmqbinding">
<identity>
<dns value="localhost" />
</identity>
</endpoint> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:19200/HomeService"/>
</baseAddresses>
</host>
</service>
</services> </system.serviceModel>
</configuration>

纵观上面的三种binding,配置起来何其简单,底层的各种通讯协议貌似对我来说都是透明的,其实呢???wcf在底层做了何其多的事情,而我却没有挖掘。。。

这对码农里说也是一种悲哀啊。。。出了问题就只能祷告上天。。。下一篇我会开始深入剖析。

[转]十五天精通WCF——第一天 三种Binding让你KO80%的业务的更多相关文章

  1. 十五天精通WCF——第一天 三种Binding让你KO80%的业务

    转眼wcf技术已经出现很多年了,也在.net界混的风生水起,同时.net也是一个高度封装的框架,作为在wcf食物链最顶端的我们所能做的任务已经简单的不能再简单了, 再简单的话马路上的大妈也能写wcf了 ...

  2. 十五天精通WCF——第八天 对“绑定”的最后一点理解

    转眼已经中断10几天没有写博客了,也不是工作太忙,正好碰到了端午节,然后最近看天津台的爱情保卫战入迷了...太好看了,一直都是回味无穷...而且 涂磊老师话说的真是tmd的经典,然后就这样耽搁了,好了 ...

  3. 十五天精通WCF——第五天 你需要了解的三个小技巧

    一: 服务是端点的集合 当你在开发wcf的时候,你或许已经注意到了一个service可以公布多个endpoint,确实是这样,在wcf中有一句很经典的话,叫做“服务是端点的集合",就 比如说 ...

  4. [转]十五天精通WCF——第八天 对“绑定”的最后一点理解

    转眼已经中断10几天没有写博客了,也不是工作太忙,正好碰到了端午节,然后最近看天津台的爱情保卫战入迷了...太好看了,一直都是回味无穷...而且 涂磊老师话说的真是tmd的经典,然后就这样耽搁了,好了 ...

  5. [转]十五天精通WCF——第五天 你需要了解的三个小技巧

    一: 服务是端点的集合 当你在开发wcf的时候,你或许已经注意到了一个service可以公布多个endpoint,确实是这样,在wcf中有一句很经典的话,叫做“服务是端点的集合",就 比如说 ...

  6. 十五天精通WCF——第十四天 一起聊聊FaultException

     我们在玩web编程的时候,可能你会不经意的见到一些http500的错误,我想你应该不会陌生的,原因你应该也知道,服务器异常嘛, 这时候clr会把这个未处理的异常抛给iis并且包装成http500的错 ...

  7. [转]十五天精通WCF——第十四天 一起聊聊FaultException

     我们在玩web编程的时候,可能你会不经意的见到一些http500的错误,我想你应该不会陌生的,原因你应该也知道,服务器异常嘛, 这时候clr会把这个未处理的异常抛给iis并且包装成http500的错 ...

  8. 十五天精通WCF——终结篇 那些你需要注意的坑

    终于一路走来,到了本系列的最后一篇了,这一篇也没什么好说的,整体知识框架已经在前面的系列文章中讲完了,wcf的配置众多,如果 不加一些指定配置,你可能会遇到一些灾难性的后果,快来一睹为快吧. 一: 第 ...

  9. 十五天精通WCF——第十一天 如何对wcf进行全程监控

    说点题外话,我们在玩asp.net的时候,都知道有一个叼毛玩意叫做“生命周期”,我们可以用httpmodule在先于页面的page_load中 做一些拦截,这样做的好处有很多,比如记录日志,参数过滤, ...

随机推荐

  1. codevs1369 xth 砍树(线段树)

    1369 xth 砍树  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond     题目描述 Description 在一个凉爽的夏夜,xth 和 rabbi ...

  2. [Swift通天遁地]八、媒体与动画-(3)实现视频播放的水印、Overlay、暂停时插入广告等效果

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  3. 微信小程序的wxml文件和wxss文件在webstrom的支持

    webstrom默认不支持wxml文件和wxss文件,所以要进入设置里面手动添加支持. 对wxml文件的支持: 文件 -> 设置 -> 编辑器 -> 文件类型, 然后选择XML文件, ...

  4. Android内存管理(11)*常见JVM回收机制「Java进程内存堆分代,JVM分代回收内存,三种垃圾回收器」

    参考: http://www.blogjava.net/rosen/archive/2010/05/21/321575.html 1,Java进程内存堆分代: 典型的JVM根据generation(代 ...

  5. vs2010 视图 aspx页面设计窗口创建控件时出错 未将对象引用设置到对象的实例

    第一步,首先关闭aspx页面 第二步,在单击项目右击,选择“清理” 第三步,然后在打开aspx页面,就可以看到正常的页面了. 注:一次不行的会,多做几次. 如果还是不行的话,你看看你.cs页面是否继承 ...

  6. Angular——表单指令

    基本介绍 这些指定只能针对input标签 基本使用 <!DOCTYPE html> <html lang="en"> <head> <me ...

  7. jQuery——事件操作

    事件绑定 1.简单事件绑定 $("button").click(function () {})//可重复绑定,不会被层叠 2.bind():不推荐使用 $("button ...

  8. RTL Compiler之Technology Library

    1 Target Library Design Compiler uses the target library to build a circuit. During mapping, Design ...

  9. Origin C调用GSL

    GSL (GNU Scientific Library, http://www.gnu.org/software/gsl/)是C.C++的数值算法库,提供了范围宽广的数学程序,包括随机数字生成器.数理 ...

  10. CAD计算两曲线间最短路径(com接口)

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...