我有两个独立的App.Config文件可以正常工作。
包含数据库连接的配置文件:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" />
</startup>
<connectionStrings><add name="EmsDatabaseEntities" connectionString="metadata=res://*/Data.EmsEntityModel.csdl|res://*/Data.EmsEntityModel.ssdl|res://*/Data.EmsEntityModel.msl;provider=System.Data.SqlClient;provider connection string="data source=localhost\SQL2008EXPRESS;initial catalog=EmsDatabase;integrated security=True;multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient" /><add name="LocalEmsDatabaseEntities" connectionString="metadata=res://*/Data.LocalEmsEnityModel.csdl|res://*/Data.LocalEmsEnityModel.ssdl|res://*/Data.LocalEmsEnityModel.msl;provider=System.Data.SqlServerCe.3.5;provider connection string="Data Source=|DataDirectory|\Data\LocalEmsDatabase.sdf"" providerName="System.Data.EntityClient" /></connectionStrings>
</configuration>
包含WCF设置的配置文件:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="CheckResultServiceBehavior">
<serviceMetadata httpGetEnabled="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="Ems.CheckResultService" behaviorConfiguration="CheckResultServiceBehavior">
<endpoint
address=""
binding="netTcpBinding"
bindingConfiguration="tcpConfig"
contract="Maha.AIP.Ems.WcfContracts.ICheckResultService_1_0"/>
<endpoint
address="mex"
binding="mexTcpBinding"
bindingConfiguration=""
contract="IMetadataExchange"/>
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:8080/CheckResultService_1_0/"/>
</baseAddresses>
</host>
</service>
</services>
<bindings>
<netTcpBinding>
<binding
name="tcpConfig"
openTimeout="00:01:00"
closeTimeout="00:01:00"
sendTimeout="01:00:00"
receiveTimeout="01:00:00"
maxBufferSize="2147483647"
maxReceivedMessageSize="2147483647"
maxBufferPoolSize="2147483647">
<security mode="None"/>
</binding>
</netTcpBinding>
</bindings>
</system.serviceModel>
</configuration>
但是如果我将两个文件的内容一起复制到一个文件中,则配置不起作用。这取决于文件顶部的哪个配置可以读取哪个部分配置。如果WCF配置在顶部,它将工作,但我无法连接到数据库。如果数据库连接在顶部,它将工作,但我无法打开服务主机。
整个文件:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" />
</startup>
<connectionStrings><add name="EmsDatabaseEntities" connectionString="metadata=res://*/Data.EmsEntityModel.csdl|res://*/Data.EmsEntityModel.ssdl|res://*/Data.EmsEntityModel.msl;provider=System.Data.SqlClient;provider connection string="data source=localhost\SQL2008EXPRESS;initial catalog=EmsDatabase;integrated security=True;multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient" /><add name="LocalEmsDatabaseEntities" connectionString="metadata=res://*/Data.LocalEmsEnityModel.csdl|res://*/Data.LocalEmsEnityModel.ssdl|res://*/Data.LocalEmsEnityModel.msl;provider=System.Data.SqlServerCe.3.5;provider connection string="Data Source=|DataDirectory|\Data\LocalEmsDatabase.sdf"" providerName="System.Data.EntityClient" /></connectionStrings>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="CheckResultServiceBehavior">
<serviceMetadata httpGetEnabled="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="Ems.CheckResultService" behaviorConfiguration="CheckResultServiceBehavior">
<endpoint
address=""
binding="netTcpBinding"
bindingConfiguration="tcpConfig"
contract="Maha.AIP.Ems.WcfContracts.ICheckResultService_1_0"/>
<endpoint
address="mex"
binding="mexTcpBinding"
bindingConfiguration=""
contract="IMetadataExchange"/>
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:8080/CheckResultService_1_0/"/>
</baseAddresses>
</host>
</service>
</services>
<bindings>
<netTcpBinding>
<binding
name="tcpConfig"
openTimeout="00:01:00"
closeTimeout="00:01:00"
sendTimeout="01:00:00"
receiveTimeout="01:00:00"
maxBufferSize="2147483647"
maxReceivedMessageSize="2147483647"
maxBufferPoolSize="2147483647">
<security mode="None"/>
</binding>
</netTcpBinding>
</bindings>
</system.serviceModel>
</configuration>
如何将两个配置放在一个文件中并使其有效?我需要做些什么?
我试图找到一种使用自定义WCF配置文件的简单方法,但我发现以下链接需要大量代码才能读取该文件。我无法相信这个简单的任务需要这么多的代码来读取配置包含相同内容的另一个文件。
Loading WCF config (for server + client) from custom source (not standard XML) http://blogs.msdn.com/b/dotnetinterop/archive/2008/09/22/custom-service-config-file-for-a-wcf-service-hosted-in-iis.aspx http://msdn.microsoft.com/en-us/library/aa395224(v=vs.100).aspx
我希望有一种简单的方法可以将两个配置只放入一个App.config文件中。
答案 0 :(得分:1)
如果你像下面那样访问它应该不会有任何问题 - ConfigurationManager.ConnectionStrings [“ConnectionStringName”]。ConnectionString