自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

WCF服務(wù)加載實(shí)際應(yīng)用方法詳解

開(kāi)發(fā) 開(kāi)發(fā)工具
WCF服務(wù)加載可以使用self-host的方式來(lái)實(shí)現(xiàn)。那么在實(shí)現(xiàn)的過(guò)程中通常會(huì)遇到一些問(wèn)題,在這里我們就針對(duì)一些問(wèn)題進(jìn)行解讀。

WCF開(kāi)發(fā)工具中有很多比較重要的操作技術(shù)是需要我們?nèi)ナ炀毜恼莆?,以至于在?shí)際應(yīng)用中獲得些幫助。今天我們就為大家介紹一下有關(guān)WCF服務(wù)加載在實(shí)現(xiàn)的過(guò)程中出現(xiàn)的一些問(wèn)題的解決方法。

如果用self-host的方式來(lái)實(shí)現(xiàn)WCF服務(wù)加載的話,我們一般是用這樣的代碼:ServiceHost host1 = new ServiceHost(typeof(UserService)); 問(wèn)題是,如果當(dāng)你的服務(wù)很多的時(shí)候這樣做是不勝其煩的,今天在看Ingo Rammer(大名鼎鼎的《Advanced .Net Remoting》作者)的Blog的時(shí)候,看到了他解決這一問(wèn)題的方法:

一.服務(wù)配置在app.config或web.config中:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Reflection;  
  4. using System.Configuration;  
  5. using System.ServiceModel.Configuration;  
  6. using System.ServiceModel;  
  7. public class ServiceHostGroup  
  8. {  
  9. static List< ServiceHost> _hosts = new List< ServiceHost>();  
  10. private static void OpenHost(Type t)  
  11. {  
  12. ServiceHost hst = new ServiceHost(t);  
  13. hst.Open();  
  14. _hosts.Add(hst);  
  15. }  
  16. public static void StartAllConfiguredServices()  
  17. {  
  18. Configuration conf =   
  19. ConfigurationManager.OpenExeConfiguration(Assembly.
    GetEntryAssembly().Location);  
  20. ServiceModelSectionGroup svcmod =   
  21. (ServiceModelSectionGroup)conf.GetSectionGroup("system.serviceModel");  
  22. foreach (ServiceElement el in svcmod.Services.Services)  
  23. {  
  24. Type svcType = Type.GetType(el.Name);  
  25. if (svcType == null)   
  26. throw new Exception("Invalid Service Type " + el.Name + 
    " in configuration file.");  
  27. OpenHost(svcType);  
  28. }  
  29. }  
  30. public static void CloseAllServices()  
  31. {  
  32. foreach (ServiceHost hst in _hosts)  
  33. {  
  34. hst.Close();  
  35. }  
  36. }  

WCF服務(wù)加載解決問(wèn)題方法步驟之二.服務(wù)配置在外部配置文件中:

Services.XML:

  1. < configuredServices> 
  2. < service type="ServiceImplementation.ArticleService, 
    ServiceImplementation"
     /> 
  3. < /configuredServices> 

ServiceHostBase.cs:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Reflection;  
  4. using System.Configuration;  
  5. using System.ServiceModel.Configuration;  
  6. using System.ServiceModel;  
  7. using System.Xml;  
  8. using System.Xml.Serialization;  
  9. using System.IO;  
  10. public class ServiceHostGroup  
  11. {  
  12. static List< ServiceHost> _hosts = new List< ServiceHost>();  
  13. private static void OpenHost(Type t)  
  14. {  
  15. ServiceHost hst = new ServiceHost(t);  
  16. Type ty = hst.Description.ServiceType;  
  17. hst.Open();  
  18. _hosts.Add(hst);  
  19. }  
  20. public static void StartAllConfiguredServices()  
  21. {  
  22. ConfiguredServices services = ConfiguredServices.
    LoadFromFile("services.xml");  
  23. foreach (ConfiguredService svc in services.Services)  
  24. {  
  25. Type svcType = Type.GetType(svc.Type);  
  26. if (svcType == null) throw new Exception("Invalid Service Type " 
    + svc.Type + " in configuration file.");  
  27. OpenHost(svcType);  
  28. }  
  29. }  
  30. public static void CloseAllServices()  
  31. {  
  32. foreach (ServiceHost hst in _hosts)  
  33. {  
  34. hst.Close();  
  35. }  
  36. }  
  37. }  
  38. [XmlRoot("configuredServices")]  
  39. public class ConfiguredServices  
  40. {  
  41. public static ConfiguredServices LoadFromFile(string filename)  
  42. {  
  43. if (!File.Exists(filename)) return new ConfiguredServices();  
  44. XmlSerializer ser = new XmlSerializer(typeof(ConfiguredServices));  
  45. using (FileStream fs = File.OpenRead(filename))  
  46. {  
  47. return (ConfiguredServices) ser.Deserialize(fs);  
  48. }  
  49. }  
  50. [XmlElement("service", typeof(ConfiguredService))]  
  51. public List< ConfiguredService> Services = new List
    < ConfiguredService>();  
  52. }  
  53. public class ConfiguredService  
  54. {  
  55. [XmlAttribute("type")]  
  56. public string Type;  

以上就是對(duì)WCF服務(wù)加載中遇到的相關(guān)問(wèn)題的解決方法。

【編輯推薦】

  1. AJAX WCF服務(wù)項(xiàng)模板正確使用方法介紹
  2. WCF返回值適用場(chǎng)景分析
  3. WCF數(shù)據(jù)量在實(shí)際應(yīng)用中錯(cuò)誤解決方法
  4. WCF發(fā)布訂閱實(shí)質(zhì)內(nèi)容剖析
  5. WCF線程安全性問(wèn)題有所解決
責(zé)任編輯:曹凱 來(lái)源: 博客園
相關(guān)推薦

2010-03-01 13:06:49

WCF繼承

2010-02-25 17:22:39

WCF服務(wù)行為

2009-12-21 14:49:27

2010-02-26 10:56:06

WCF Stream

2010-02-22 11:25:50

WCF DateSet

2010-02-22 13:28:05

WCF異步調(diào)用

2010-02-23 10:25:29

2010-02-24 14:05:08

WCF openati

2010-03-02 16:43:46

2009-12-22 17:30:47

WCF Address

2009-12-22 16:36:38

WCF重載

2009-12-21 18:32:22

關(guān)閉WCF鏈接

2010-03-01 10:45:59

WCF集合類

2010-03-01 17:52:03

WCF選擇綁定

2009-12-21 14:58:57

WCF用戶密碼認(rèn)證

2010-02-23 16:32:29

WCF服務(wù)

2010-02-25 13:54:48

WCF安全參數(shù)

2010-02-23 14:48:38

WCF事件通知

2010-02-25 16:12:23

WCF IDispos

2009-12-21 16:04:45

WCF Dispose
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)