最近很少在寫文章,也佷少在討論區解問題(因為工作很忙),所以沒什麼東西可以寫
這幾天無聊去看看WCF的東西,介紹給大家如何利用net.tcp傳輸協定來建置WCF Service
啓動WCF服務的方式有下列幾種方式:
1.利用Console或WinFrom方式2.利用Windows Service方式3.利用Web Server IIS方式
WCF支援的傳輸協定有下列幾種方式:
1.HTTP2.net.tcp3.net.pipe4.net.msmq
存取WCF服務的Client端也可利用下列幾種方式:
1.WinForm or Console2.ASP.NET or ASP.NET AJAX3.WPF or Silverlight...很多
首先準備下列專案:
WcfBase是給ConsoleHost與WinFormClient共用的ConsoleHost是Server端WinFormClient是Client端
Server端啓動服務利用ServiceHostClient端呼叫服務利用ClientBase或ChannelFactory
此範例主要是利用Console來啓動WCF Service,利用WinForm來呼叫WCF Service
WcfBase(IHello.cs,Hello.cs)
IHello.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; //加入System.ServiceModel參考 namespace WcfBase { [ServiceContract] public interface IHello { [OperationContract] string HelloWorld(); } }
Hello.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace WcfBase { public class Hello : IHello { public string HelloWorld() { return "HelloWorld"; } } }
ConsoleHost(Program.cs,App.config)
Program.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; //加入System.ServiceModel與WcfBase參考 namespace ConsoleHost { class Program { static void Main(string[] args) { //WCF Console Host //此種方式需App.Config ServiceHost host = new ServiceHost(typeof(WcfBase.Hello)); host.Open(); Console.WriteLine("WCF Service Start..."); Console.WriteLine("Press Enter to Stop WCF Service..."); Console.ReadLine(); host.Close(); //此種方式不需App.Config //ServiceHost host = new ServiceHost(typeof(WcfBase.Hello), new Uri("net.tcp://localhost:9000/")); //host.AddServiceEndpoint(typeof(WcfBase.IHello), new NetTcpBinding(), "HelloService"); //host.Open(); //Console.WriteLine("WCF Service Start..."); //Console.WriteLine("Press Enter to Stop WCF Service..."); //Console.ReadLine(); //host.Close(); } } }
App.config
<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <services> <service name="WcfBase.Hello"> <endpoint address="HelloService" binding="netTcpBinding" contract="WcfBase.IHello"/> <host> <baseAddresses> <add baseAddress="net.tcp://localhost:9000/"/> </baseAddresses> </host> </service> </services> </system.serviceModel> </configuration>
WinFormClient(FrmClient.cs,HelloClient.cs,App.config)
FrmClient.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; //加入System.ServiceModel與WcfBase參考 namespace WinFormClient { public partial class FrmClient : Form { public FrmClient() { InitializeComponent(); } private void btnClientBase_Click(object sender, EventArgs e) { //此種方式需App.Config using (HelloClient client = new HelloClient()) { client.Open(); MessageBox.Show(client.HelloWorld()); } //此種方式不需App.Config //using (HelloClient client = new HelloClient( new NetTcpBinding(),new EndpointAddress("net.tcp://localhost:9000/HelloService"))) //{ // client.Open(); // MessageBox.Show(client.HelloWorld()); //} } private void btnChannelFactory_Click(object sender, EventArgs e) { //此種方式需App.Config using (ChannelFactory<WcfBase.IHello> channel = new ChannelFactory<WcfBase.IHello>("Hello"))//指定ConfigName { WcfBase.IHello client = channel.CreateChannel();//CreateChannel後就會Open Channel MessageBox.Show(client.HelloWorld()); } //此種方式不需App.Config //using (ChannelFactory<WcfBase.IHello> channel = new ChannelFactory<WcfBase.IHello>(new NetTcpBinding(), new EndpointAddress("net.tcp://localhost:9000/HelloService"))) //{ // WcfBase.IHello client = channel.CreateChannel();//CreateChannel後就會Open Channel // MessageBox.Show(client.HelloWorld()); //} } } }
HelloClient.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace WinFormClient { /// <summary> /// 利用此類別來呼叫WCF Service, /// </summary> public class HelloClient : System.ServiceModel.ClientBase<WcfBase.IHello>, WcfBase.IHello { //沒有傳入任何Binding與EndpointAddress,會找App.Config的設定 public HelloClient() { } //依使用者定義的Binding與EndpointAddress,來設定 public HelloClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) : base(binding, remoteAddress) { } public string HelloWorld() { return base.Channel.HelloWorld(); } } }
<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <client> <endpoint name="Hello" address="net.tcp://localhost:9000/HelloService" binding="netTcpBinding" contract="WcfBase.IHello"> </endpoint> </client> </system.serviceModel> </configuration>
設定Config可以利用SvcConfigEditor.exe工具
參考網址:http://msdn.microsoft.com/zh-tw/library/bb332338.aspxhttp://www.devx.com/codemag/Article/33655/1763/page/1http://www.codeproject.com/KB/WCF/WCFMultipleHosting.aspxhttp://msdn.microsoft.com/zh-tw/library/ms732015.aspx