1. 程式人生 > >[WCF REST] WebServiceHost 不依賴配置文件啟動簡單服務

[WCF REST] WebServiceHost 不依賴配置文件啟動簡單服務

簡單 配置 ext api ora host 發現 arch orm

最近用WPF啟動 WCF REST 服務,發現app.config 配置好煩,簡單一個exe 可以到處搬動,還非得帶一個累贅配置,不小心丟了程序就跑不起來。

最後決定,砍去WCF配置項,用WebServiceHost 簡單啟動服務,監聽端口與路徑寫在註冊表中。WPF程序給一個配置項配置端口與路徑項。

 1 [ServiceContract]
 2 public interface IHomeService
 3 {
 4     [OperationContract]
 5     [WebGet(UriTemplate = "Get/{id}", RequestFormat = WebMessageFormat.Json,ResponseFormat = WebMessageFormat.Json)]
6 string Get(string id); 7 8 [OperationContract] 9 [WebInvoke(Method = "POST", UriTemplate = "Add", RequestFormat = WebMessageFormat.Json,ResponseFormat = WebMessageFormat.Json)] 10 string Add(string stu); 11 } 12 13 public class HomeService : IHomeService 14 { 15 public
string Get(string id) 16 { 17 return "Get " + id; 18 } 19 20 public string Add(string id) 21 { 22 return "Add " + id; 23 } 24 } 25 26 public class WebApi 27 { 28 private static WebServiceHost _host; 29 30 /// <summary> 31 /// 開啟服務 32
/// </summary> 33 /// <param name="url">監聽路徑(http://127.0.0.1:3721/abc)</param> 34 public static void Open(string url) 35 { 36 if (_host==null) 37 { 38 Uri baseAddress = new Uri(url); 39 _host = new WebServiceHost(typeof(HomeService), baseAddress); 40 _host.Open(); 41 } 42 } 43 }

更多詳細內容 可以看 Artech 的詳細分解

https://www.cnblogs.com/artech/archive/2012/02/15/wcf-rest.html

[WCF REST] WebServiceHost 不依賴配置文件啟動簡單服務