WCF發布方式介紹

來源:互聯網
上載者:User

標籤:winform   class   blog   http   tar   ext   

轉載出處:http://blog.csdn.net/fangxing80/article/details/6101790

 

 

 

 

 

 

 

 

 

 

 

 

從VS2005推出WCF以來,WCF逐步取代了Remoting, WebService成為.NET上分布式程式的主要技術。WCF統一的模型整合了以往的 WebService、Remoting、MSMQ 等技術,讓分布式開發變得更加簡單,方便,快捷。 wcf_host_architecture (選自《Programming WCF Services》) WCF基本概念(ABC): 1.地址(Address):決定服務的地址;2.綁定(Binding):決定服務的細節;3.契約(Contract):服務的定義(抽象),決定訊息結構的定義。 WCF的發布:WCF服務的發布可以有幾種形式: IIS, Windows Service, Self-Host(可以是Console程式也可以是Winform程式)。 WCF的工具: Windows Communication Foundation 工具 簡單一實例-1: 內建AppDomain (無配置) 1. Service1.cs [c-sharp] view plaincopy namespace WCFStudy1 { [ServiceContract] public interface IService1 { [OperationContract] string SendMessage(string clientInput); } public class Service1 : IService1 { #region IService1 Members public string SendMessage(string clientInput) { return string.Format("Server Get Message: {0}", clientInput); } #endregion } } 2. Program.cs [c-sharp] view plaincopy using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; namespace WCFStudy1 { class Program { static void Main(string[] args) { // 建立一個獨立AppDomain作為服務端。 AppDomain.CreateDomain("Server1").DoCallBack(delegate { ServiceHost host = new ServiceHost(typeof(Service1)); host.AddServiceEndpoint(typeof(IService1), //契約(C) new BasicHttpBinding(), //綁定(B) "http://localhost:9999/myservice"); //地址(A) host.Open(); }); // 下面是用戶端 ChannelFactory factory = new ChannelFactory( new BasicHttpBinding(), "http://localhost:9999/myservice"); IService1 client = factory.CreateChannel(); var reply = client.SendMessage("Hello WCF"); Console.WriteLine(reply); Console.Read(); } } } : 簡單一實例-2: 建立 Console Self-Host SelfHostConsole_overview WcfServiceLib - 服務契約的實現; *ConsoleHost工程 – Wcf宿主; *ConsoleClient工程 - Wcf用戶端 建立WcfServiceLib工程(選WCF Service Library工程模板: VS為我們自動添加一個IService1.cs和Service1.cs) SelfHostConsole_servicelib Host工程裡引用WcfServiceLib工程 SelfHostConsole_hostRef 將WcfServiceLib裡App.config移動到ConsoleHost工程裡,刪掉Lib工程裡的App.config SelfHostConsole_appconfig Host工程的Program.cs添加下面的代碼,右擊Builder工程 [c-sharp] view plaincopy using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; using WcfServiceLib; namespace WCFStudy2ConsoleHost { class Program { static void Main(string[] args) { using(var host = new ServiceHost(typeof(Service1))) { host.Open(); Console.WriteLine("Service start."); Console.Read(); } } } } 運行 ConsoleHost工程 bin/debug 下面的 exe(這一步是為了產生用戶端代理,需要啟動Host) 在Client工程裡通過添加 Service References,產生用戶端Proxy,關閉exe SelfHostConsole_clientproxy 在Client工程的Program.cs裡添加如下代碼。 [c-sharp] view plaincopy using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace WCFStudy2ConsoleClient { class Program { static void Main(string[] args) { MyWcfSvc.Service1Client client = new MyWcfSvc.Service1Client(); var result = client.GetData(123); Console.WriteLine(result); Console.Read(); } } } F5 運行Solution裡的Host, 再右擊Client工程選Debug的Start new instance方式,運行Client 運行結果: SelfHostConsole_runtime 由於ServiceHost執行個體是被建立在應用程式定義域中,必須保證宿主進程在調用服務期間不會被關閉,因此利用Console.Read()來阻塞進程,以使得控制台應用程式能夠一直運行,直到人為關閉應用程式。 簡單一實例-3: 建立 Winform Self-Host Winform的Self-Host和ConsoleHost類似,先添加 WcfServiceLib 工程引用,將 WcfServiceLib 裡的App.config 移到 Winform 工程裡。加上啟動Service的代碼就OK了! [c-sharp] view plaincopy public partial class Form1 : Form { public Form1() { InitializeComponent(); } private ServiceHost host = null; // 開啟服務端 private void btnStart_Click(object sender, EventArgs e) { try { if (host != null) host.Close(); host = new ServiceHost(typeof(WcfServiceLib.Service1)); host.Open(); this.textBox1.Text = "Server Opened!"; } catch (Exception ex) { MessageBox.Show(ex.ToString()); if (host != null) host.Close(); } } // 關閉服務端 private void btnStop_Click(object sender, EventArgs e) { if (host != null) { host.Close(); this.textBox1.Text += "Server Closed!"; } } } 在Winform中,不要使用 using(...) 代碼塊,這將導致在Button方法結束後自動銷毀Host對象而關閉服務。 簡單一實例-4: 建立 Windows Service Host Windows Services宿主便於管理者方便地啟動或停止服務,且在服務出現故障之後,能夠重新啟動服務。還可以通過Service Control Manager(服務控制管理員),將服務設定為自動啟動方式,省去了服務的管理工作。此外,Windows Services自身還提供了一定的安全性以及檢測機制和日誌機制。 1. 建立Windows Service工程 2. 引用 WcfServiceLib 工程,添加 App.config (和前面Host添加的App.config一樣) 3. 重寫 WindowsService 類的 OnStart 和 OnStop 方法 [c-sharp] view plaincopy public partial class Service1 : ServiceBase { public Service1() { InitializeComponent(); } private ServiceHost host = null; protected override void OnStart(string[] args) { if (host != null) host.Close(); host = new ServiceHost(typeof(WcfServiceLib.Service1)); host.Open(); } protected override void OnStop() { if (host != null) host.Close(); } } 4. 建立Service的安裝類:在WindowsService 類的設計介面上右擊選擇 [Add Installer] 修改 serviceProcessInstaller 的 Account 屬性 (預設為User) 改為 LocalSystem 通過在Visual Studio的 [Command Prompt] (命令列)模式下通過 InstallUtil 工具安裝 Windows服務: InstallUtil [絕對路徑]/WCFStudy2WindowsServiceHost.exe (安裝成功之後,使用Services.msc查看服務) 簡單一實例-5: 建立 IIS Host 最簡單的就是直接建立一個 WCF Service Application 就OK了。 以上所有工程的關係圖如下:

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.