化零为整WCF(3) - 绑定Binding(basicHttpBinding和netTcpBinding)
作者:webabcd
介绍
WCF(Windows Communication Foundation),绑定Binding,Http以basicHttpBinding为例,Tcp以netTcpBinding为例
示例
1、服务
IHello.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
namespace WCF.ServiceLib.Binding

{
/**////<summary>
/// IHello接口
///</summary>
[ServiceContract]
publicinterface IHello

{
/**////<summary>
/// 打招呼方法
///</summary>
///<param name="name">人名</param>
///<returns></returns>
[OperationContract]
string SayHello(string name);
}
}Hello.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
namespace WCF.ServiceLib.Binding

{
/**////<summary>
/// Hello类
///</summary>

public
class Hello : IHello

{
/**////<summary>
/// 打招呼方法
///</summary>
///<param name="name">人名</param>
///<returns></returns>

public
string SayHello(string name)

{
return"Hello: "
+ name;
}
}
}2、宿主
Hello.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
namespace WCF.ServiceHost2.Binding

{
class Hello
{
staticvoid Main(string[] args)

{
using (ServiceHost host =new ServiceHost(typeof(WCF.ServiceLib.Binding.Hello)))

{
// 写代码的方式做host
// host.AddServiceEndpoint(typeof(WCF.ServiceLib.Binding.IHello), new NetTcpBinding(), "net.tcp://localhost:54321/Binding/Hello");
host.Open();
Console.WriteLine("服务已启动");
Console.WriteLine("按<ENTER>停止服务");
Console.ReadLine();
}
}
}
}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" c 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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ServiceModel;
namespace Client2.Binding

{
publicpartial
class Hello : Form

{
public Hello()
{
InitializeComponent();
}
privatevoid btnSayHello_Click(object sender, EventArgs e)

{
// 写代码的方式做client
// IHello proxy = ChannelFactory<IHello>.CreateChannel(new NetTcpBinding(), new EndpointAddress("net.tcp://localhost:54321/Binding/Hello"));
Binding.HelloClient proxy =new Binding.HelloClient();

MessageBox.Show(proxy.SayHello(txtName.Text));
proxy.Close();
}
}
}
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" c />-->
<endpoint address="net.tcp://localhost:54321/Binding/Hello" binding="netTcpBinding" contract="Binding.IHello"/>
</client>
</system.serviceModel>
</configuration>运行结果:
单击"Hello"按钮后弹出提示框,显示"Hello: webabcd"
OK
[源码下载]

添加至收藏夹