有时我们需要将WCF的配置文件放在单独的配置文件中,而默认情况下WCF又是在web.config或app.config中去寻找服务配置。如果我们把配置文件放在另一个config文件中,如何让WCF知道呢?

答案就是重写ServiceHost。在重写中告诉WCF配置文件的路径。

   public class MyServiceHost:ServiceHost
{
private string ConfigPath =System.AppDomain.CurrentDomain.BaseDirectory+ "MyApp.config"; public MyServiceHost(Type serviceType, params Uri[] baseAddresses) :
base(serviceType, baseAddresses)
{
} protected override void ApplyConfiguration()
{
// Check user config invalidation
if (!CheckConfigExist(ConfigPath))
{
// Use default config
base.ApplyConfiguration();
return;
}
//base.ApplyConfiguration();
// Use user config
ExeConfigurationFileMap execfgMap = new ExeConfigurationFileMap();
// Set user config FilePath
execfgMap.ExeConfigFilename = ConfigPath;
// Config info
Configuration cfg = ConfigurationManager.OpenMappedExeConfiguration(execfgMap,ConfigurationUserLevel.None);
// Gets all service model config sections
ServiceModelSectionGroup servicemodelSections = ServiceModelSectionGroup.GetSectionGroup(cfg); // Find serivce section matched with the name "this.Description.ServiceType.FullName"
if (!ApplySectionInfo(this.Description.ServiceType.FullName,servicemodelSections))
{
throw new Exception("ConfigApply Error : There is no endpoint existed in your config!! Please check your config file!");
}
this.ApplyMultiBehaviors(servicemodelSections); } /// <summary>
/// Check config file!
/// </summary>
/// <param name="configpath"></param>
/// <returns></returns>
private bool CheckConfigExist(string configpath)
{
if (string.IsNullOrEmpty(configpath)) return false;
if (!File.Exists(configpath)) return false;
return true;
} /// <summary>
/// Apply section info
/// </summary>
/// <param name="serviceFullName"></param>
/// <param name="servicemodelSections"></param>
/// <returns></returns>
private bool ApplySectionInfo(string serviceFullName, ServiceModelSectionGroup servicemodelSections)
{
// Check config sections (!including one section at least!)
if (servicemodelSections == null) return false;
// Service name can't be none!
if (string.IsNullOrEmpty(serviceFullName)) return false;
bool isElementExist = false;
foreach (ServiceElement element in servicemodelSections.Services.Services)
{
if (element.Name == serviceFullName)
{
// Find successfully & apply section info of config file
base.LoadConfigurationSection(element);
// Find service element successfully
isElementExist = true;
break;
}
}
return isElementExist;
} /// <summary>
/// Add behaviors
/// </summary>
/// <param name="servicemodelSections"></param>
/// <returns></returns>
private bool ApplyMultiBehaviors(ServiceModelSectionGroup servicemodelSections)
{
if (servicemodelSections == null) return false;
foreach (ServiceBehaviorElement element in servicemodelSections.Behaviors.ServiceBehaviors)
{
foreach (BehaviorExtensionElement behavior in element)
{
BehaviorExtensionElement behaviorEx = behavior;
object extention = behaviorEx.GetType().InvokeMember("CreateBehavior",
BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance,
null,
behaviorEx,
null);
if (extention == null) continue;
IServiceBehavior isb = (IServiceBehavior)extention;
//if (base.Description.Behaviors.Contains(isb)) break;
bool isbehaviorExisted = false;
foreach (IServiceBehavior i in base.Description.Behaviors)
{
if (i.GetType().Name == isb.GetType().Name)
{
isbehaviorExisted = true;
break;
}
}
if (isbehaviorExisted) break;
base.Description.Behaviors.Add((IServiceBehavior)extention);
}
}
return true;
} }

像上面那样,我们就把配置文件移到了MyApp.config。如果是控制台程序类的WCF服务就可以像下面那样来启动:

          using (MyServiceHost host=new MyServiceHost(typeof(Service1)))
{
//... do something
host.Open();
}

而对于宿主是IIS的WCF服务,我们还需要再重写ServiceHostFactory:

 public class MyServiceHostFactory :  ServiceHostFactory
{
protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
{
return new MyServiceHost(serviceType, baseAddresses);
}
}

然后在WCF服务的svc文件中,指定factory属性的值为我们重写的ServiceHostFactory:

<%@ ServiceHost Factory="WcfService1.MyServiceHostFactory"  Language="C#" Debug="true" Service="WcfService1.Service1" CodeBehind="Service1.svc.cs" %>

