介紹
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] public interface 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 { static void 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" 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
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 { public partial class Hello : Form { public Hello() { InitializeComponent(); } private void 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" contract="Binding.IHello" />--> <endpoint address="net.tcp://localhost:54321/Binding/Hello" binding="netTcpBinding" contract="Binding.IHello" /> </client> </system.serviceModel> </configuration>
運行結果:
單擊"Hello"按鈕後彈出提示框,顯示"Hello: webabcd"
OK