化零为整WCF(3) - 绑定Binding(basicHttpBinding和netTcpBinding)

简介:
[索引页]
[源码下载] 


化零为整WCF(3) - 绑定Binding(basicHttpBinding和netTcpBinding)


作者: webabcd


介绍
WCF(Windows Communication Foundation) - 绑定Binding:Http以basicHttpBinding为例,Tcp以netTcpBinding为例。


示例
1、服务
IHello.cs
InBlock.gif using System; 
InBlock.gif using System.Collections.Generic; 
InBlock.gif using System.Linq; 
InBlock.gif using System.Text; 
InBlock.gif 
InBlock.gif using System.ServiceModel; 
InBlock.gif 
InBlock.gif namespace WCF.ServiceLib.Binding 
InBlock.gif
InBlock.gif         /// <summary> 
InBlock.gif         /// IHello接口 
InBlock.gif         /// </summary> 
InBlock.gif        [ServiceContract] 
InBlock.gif         public  interface IHello 
InBlock.gif        { 
InBlock.gif                 /// <summary> 
InBlock.gif                 /// 打招呼方法 
InBlock.gif                 /// </summary> 
InBlock.gif                 /// <param name="name">人名</param> 
InBlock.gif                 /// <returns></returns> 
InBlock.gif                [OperationContract] 
InBlock.gif                 string SayHello( string name); 
InBlock.gif        } 
InBlock.gif}
 
Hello.cs
InBlock.gif using System; 
InBlock.gif using System.Collections.Generic; 
InBlock.gif using System.Linq; 
InBlock.gif using System.Text; 
InBlock.gif 
InBlock.gif using System.ServiceModel; 
InBlock.gif 
InBlock.gif namespace WCF.ServiceLib.Binding 
InBlock.gif
InBlock.gif         /// <summary> 
InBlock.gif         /// Hello类 
InBlock.gif         /// </summary> 
InBlock.gif         public  class Hello : IHello 
InBlock.gif        { 
InBlock.gif                 /// <summary> 
InBlock.gif                 /// 打招呼方法 
InBlock.gif                 /// </summary> 
InBlock.gif                 /// <param name="name">人名</param> 
InBlock.gif                 /// <returns></returns> 
InBlock.gif                 public  string SayHello( string name) 
InBlock.gif                { 
InBlock.gif                         return  "Hello: " + name; 
InBlock.gif                } 
InBlock.gif        } 
InBlock.gif}
 

2、宿主
Hello.cs
InBlock.gif using System; 
InBlock.gif using System.Collections.Generic; 
InBlock.gif using System.Linq; 
InBlock.gif using System.Text; 
InBlock.gif 
InBlock.gif using System.ServiceModel; 
InBlock.gif 
InBlock.gif namespace WCF.ServiceHost2.Binding 
InBlock.gif
InBlock.gif         class Hello 
InBlock.gif        { 
InBlock.gif                 static  void Main( string[] args) 
InBlock.gif                { 
InBlock.gif                         using (ServiceHost host =  new ServiceHost( typeof(WCF.ServiceLib.Binding.Hello))) 
InBlock.gif                        { 
InBlock.gif                                 // 写代码的方式做host 
InBlock.gif                                 // host.AddServiceEndpoint(typeof(WCF.ServiceLib.Binding.IHello), new NetTcpBinding(), "net.tcp://localhost:54321/Binding/Hello"); 
InBlock.gif                                host.Open(); 
InBlock.gif 
InBlock.gif                                Console.WriteLine( "服务已启动"); 
InBlock.gif                                Console.WriteLine( "按<ENTER>停止服务"); 
InBlock.gif                                Console.ReadLine(); 
InBlock.gif                        } 
InBlock.gif                } 
InBlock.gif        } 
InBlock.gif}
 
App.config
<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <system.serviceModel> 
        <services> 
            <!--name - 提供服务的类名--> 
            <!--behaviorConfiguration - 指定相关的行为配置--> 
            <service name="WCF.ServiceLib.Binding.Hello" behaviorConfiguration="BindingBehavior"> 
                <!--address - 服务地址--> 
                <!--binding - 通信方式--> 
                <!--contract - 服务契约--> 
                <!--<endpoint binding="basicHttpBinding" contract="WCF.ServiceLib.Binding.IHello" address="Hello" />--> 
                <endpoint binding="netTcpBinding" contract="WCF.ServiceLib.Binding.IHello" address="net.tcp://localhost:54321/Binding/Hello" /> 
                <!--元数据交换的endpoint--> 
                <!--注:address是mex,它会和host/baseAddresses节点中的baseAddress做拼接,即提供元数据交换的地址为:http://localhost:12345/Binding/mex--> 
                <endpoint binding="mexHttpBinding" contract="IMetadataExchange" address="mex" /> 
                <host> 
                    <baseAddresses> 
                        <add baseAddress="http://localhost:12345/Binding/"/> 
                    </baseAddresses> 
                </host> 
            </service> 
        </services> 
        <behaviors> 
            <serviceBehaviors> 
                <behavior name="BindingBehavior"> 
                    <!--httpGetEnabled - 使用get方式提供服务--> 
                    <serviceMetadata httpGetEnabled="true" /> 
                </behavior> 
            </serviceBehaviors> 
        </behaviors> 
    </system.serviceModel> 
