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

推荐订阅源

雷峰网
雷峰网
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
大猫的无限游戏
大猫的无限游戏
Google DeepMind News
Google DeepMind News
V
V2EX
T
The Blog of Author Tim Ferriss
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Hugging Face - Blog
Hugging Face - Blog
Stack Overflow Blog
Stack Overflow Blog
I
InfoQ
博客园_首页
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Last Week in AI
Last Week in AI
Recent Announcements
Recent Announcements
Vercel News
Vercel News
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
T
Tailwind CSS Blog
美团技术团队
Martin Fowler
Martin Fowler
宝玉的分享
宝玉的分享
Blog — PlanetScale
Blog — PlanetScale
GbyAI
GbyAI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
J
Java Code Geeks

博客园 - 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