日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

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

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

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

如果用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. }
  37. }

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

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


名稱欄目:WCF服務(wù)加載實(shí)際應(yīng)用方法詳解
標(biāo)題來(lái)源:http://www.dlmjj.cn/article/cdihpdd.html