WCF服务自定义配置 [英] WCF Service Custom Configuration
问题描述
在承载几个WCF服务的应用程序,这将是添加自定义的配置信息,每个服务的最佳方法是什么?例如,您可能想通过或设置公司名称或指定的connectionString服务或其他一些参数。
In an application that is hosting several WCF services, what would be the best way to add custom configuration information for each service? For example you may want to pass or set a company name or specify the connectionString a service or some other parameter.
我猜这是可能通过实施IServiceBehavior接口。
I'm guessing this might be possible by implementing IServiceBehavior.
即是这样......
i.e something like....
<behaviors>
<serviceBehaviors>
<behavior name="MyBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug />
<customBehavior myCompany="ABC" />
</behavior>
<behavior name="MyOtherBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug />
<customBehavior myCompany="DEF" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="MyBehavior" name="MyNameSpace.MyService">
<endpoint address="" behaviorConfiguration="" binding="netTcpBinding"
name="TcpEndpoint" contract="MyNameSpace.IMyService" />
<endpoint address="mex" binding="mexTcpBinding" bindingConfiguration=""
name="TcpMexEndpoint" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:4000/MyService" />
</baseAddresses>
</host>
</service>
<service behaviorConfiguration="MyOtherBehavior" name="MyNameSpace.MyOtherService">
<endpoint address="" behaviorConfiguration="" binding="netTcpBinding"
name="TcpEndpoint" contract="MyNameSpace.IMyOtherService" />
<endpoint address="mex" binding="mexTcpBinding" bindingConfiguration=""
name="TcpMexEndpoint" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:4000/MyOtherService" />
</baseAddresses>
</host>
</service>
</services>
会在为MyService和DEF对MyOtherService设置ABC(假设他们有一个公司的名字了一些常用的接口)。
Would set ABC on MyService and DEF on MyOtherService (assuming they have some common interface with a company name).
任何人都可以详细阐述如何实现这一点?
Can anyone elaborate on how you implement this?
TIA
迈克尔
推荐答案
我知道这是旧的,但它从未被标记为回答,所以我想我会采取了一枪。如果我理解你以后,你可以用自定义ServiceHostFactory做到这一点。
好的职位在此<一个href="http://blogs.msdn.com/dotnetinterop/archive/2008/09/22/custom-service-config-file-for-a-wcf-service-hosted-in-iis.aspx">here.
I know this is old, but it was never marked answered, so I thought I'd take a shot. If I understand what you're after, you can do it with a custom ServiceHostFactory.
Good post on this here.
您设置yuour定制ServiceHostFactory像这样:
You set up yuour custom ServiceHostFactory like so:
<%@ ServiceHost
Language="C#"
Debug="true"
Service="Ionic.Samples.Webservices.Sep20.CustomConfigService"
Factory="Ionic.ServiceModel.ServiceHostFactory"%>
然后,在你ServiceHostFactory,可以覆盖一个名为ApplyConfiguration方法。通常情况下为WCF应用程序托管在IIS,WCF自动寻找将为配置在web.config中。在这个例子中,我们重写该行为首先寻找之后,WCF服务说明命名的配置文件。
Then, in your ServiceHostFactory, you can override a method called ApplyConfiguration. Normally for WCF apps hosted in IIS, WCF would automatically look for config in web.config. In this example, we override that behavior to first look for a config file named after the WCF Service Description.
protected override void ApplyConfiguration()
{
// generate the name of the custom configFile, from the service name:
string configFilename = System.IO.Path.Combine ( physicalPath,
String.Format("{0}.config", this.Description.Name));
if (string.IsNullOrEmpty(configFilename) || !System.IO.File.Exists(configFilename))
base.ApplyConfiguration();
else
LoadConfigFromCustomLocation(configFilename);
}
您可以用什么替换 - 比如,寻找配置数据库表中。
You could replace this with "anything" - for example, looking for config in a database table.
这几个方法完成拼图。
private string _physicalPath = null;
private string physicalPath
{
get
{
if (_physicalPath == null)
{
// if hosted in IIS
_physicalPath = System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath;
if (String.IsNullOrEmpty(_physicalPath))
{
// for hosting outside of IIS
_physicalPath= System.IO.Directory.GetCurrentDirectory();
}
}
return _physicalPath;
}
}
private void LoadConfigFromCustomLocation(string configFilename)
{
var filemap = new System.Configuration.ExeConfigurationFileMap();
filemap.ExeConfigFilename = configFilename;
System.Configuration.Configuration config =
System.Configuration.ConfigurationManager.OpenMappedExeConfiguration
(filemap,
System.Configuration.ConfigurationUserLevel.None);
var serviceModel = System.ServiceModel.Configuration.ServiceModelSectionGroup.GetSectionGroup(config);
bool loaded= false;
foreach (System.ServiceModel.Configuration.ServiceElement se in serviceModel.Services.Services)
{
if(!loaded)
if (se.Name == this.Description.ConfigurationName)
{
base.LoadConfigurationSection(se);
loaded= true;
}
}
if (!loaded)
throw new ArgumentException("ServiceElement doesn't exist");
}
这篇关于WCF服务自定义配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!