日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側工具欄

新聞中心

這里有您想知道的互聯(lián)網營銷解決方案
WCF服務加載實際應用方法詳解

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

創(chuàng)新互聯(lián)公司專注于赤峰林西企業(yè)網站建設,自適應網站建設,購物商城網站建設。赤峰林西網站建設公司,為赤峰林西等地區(qū)提供建站服務。全流程按需網站建設,專業(yè)設計,全程項目跟蹤,創(chuàng)新互聯(lián)公司專業(yè)和態(tài)度為您提供的服務

如果用self-host的方式來實現(xiàn)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. }
  37. }

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;
  57. }

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


當前名稱:WCF服務加載實際應用方法詳解
本文URL:http://m.5511xx.com/article/cdihpdd.html