WCF重写ServiceHost,实现独立配置文件的更多相关文章

  1. IOC容器Unity的使用及独立配置文件Unity.Config

    [本段摘录自:IOC容器Unity 使用http://blog.csdn.net/gdjlc/article/details/8695266] 面向接口实现有很多好处,可以提供不同灵活的子类实现,增加 ...

  2. log4net保存到数据库系列二:独立配置文件中配置log4net

    园子里面有很多关于log4net保存到数据库的帖子,但是要动手操作还是比较不易,从头开始学习log4net数据库日志一.WebConfig中配置log4net 一.WebConfig中配置log4ne ...

  3. 改进uwsgi启动脚本,使其支持多个独立配置文件

    最近在研究flask,在架设运行环境的时候犯了难.因为我想把每个独立的应用像NGINX处理多个网站那样,每个应用单独一个配置文件.而网上流传的uwsgi启动脚本都只支持单个配置文件.虽然有文章说可以把 ...

  4. WCF 自托管、无配置文件实现jsonp(跨域)的访问

    以下内容基于WCF4.0,本文将对比讨论配置文件方案和无配置文件方案的实现方式. WCF4.0加入了对RESTFU和标准终结点的支持,这为实现跨域提供了简单的方式. 一.有配置文件的情况: 首先我们先 ...

  5. Redis 哨兵节点之间相互自动发现机制(自动重写哨兵节点的配置文件)

    Redis的哨兵机制中,如果是多哨兵模式,哨兵节点之间也是可以相互感知的,各种搜索之后出来的是千篇一律的一个基础配置文件,在配置当前哨兵节点的配置文件中,并没有配置其他哨兵节点的任何信息.如下是一个哨 ...

  6. log4net独立配置文件配置(winfrom)

    log4net配置很多,具体配置步骤不细说,具体说出个人遇到的问题. 在winfrom和web应用程序中配置,在默认配置文件配置都没问题,因为EF也写在默认配置文件中,就会冲突解决办法就是将log4. ...

  7. WCF全面解析之三 使用配置文件启动WCF服务

    知识:WCF地址.WCF绑定 Endpoint的配置 服务的三要素(ABC) A:Address 地址 有传输方式信息 B:Binding 怎么做(与地址的传输方式要匹配) C:Contract 做什 ...

  8. WCF的行为与异常-------配置文件说明

    ServiceBehavior and OperationBehavior(这些都是应用在实现类上) http://msdn.microsoft.com/zh-cn/library/system.se ...

  9. [WCF REST] WebServiceHost 不依赖配置文件启动简单服务

    最近用WPF启动 WCF REST 服务,发现app.config 配置好烦,简单一个exe 可以到处搬动,还非得带一个累赘配置,不小心丢了程序就跑不起来. 最后决定,砍去WCF配置项,用WebSer ...

随机推荐

  1. 响应性web设计实战总结

    响应性web设计实战 响应性web设计的理念是:页面的设计与开发应当根据用户行为与设备环境(包括系统平台,屏幕尺寸,屏幕定向等)进行相应的响应及调整.具体的实践方式由多方面组成,包括弹性网格和布局,图 ...

  2. Roslyn词法分析器初使用

    需:install-package:Microsoft.CodeAnalysis ];         NamespaceDeclarationSyntax NameSpaceDeclaration  ...

  3. 大数据BI积累

    http://blog.csdn.net/wyzxg/article/category/535869 设计论文:http://www.doc88.com/p-3877368345851.html 自动 ...

  4. uml面向对象建模基础总结

    uml九种图,其中的细节不说了.在后面的具体使用中提到这九种图. 建模流程: 1.分析需求. 2.通过分析名词,发现类,使用到类图. 3.建立用例模型,通过参与者分析用例,使用到用例图. 4.为用例建 ...

  5. 第12天 android studio

    1. http://jingyan.baidu.com/article/215817f7888dc21eda14230d.html Gradle DSL method not found:‘andro ...

  6. keepalived安装

    两台虚拟机 两台配置操作一样 环境配置 [root@lb01 /]# yum -y install openssl openssl-devel [root@lb01 /]# yum -y instal ...

  7. C++Socket编程总结 [转]

    使用socket写代码主要是要看自己的需求是什么. 如果通信时,内容很重要就要使TCP方式. 如果用户数太多,可能就要使用UDP方式了. 在TCP模式下,最简单的方式就是这样的,使阻塞方式: 服务端: ...

  8. Eclipse的link方式安装JBPM6插件(JBPM学习之一)

    1. 首先下载最新的JAVA开发最受欢迎的Eclipse IDE工具,下载地址:http://www.eclipse.org/downloads/ 2. 然后去JBPM社区去下载最新的JBPM6,下载 ...

  9. 获取action name在asp.net mvc

    Update for MVC 3 ViewContext.Controller.ValueProvider.GetValue("action").RawValue ViewCont ...

  10. Android自定义标题栏

    预览一下效果: 素材: 新建一个布局title_bar.xml,代码如下: <?xml version="1.0" encoding="utf-8"?&g ...