成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

WCF服務加載實際應用方法詳解

開發 開發工具
WCF服務加載可以使用self-host的方式來實現。那么在實現的過程中通常會遇到一些問題,在這里我們就針對一些問題進行解讀。

WCF開發工具中有很多比較重要的操作技術是需要我們去熟練的掌握,以至于在實際應用中獲得些幫助。今天我們就為大家介紹一下有關WCF服務加載在實現的過程中出現的一些問題的解決方法。

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

一.服務配置在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服務加載解決問題方法步驟之二.服務配置在外部配置文件中:

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;  

以上就是對WCF服務加載中遇到的相關問題的解決方法。

【編輯推薦】

  1. AJAX WCF服務項模板正確使用方法介紹
  2. WCF返回值適用場景分析
  3. WCF數據量在實際應用中錯誤解決方法
  4. WCF發布訂閱實質內容剖析
  5. WCF線程安全性問題有所解決
責任編輯:曹凱 來源: 博客園
相關推薦

2010-03-01 13:06:49

WCF繼承

2010-02-25 17:22:39

WCF服務行為

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異步調用

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

2010-03-01 10:45:59

WCF集合類

2010-03-01 17:52:03

WCF選擇綁定

2009-12-21 14:58:57

WCF用戶密碼認證

2009-12-21 18:32:22

關閉WCF鏈接

2009-12-22 16:36:38

WCF重載

2010-02-23 16:32:29

WCF服務

2010-02-25 16:12:23

WCF IDispos

2009-12-21 16:04:45

WCF Dispose

2010-02-22 11:02:06

WCF元數據

2010-02-23 14:48:38

WCF事件通知
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 国产精品视频久久久久 | 毛片网站在线观看 | 国产精品久久一区 | 久久福利| 视频在线观看一区二区 | 久久网一区二区三区 | 99久久久久久久久 | 国产高清视频一区 | 欧美视频在线播放 | 日日骚网 | 在线视频国产一区 | 欧美在线视频一区二区 | 亚洲一区二区精品视频 | 91久久精品日日躁夜夜躁国产 | 成人毛片视频免费 | 国产精品日韩欧美一区二区三区 | 日本不卡一区二区三区在线观看 | 91亚洲精品国偷拍自产在线观看 | 久久国| 九九国产在线观看 | 涩涩视频网站在线观看 | www.99热 | 911精品美国片911久久久 | 91在线资源 | 国产精品久久99 | 99热精品久久 | 欧美一区| 色综合久久88色综合天天 | 午夜视频免费 | 久久久久久久久久久久久91 | 亚洲三级在线 | 一区二区三区回区在观看免费视频 | 欧美久久久久久久 | 国产在线中文字幕 | h小视频 | 天天夜夜操 | 日韩欧美不卡 | 欧美一级www片免费观看 | 亚洲精品免费视频 | 国产一级特黄aaa大片评分 | 精品在线观看入口 |