我可以在WCF.config文件中嵌套连接字符串吗?
我正在使用一个web应用程序,该应用程序具有具有多个调用的WCF web服务。我们正试图将这些调用划分为属于同一业务功能的单独调用模块,其中一个核心模块包含所有通用设置 但是,我们有一种情况,其中一个模块需要一个不同的数据库连接,但它是特定于业务功能的,因此不包含在core.config中我可以在WCF.config文件中嵌套连接字符串吗?,wcf,web-config,wcf-data-services,Wcf,Web Config,Wcf Data Services,我正在使用一个web应用程序,该应用程序具有具有多个调用的WCF web服务。我们正试图将这些调用划分为属于同一业务功能的单独调用模块,其中一个核心模块包含所有通用设置 但是,我们有一种情况,其中一个模块需要一个不同的数据库连接,但它是特定于业务功能的,因此不包含在core.config中 <configuration> <connectionStrings> <add name="connectionName" providerName="
<configuration>
<connectionStrings>
<add name="connectionName" providerName="System.Data.SqlClient"
connectionString="Data Source=server,1111;Database=Whatever;Trusted_Connection=True;Min Pool Size=0;Max Pool Size=50;" />
</connectionStrings>
<system.serviceModel>
<services>
<service name="Foo.DataFeedService" behaviorConfiguration="FooServiceBehavior">
<endpoint binding="basicHttpBinding" bindingConfiguration="FooBasicHttpBinding"
name="Foo" contract="Foo.IDataFeedService"/>
</service>
</services>
</system.serviceModel>
</configuration>
本质上,这只是一个带有连接字符串的服务存根。我们的其他每个.config文件都是这样构造的,服务模型在每个文件中都是存根的,sans-
。它们都工作正常。但是,引入连接字符串会导致问题。将连接字符串移动到根配置中解决了这个问题,这与我们试图通过分割每个业务功能来实现的目标背道而驰。有任何提示吗?不可能在多个配置文件中分离出连接字符串,也不可能在单个配置文件中的不同位置定义它们
<configuration>
<configSections>
<section name="dataFeedServiceSettings" type="DataFeedServiceSettings" />
<section name="otherServiceSettings" type="OtherServiceSettings" />
</configSections>
<dataFeedServiceSettings connectionString="[connectionstring1]"
propertyA="value1" />
<otherServiceSettings connectionString="[connectionstring2]"
propertyB="value2" />
</configuration>
如果您有一个应用程序,并且您正试图按业务功能分离设置,则可以使用自定义配置部分
A.为服务的每个应用程序设置定义自定义配置节类:
public class DataFeedServiceSettings : ConfigurationSection
{
[ConfigurationProperty("connectionString", IsRequired=true)]
public string ConnectionString { get; set; }
[ConfigurationProperty("propertyA", IsRequired = true)]
public string PropertyA { get; set; }
}
public class OtherServiceSettings : ConfigurationSection
{
[ConfigurationProperty("connectionString", IsRequired = true)]
public string ConnectionString { get; set; }
[ConfigurationProperty("propertyB", IsRequired = true)]
public string PropertyB { get; set; }
}
B.将配置部分添加到配置文件中
<configuration>
<configSections>
<section name="dataFeedServiceSettings" type="DataFeedServiceSettings" />
<section name="otherServiceSettings" type="OtherServiceSettings" />
</configSections>
<dataFeedServiceSettings connectionString="[connectionstring1]"
propertyA="value1" />
<otherServiceSettings connectionString="[connectionstring2]"
propertyB="value2" />
</configuration>
配置部分也可以位于单独的文件中
<configuration>
<configSections>
<section name="dataFeedServiceSettings" type="DataFeedServiceSettings" />
<section name="otherServiceSettings" type="OtherServiceSettings" />
</configSections>
<dataFeedServiceSettings configSource="file1.config" />
<otherServiceSettings configSource="file2.config" />
</configuration>
File1.config
<?xml version='1.0' encoding='utf-8'?>
<dataFeedServiceSettings connectionString="[connectionstring1]"
propertyA="value1" />
File2.config
<?xml version='1.0' encoding='utf-8'?>
<otherServiceSettings connectionString="[connectionstring2]"
propertyB="value2" />
这种方法的缺点是连接字符串不再位于ConnectionString部分,但是您可以对“业务模块”进行分组在他们自己的部分中,将业务配置移动到他们自己的文件中。不可能在多个配置文件中分离出连接字符串,也不可能在单个配置文件中的不同位置定义它们
<configuration>
<configSections>
<section name="dataFeedServiceSettings" type="DataFeedServiceSettings" />
<section name="otherServiceSettings" type="OtherServiceSettings" />
</configSections>
<dataFeedServiceSettings connectionString="[connectionstring1]"
propertyA="value1" />
<otherServiceSettings connectionString="[connectionstring2]"
propertyB="value2" />
</configuration>
如果您有一个应用程序,并且您正试图按业务功能分离设置,则可以使用自定义配置部分
A.为服务的每个应用程序设置定义自定义配置节类:
public class DataFeedServiceSettings : ConfigurationSection
{
[ConfigurationProperty("connectionString", IsRequired=true)]
public string ConnectionString { get; set; }
[ConfigurationProperty("propertyA", IsRequired = true)]
public string PropertyA { get; set; }
}
public class OtherServiceSettings : ConfigurationSection
{
[ConfigurationProperty("connectionString", IsRequired = true)]
public string ConnectionString { get; set; }
[ConfigurationProperty("propertyB", IsRequired = true)]
public string PropertyB { get; set; }
}
B.将配置部分添加到配置文件中
<configuration>
<configSections>
<section name="dataFeedServiceSettings" type="DataFeedServiceSettings" />
<section name="otherServiceSettings" type="OtherServiceSettings" />
</configSections>
<dataFeedServiceSettings connectionString="[connectionstring1]"
propertyA="value1" />
<otherServiceSettings connectionString="[connectionstring2]"
propertyB="value2" />
</configuration>
配置部分也可以位于单独的文件中
<configuration>
<configSections>
<section name="dataFeedServiceSettings" type="DataFeedServiceSettings" />
<section name="otherServiceSettings" type="OtherServiceSettings" />
</configSections>
<dataFeedServiceSettings configSource="file1.config" />
<otherServiceSettings configSource="file2.config" />
</configuration>
File1.config
<?xml version='1.0' encoding='utf-8'?>
<dataFeedServiceSettings connectionString="[connectionstring1]"
propertyA="value1" />
File2.config
<?xml version='1.0' encoding='utf-8'?>
<otherServiceSettings connectionString="[connectionstring2]"
propertyB="value2" />
这种方法的缺点是连接字符串不再位于ConnectionString部分,但您可以将“业务模块”分组到它们自己的部分中,并将业务配置移动到它们自己的文件中。是否尝试拥有多个配置文件(每个模块配置)并在每个配置中分离出连接字符串(假设这些模块都在同一个应用程序中)?您是否尝试拥有多个配置文件(每个模块配置)并在每个配置中分离出连接字符串(假设这些模块都在同一个应用程序中)?但这是否特定于WCF web服务?我们在ASP.NET web应用程序中使用了相同的方法(将连接字符串分解为单独的配置),并且工作正常。没有足够的代表对此进行投票,但我确实喜欢此解决方案,并将其传递。连接字符串可以放在单独的文件(单个文件)中通过configSource属性,就像我对配置部分所做的那样,但我不知道有一种方法可以实现多个配置。AppSetting可以通过文件属性具有多个,它将多个合并为一个。但是,ConnectionString部分不存在此属性。尽管如此,我打赌有一种未经记录的(或有记录的,但我找不到相关信息)方式来实现目标。有一个内置的层次结构,您可以在machine.config中定义,然后再在web.config中定义。这里的问题是,如果您定义相同的条目,您会得到一个重复的键错误。您可以在该部分中放置一个清除标记,但随后会丢失machine.config中定义的值。在我看来,似乎web.config文件在WCF中的工作方式与ASP.NET不同,如果是这样,我很好奇为什么。和您一样,我也无法找到有关此问题的文档案例,因此希望这能对将来的人有所帮助。不过,这是WCF web服务特有的吗?我们在ASP.NET web应用程序中使用了相同的方法(将连接字符串分解为单独的配置),并且工作正常。没有足够的代表对此进行投票,但我确实喜欢此解决方案,并将其传递。连接字符串可以放在单独的文件(单个文件)中通过configSource属性,就像我对配置部分所做的那样,但我不知道有一种方法可以实现多个配置。AppSetting可以通过文件属性具有多个,它将多个合并为一个。但是,ConnectionString部分不存在此属性。尽管如此,我打赌有一种未经记录的(或有记录的,但我找不到相关信息)方式来实现目标。有一个内置的层次结构,您可以在machine.config中定义,然后再在web.config中定义。这里的问题是,如果您定义相同的条目,您会得到一个重复的键错误。您可以在该部分中放置一个清除标记,但随后会丢失machine.config中定义的值。在我看来,似乎web.config文件在WCF中的工作方式与ASP.NET不同,如果是这样,我很好奇为什么。像你一样,我还没有找到这个问题的记录案例,所以希望这能对将来的人有所帮助。