</configuration>
 
 
3、客户端
Hello.cs
InBlock.gif using System; 
InBlock.gif using System.Collections.Generic; 
InBlock.gif using System.ComponentModel; 
InBlock.gif using System.Data; 
InBlock.gif using System.Drawing; 
InBlock.gif using System.Linq; 
InBlock.gif using System.Text; 
InBlock.gif using System.Windows.Forms; 
InBlock.gif 
InBlock.gif using System.ServiceModel; 
InBlock.gif 
InBlock.gif namespace Client2.Binding 
InBlock.gif
InBlock.gif         public partial  class Hello : Form 
InBlock.gif        { 
InBlock.gif                 public Hello() 
InBlock.gif                { 
InBlock.gif                        InitializeComponent(); 
InBlock.gif                } 
InBlock.gif 
InBlock.gif                 private  void btnSayHello_Click( object sender, EventArgs e) 
InBlock.gif                { 
InBlock.gif                         // 写代码的方式做client 
InBlock.gif                         // IHello proxy = ChannelFactory<IHello>.CreateChannel(new NetTcpBinding(), new EndpointAddress("net.tcp://localhost:54321/Binding/Hello")); 
InBlock.gif 
InBlock.gif                        Binding.HelloClient proxy =  new Binding.HelloClient(); 
InBlock.gif 
InBlock.gif                        MessageBox.Show(proxy.SayHello(txtName.Text)); 
InBlock.gif 
InBlock.gif                        proxy.Close(); 
InBlock.gif                } 
InBlock.gif        } 
InBlock.gif}
 
App.config
<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <system.serviceModel> 
        <client> 
            <!--address - 服务地址--> 
            <!--binding - 通信方式--> 
            <!--contract - 服务契约--> 
            <!--<endpoint address="http://localhost:12345/Binding/Hello" binding="basicHttpBinding" contract="Binding.IHello" />--> 
            <endpoint address="net.tcp://localhost:54321/Binding/Hello" binding="netTcpBinding" contract="Binding.IHello" /> 
        </client> 
    </system.serviceModel> 
</configuration>
 
 

运行结果:
单击"Hello"按钮后弹出提示框,显示"Hello: webabcd"


OK
[源码下载]




     本文转自webabcd 51CTO博客,原文链接: http://blog.51cto.com/webabcd/344108 ,如需转载请自行联系原作者


相关文章
|
网络协议 安全 Windows
WCF如何绑定netTcpBinding寄宿到控制台应用程序详解
新建一个WCF服务类库项目,在其中添加两个WCF服务:GameService,PlayerService
WCF如何绑定netTcpBinding寄宿到控制台应用程序详解
|
网络协议
WCF绑定的选择
版权声明:欢迎评论和转载,转载请注明来源。 https://blog.csdn.net/zy332719794/article/details/8593924 格式与编码 每种标准绑定使用的传输协议与编码格式都不相同,如表1-1 所示。
599 0
|
数据库 网络架构
我的服装DRP之即时通讯——为WCF增加UDP绑定(应用篇)
发个牢骚,博客园发博文竟然不能写副标题。这篇既为我的服装DRP系列第二篇,也给为WCF增加UDP绑定系列收个尾。原本我打算记录开发过程中遇到的一些问题和个人见解,不过写到一半发现要写的东西实在太多,有些问题甚至不好描述,又担心误导读者,就作罢了。
623 0
为WCF增加UDP绑定(实践篇)
这两天忙着系统其它功能的开发,没顾上写日志。本篇所述皆围绕为WCF增加UDP绑定(储备篇)中讲到的微软示例,该示例我已上传到网盘。 上篇说道,绑定是由若干绑定元素有序组成,为WCF增加UDP绑定其实就是为绑定增加UDP传输绑定元素,最终目的是在信道栈中生成UDP传输信道。
887 0
|
网络协议 网络架构 安全
为WCF增加UDP绑定(储备篇)
日前我开发的服装DRP需要用到即时通信方面的技术,比如当下级店铺开出零售单时上级机构能实时收到XX店铺XX时XX分卖出XX款衣服X件之类的信息,当然在上级发货时,店铺里也能收到已经发货的提醒。即时通信技术能运用到DRP系统的很多方面,若深入下去,甚至可以开发一个系统内部的通讯模块,类似于QQ。
981 0
|
网络协议
WCF绑定类型选择
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chinahuyong/article/details/6070364     WCF绑定类型选择     发布日期:2010年12月10日星期五 作者:EricHu     在开发WCF程序时,如何选择一个适合的绑定对于消息传输的可靠性,传输模式是否跨进程、主机、网络,传输模式的支持、安全性、性能等方面有着重要的影响。
790 0
|
安全 网络架构
消息(7)——WCF编程模型中控制消息(1)绑定,契约
WCF服务要通过终结点来进行通信,终结点三大构成元素:ABC,其中的B,binding是重中之重,它解决了在消息交换过程中的编码,传输协议,安全等问题。 绑定是分层的,一个绑定对象对应一组有序的绑定元素的集合。
745 0
|
XML 安全 网络协议
WCF入门(一)——终结点,地址,绑定(1)
运行WCF服务 这里通过自宿主方式self-host来运行wcf服务。 公开终结点Endpoint,终结点由ServiceEndpoint 类来实现。它有很多的成员。其中要用到的是所说的ABC。 Address,Binding,Contract,地址,绑定,契约。
1006 0
WCF绑定细节(2)——绑定,绑定元素
绑定这块引出了很多细节。绑定解决了消息交换中的传输协议,传输,编码等问题。如果要公开WCF服务,就要公开终结点Endpoint,WCF服务信息交换就是Endpoint之间的信息交换。终结点三大元素:ABC。
859 0
|
安全 数据格式 XML
WCF—Binding
Binding描述了哪些层面的信息 一个Binding包含着丰富的信息,每种信息都描述了服务端和客户端交互过程中的一方面,如下表所示,Binding描述了这些层面的信息: 层次 备注说明 Transactions(事务) TransactionF...
693 0