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服务行为

2010-02-26 10:56:06

WCF Stream

2009-12-21 14:49:27

2010-02-23 10:25:29

2010-02-24 14:05:08

WCF openati

2010-02-22 13:28:05

WCF异步调用

2010-02-22 11:25:50

WCF DateSet

2010-03-02 16:43:46

2009-12-21 14:58:57

WCF用户密码认证

2010-03-01 17:52:03

WCF选择绑定

2010-03-01 10:45:59

WCF集合类

2009-12-22 17:30:47

WCF Address

2009-12-21 18:32:22

关闭WCF链接

2009-12-22 16:36:38

WCF重载

2010-02-23 16:32:29

WCF服务

2009-10-28 14:28:10

2010-06-24 14:08:25

Linux Cat命令

2009-12-21 16:04:45

WCF Dispose

2010-02-22 11:02:06

WCF元数据
点赞
收藏

51CTO技术栈公众号