WCF开发工具中有很多比较重要的操作技术是需要我们去熟练的掌握,以至于在实际应用中获得些帮助。今天我们就为大家介绍一下有关WCF服务加载在实现的过程中出现的一些问题的解决方法。
如果用self-host的方式来实现WCF服务加载的话,我们一般是用这样的代码:ServiceHost host1 = new ServiceHost(typeof(UserService)); 问题是,如果当你的服务很多的时候这样做是不胜其烦的,今天在看Ingo Rammer(大名鼎鼎的《Advanced .Net Remoting》作者)的Blog的时候,看到了他解决这一问题的方法:
一.服务配置在app.config或web.config中:
- using System;
- using System.Collections.Generic;
- using System.Reflection;
- using System.Configuration;
- using System.ServiceModel.Configuration;
- using System.ServiceModel;
- public class ServiceHostGroup
- {
- static List< ServiceHost> _hosts = new List< ServiceHost>();
- private static void OpenHost(Type t)
- {
- ServiceHost hst = new ServiceHost(t);
- hst.Open();
- _hosts.Add(hst);
- }
- public static void StartAllConfiguredServices()
- {
- Configuration conf =
- ConfigurationManager.OpenExeConfiguration(Assembly.
GetEntryAssembly().Location);- ServiceModelSectionGroup svcmod =
- (ServiceModelSectionGroup)conf.GetSectionGroup("system.serviceModel");
- foreach (ServiceElement el in svcmod.Services.Services)
- {
- Type svcType = Type.GetType(el.Name);
- if (svcType == null)
- throw new Exception("Invalid Service Type " + el.Name +
" in configuration file.");- OpenHost(svcType);
- }
- }
- public static void CloseAllServices()
- {
- foreach (ServiceHost hst in _hosts)
- {
- hst.Close();
- }
- }
- }
WCF服务加载解决问题方法步骤之二.服务配置在外部配置文件中:
Services.XML:
- < configuredServices>
- < service type="ServiceImplementation.ArticleService,
ServiceImplementation" />- < /configuredServices>
ServiceHostBase.cs:
- using System;
- using System.Collections.Generic;
- using System.Reflection;
- using System.Configuration;
- using System.ServiceModel.Configuration;
- using System.ServiceModel;
- using System.Xml;
- using System.Xml.Serialization;
- using System.IO;
- public class ServiceHostGroup
- {
- static List< ServiceHost> _hosts = new List< ServiceHost>();
- private static void OpenHost(Type t)
- {
- ServiceHost hst = new ServiceHost(t);
- Type ty = hst.Description.ServiceType;
- hst.Open();
- _hosts.Add(hst);
- }
- public static void StartAllConfiguredServices()
- {
- ConfiguredServices services = ConfiguredServices.
LoadFromFile("services.xml");- foreach (ConfiguredService svc in services.Services)
- {
- Type svcType = Type.GetType(svc.Type);
- if (svcType == null) throw new Exception("Invalid Service Type "
+ svc.Type + " in configuration file.");- OpenHost(svcType);
- }
- }
- public static void CloseAllServices()
- {
- foreach (ServiceHost hst in _hosts)
- {
- hst.Close();
- }
- }
- }
- [XmlRoot("configuredServices")]
- public class ConfiguredServices
- {
- public static ConfiguredServices LoadFromFile(string filename)
- {
- if (!File.Exists(filename)) return new ConfiguredServices();
- XmlSerializer ser = new XmlSerializer(typeof(ConfiguredServices));
- using (FileStream fs = File.OpenRead(filename))
- {
- return (ConfiguredServices) ser.Deserialize(fs);
- }
- }
- [XmlElement("service", typeof(ConfiguredService))]
- public List< ConfiguredService> Services = new List
< ConfiguredService>();- }
- public class ConfiguredService
- {
- [XmlAttribute("type")]
- public string Type;
- }
以上就是对WCF服务加载中遇到的相关问题的解决方法。
【编辑推荐】