[索引页]
[×××] 


化零为整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
[×××]