我有兩個單獨的App.Config文件,完美的工作。App.Config用於DataBase和WCF的ConnectionString在同一文件
包含數據庫連接配置文件:
<?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文件的簡單方法。
同時結合兩個配置文件,你如何使用/訪問連接字符串做數據庫連接。 ? –
如果你像下面一樣訪問它,它應該沒有任何問題 - ConfigurationManager.ConnectionStrings [「ConnectionStringName」]。ConnectionString –
Thx爲提示。它解決了這個問題。我們將連接字符串的名稱轉發給System.Data的ObjectContext。我們可以轉發整個工作的字符串。 –