自定义WCF的配置文件2012-01-02 博客园 geff zhangWCF的承载既可以通过编码实现,也能够通过配置实现.而且使用配置,更有利于日后的维护和扩展。我们经常会碰到这样的一个场景:需要把WCF的配置信息放在一个单独的文件中,这种情况经常出现在需要为自己开发的服务配置,需要采用独立的配置文件,而不是只能放到app.config/web.config中。.NET提供了一种机制是通过ConfigSource。例如在asp.net的在站点的默认 Web.Config 文件中使用:<appSettings configSource="customAppSetting.config"/>然后新建 customAppSetting.Config 文件:
<?xml version="1.0" encoding="utf-8"?><appSettings><add key="IsDev" value="True"/></appSettings>
在网站运行时,如果修改 Web.Config 文件会引起站点的重启,而修改 My.Config 文件则不会,同时也提高了配置文件的可读性。然而WCF的配置上configSource是无效的,那么WCF如何自定义配置文件?WCF的ServiceHost和ChannelFactory<T>分别提供了服务端和客户端的可扩展解决方案。下面针对这两个对象分别说明如何自定义服务端和客户端的配置文件。1、服务端自定义配置文件:在ServiceHost的父类ServiceHostBase中,有一个和配置文件的加载密切相关的方法,它为:
protected virtual void ApplyConfiguration();
这个方法用于将应用程序配置文件中<system.serviceModel>节点下的配置信息,转换成WCF的具体服务设置。那么重写这个方法,代码如下:
/// <summary>/// override ApplyConfiguration to load config from custom file/// </summary>protected override void ApplyConfiguration(){//get custom config file name by our rule: config file name = ServiceType.Namevar myConfigFileName = this.Description.ServiceType.FullName;//get config file pathstring dir = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;string myConfigFilePath = System.IO.Path.Combine(dir, myConfigFileName + ".config");if (!System.IO.File.Exists(myConfigFilePath)){base.ApplyConfiguration();return;}var configFileMap = new System.Configuration.ExeConfigurationFileMap();configFileMap.ExeConfigFilename = myConfigFilePath;var config = System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);var serviceModel = System.ServiceModel.Configuration.ServiceModelSectionGroup.GetSectionGroup(config);if (serviceModel == null){base.ApplyConfiguration();return;}foreach (ServiceElement serviceElement in serviceModel.Services.Services){if (serviceElement.Name == this.Description.ServiceType.FullName){LoadConfigurationSection(serviceElement);return;}}throw new Exception("there is no service element match the description!");}}}