Custom WCF Configuration File
2009-07-18 13:51 Jun1st 阅读(1803) 评论(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:
这里的所指的Description就是ServiceDescription
此时基本上可以肯定了,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了
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述