代码改变世界

Custom WCF Configuration File

2009-07-18 13:51  Jun1st  阅读(1796)  评论(7编辑  收藏  举报

Summary

在写WCF的各种Service时,通常我们都会选择通过使用App.config或者Web.config来配置我们的Service。但是,当我们的程序要在不同的环境上测试或运行的时候,而作为开发人员的你在某些环境上并没有管理的权限时,通过唯一的App.config或者Web.config来配置Service就会造成一定程度上的麻烦。本文介绍了如何将这些config信息写在自定义的文件中,并且本文侧重于使用IIS作为host方式运行的Service。

Reading Config File

既然想把Service的配置信息写在自定义的config文件而非web.config中,那么就得找到读取config文件的函数,并且重写这个函数,已实现自己想要的功能。ServiceHostBase.ApplyConfiguration:

Loads the service description information from the configuration file and applies it to the runtime being constructed

这里的所指的Description就是ServiceDescription

Represents a complete, in-memory description of the service, including all the endpoints for the service and specifications for their respective addresses, bindings, contracts and behaviors.

此时基本上可以肯定了,We are on the way. And It’s time to get our hands dirty to write our own ServiceHost which will read the configuration file from the location where we want.

实现ApplyConfiguration

01.protected override void ApplyConfiguration()
02.{
03.    string configFileName = System.Configuration.ConfigurationManager.AppSettings["ServiceConfigFile"];
04.    if (string.IsNullOrEmpty(configFileName))
05.    {
06.        configFileName = "Config\\Service.config";
07.    }
08.    string filePath = System.IO.Path.Combine(physicalPath, configFileName);
09.    if (!System.IO.File.Exists(filePath))
10.    {
11.        base.ApplyConfiguration();
12.        return;
13.    }
14.    var configFileMap = new System.Configuration.ExeConfigurationFileMap();
15.    configFileMap.ExeConfigFilename = filePath;
16.    var config = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);
17.    var serviceModel = System.ServiceModel.Configuration.ServiceModelSectionGroup.GetSectionGroup(config);
18.    bool loaded = false;
19.    foreach (System.ServiceModel.Configuration.ServiceElement se in serviceModel.Services.Services)
20.    {
21.        if (!loaded)
22.        {
23.            if (se.Name == this.Description.ConfigurationName)
24.            {
25.                base.LoadConfigurationSection(se);
26.                loaded = true;
27.            }
28.        }
29.    }
30.    if (!loaded)
31.        throw new ArgumentException("ServiceElement doesn't exist");
32.}

通过从AppSetting中的ServiceConfigFile来设置config文件的相对路径,然后再和PhysicalPath合并来得到文件的绝对路径,如果这个指定的这个文件不存在,那么就调用base.ApplyConfiguration(),从web.config文件中读取信息。

Set ServiceHostFactory

在使用IIS作为Host时,我们无法通过编程的方式来使用我们自己的ServiceHost,只有一个.svc文件可供我们使用。幸好在这个svc文件中,我们可以指定Factory

1.<%@ ServiceHost    Language="C#"     Debug="true"     Service="Samples.CustomConfigService"     Factory="Samples.LocalServiceHostFactory"%>

而写这个LocalServiceHostFactory也是很简单的

1.public class LocalServiceHostFactory : ServiceHostFactory
2.    {
3.        protected override System.ServiceModel.ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
4.        {
5.            return new LocalServiceHost(serviceType, baseAddresses);
6.        }
7.    }

Conclusion

That’s all
现在,在不同的环境下,只要指向预先设置好的不同的config文件就可以,不用再没到一个环境就需要改web.config了

References

  1. http://blogs.msdn.com/dotnetinterop/default.aspx
  2. http://msdn.microsoft.com