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

創(chuàng)新互聯(lián)公司專注于赤峰林西企業(yè)網(wǎng)站建設(shè),自適應(yīng)網(wǎng)站建設(shè),購物商城網(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的方式來實(shí)現(xiàn)WCF服務(wù)加載的話,我們一般是用這樣的代碼:ServiceHost host1 = new ServiceHost(typeof(UserService)); 問題是,如果當(dāng)你的服務(wù)很多的時(shí)候這樣做是不勝其煩的,今天在看Ingo Rammer(大名鼎鼎的《Advanced .Net Remoting》作者)的Blog的時(shí)候,看到了他解決這一問題的方法:
一.服務(wù)配置在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服務(wù)加載解決問題方法步驟之二.服務(wù)配置在外部配置文件中:
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;
- }
以上就是對(duì)WCF服務(wù)加載中遇到的相關(guān)問題的解決方法。
本文標(biāo)題:WCF服務(wù)加載實(shí)際應(yīng)用方法詳解
轉(zhuǎn)載來源:http://fisionsoft.com.cn/article/cdihpdd.html


咨詢
建站咨詢
