WCF服务加载实际应用方法详解

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. }
  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服务加载实际应用方法详解
转载来源:http://www.hantingmc.com/qtweb/news20/287220.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联