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

?

相关文章: