惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

G
GRAHAM CLULEY
V
V2EX
WordPress大学
WordPress大学
博客园 - Franky
Last Week in AI
Last Week in AI
博客园 - 司徒正美
有赞技术团队
有赞技术团队
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 【当耐特】
V
Visual Studio Blog
C
CERT Recently Published Vulnerability Notes
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Jina AI
Jina AI
Attack and Defense Labs
Attack and Defense Labs
腾讯CDC
The Hacker News
The Hacker News
Hugging Face - Blog
Hugging Face - Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
J
Java Code Geeks
人人都是产品经理
人人都是产品经理
阮一峰的网络日志
阮一峰的网络日志
T
Tailwind CSS Blog
S
SegmentFault 最新的问题
大猫的无限游戏
大猫的无限游戏
小众软件
小众软件
A
Arctic Wolf
量子位
博客园 - 聂微东
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
N
News and Events Feed by Topic
雷峰网
雷峰网
博客园_首页
Google Online Security Blog
Google Online Security Blog
Spread Privacy
Spread Privacy
罗磊的独立博客
H
Hacker News: Front Page
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
月光博客
月光博客
TaoSecurity Blog
TaoSecurity Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
博客园 - 三生石上(FineUI控件)
宝玉的分享
宝玉的分享
IT之家
IT之家
The Cloudflare Blog
爱范儿
爱范儿
博客园 - 叶小钗
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Apple Machine Learning Research
Apple Machine Learning Research
酷 壳 – CoolShell
酷 壳 – CoolShell

博客园 - Jun1st

Application之间共享Master Page ASP.NET AJAX 4的Client-Side Template和DataView ASP.NET AJAX 4的Client-Side Template和DataView 体验ASP.NET4之ClientID 体验ASP.NET 4之URL Routing 使用Extension Methods来使IDataReader更加方便 USE HttpRuntime.Cache OVER HttpContext.Current.Cache Make Asynchronous Calls from Page IIS and VS Embedded Local Web Server Integrate jQuery with ASP.NET Data Controls Tech-Ed 2008 上海 ASP.NET MVC的分页和导航 LINQ and Pipeline Pattern Why Would a .Net Programmer Learn Ruby On Rails(翻译) ASP.NET MVC之AJAX Asp.Net MVC---Walkthrough Asp.Net MVC 入门篇——Overview Asp.Net 2.0之SqlCacheDependency Security Basics and ASP.NET Support(翻译)
Custom WCF Configuration File
Jun1st · 2009-07-18 · via 博客园 - Jun1st

2009-07-18 13:51  Jun1st  阅读(1859)  评论()    收藏  举报

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