文章出處
文章列表
有時我們需要將WCF的配置文件放在單獨的配置文件中,而默認情況下WCF又是在web.config或app.config中去尋找服務配置。如果我們把配置文件放在另一個config文件中,如何讓WCF知道呢?
答案就是重寫ServiceHost。在重寫中告訴WCF配置文件的路徑。
public class MyServiceHost:ServiceHost { private string ConfigPath =System.AppDomain.CurrentDomain.BaseDirectory+ "MyApp.config"; public MyServiceHost(Type serviceType, params Uri[] baseAddresses) : base(serviceType, baseAddresses) { } protected override void ApplyConfiguration() { // Check user config invalidation if (!CheckConfigExist(ConfigPath)) { // Use default config base.ApplyConfiguration(); return; } //base.ApplyConfiguration(); // Use user config ExeConfigurationFileMap execfgMap = new ExeConfigurationFileMap(); // Set user config FilePath execfgMap.ExeConfigFilename = ConfigPath; // Config info Configuration cfg = ConfigurationManager.OpenMappedExeConfiguration(execfgMap,ConfigurationUserLevel.None); // Gets all service model config sections ServiceModelSectionGroup servicemodelSections = ServiceModelSectionGroup.GetSectionGroup(cfg); // Find serivce section matched with the name "this.Description.ServiceType.FullName" if (!ApplySectionInfo(this.Description.ServiceType.FullName,servicemodelSections)) { throw new Exception("ConfigApply Error : There is no endpoint existed in your config!! Please check your config file!"); } this.ApplyMultiBehaviors(servicemodelSections); } /// <summary> /// Check config file! /// </summary> /// <param name="configpath"></param> /// <returns></returns> private bool CheckConfigExist(string configpath) { if (string.IsNullOrEmpty(configpath)) return false; if (!File.Exists(configpath)) return false; return true; } /// <summary> /// Apply section info /// </summary> /// <param name="serviceFullName"></param> /// <param name="servicemodelSections"></param> /// <returns></returns> private bool ApplySectionInfo(string serviceFullName, ServiceModelSectionGroup servicemodelSections) { // Check config sections (!including one section at least!) if (servicemodelSections == null) return false; // Service name can't be none! if (string.IsNullOrEmpty(serviceFullName)) return false; bool isElementExist = false; foreach (ServiceElement element in servicemodelSections.Services.Services) { if (element.Name == serviceFullName) { // Find successfully & apply section info of config file base.LoadConfigurationSection(element); // Find service element successfully isElementExist = true; break; } } return isElementExist; } /// <summary> /// Add behaviors /// </summary> /// <param name="servicemodelSections"></param> /// <returns></returns> private bool ApplyMultiBehaviors(ServiceModelSectionGroup servicemodelSections) { if (servicemodelSections == null) return false; foreach (ServiceBehaviorElement element in servicemodelSections.Behaviors.ServiceBehaviors) { foreach (BehaviorExtensionElement behavior in element) { BehaviorExtensionElement behaviorEx = behavior; object extention = behaviorEx.GetType().InvokeMember("CreateBehavior", BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance, null, behaviorEx, null); if (extention == null) continue; IServiceBehavior isb = (IServiceBehavior)extention; //if (base.Description.Behaviors.Contains(isb)) break; bool isbehaviorExisted = false; foreach (IServiceBehavior i in base.Description.Behaviors) { if (i.GetType().Name == isb.GetType().Name) { isbehaviorExisted = true; break; } } if (isbehaviorExisted) break; base.Description.Behaviors.Add((IServiceBehavior)extention); } } return true; } }
像上面那樣,我們就把配置文件移到了MyApp.config。如果是控制臺程序類的WCF服務就可以像下面那樣來啟動:
using (MyServiceHost host=new MyServiceHost(typeof(Service1))) { //... do something host.Open(); }
而對于宿主是IIS的WCF服務,我們還需要再重寫ServiceHostFactory:
public class MyServiceHostFactory : ServiceHostFactory { protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses) { return new MyServiceHost(serviceType, baseAddresses); } }
然后在WCF服務的svc文件中,指定factory屬性的值為我們重寫的ServiceHostFactory:
<%@ ServiceHost Factory="WcfService1.MyServiceHostFactory" Language="C#" Debug="true" Service="WcfService1.Service1" CodeBehind="Service1.svc.cs" %>
文章列表
全站熱搜