gpt4 book ai didi

.net - WCF 服务自定义配置

转载 作者:行者123 更新时间:2023-12-04 13:56:17 26 4
gpt4 key购买 nike

在托管多个 WCF 服务的应用程序中,为每个服务添加自定义配置信息的最佳方式是什么?例如,您可能想要传递或设置公司名称或指定连接字符串服务或其他一些参数。

我猜这可能通过实现 IServiceBehavior 来实现。

即类似....

<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 上设置 ABC,在 MyOtherService 上设置 DEF(假设它们有一些带有公司名称的通用接口(interface))。

谁能详细说明你是如何实现的?

TIA

迈克尔

最佳答案

我知道这是旧的,但它从未被标记为已回答,所以我想我会试一试。如果我了解您的需求,您可以使用自定义 ServiceHostFactory 来完成。
好帖子here .

您像这样设置您的自定义 ServiceHostFactory:

<%@ ServiceHost
Language="C#"
Debug="true"
Service="Ionic.Samples.Webservices.Sep20.CustomConfigService"
Factory="Ionic.ServiceModel.ServiceHostFactory"%>

然后,在您的 ServiceHostFactory 中,您可以覆盖一个名为 ApplyConfiguration 的方法。通常对于托管在 IIS 中的 WCF 应用程序,WCF 会自动在 web.config 中查找配置。在此示例中,我们覆盖该行为以首先查找以 WCF 服务描述命名的配置文件。
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);
}

您可以将其替换为“任何内容” - 例如,在数据库表中查找配置。

还有一些方法可以完成这个难题。
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");
}

关于.net - WCF 服务自定义配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/211122/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com