简体   繁体   English

同一文件中的数据库和WCF的App.Config ConnectionString

[英]App.Config ConnectionString for DataBase and WCF in same file

I've two separate App.Config files that work perfectly. 我有两个单独的App.Config文件,它们可以完美地工作。

The configuration file containing the database connection: 包含数据库连接的配置文件:

<?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=&quot;data source=localhost\SQL2008EXPRESS;initial catalog=EmsDatabase;integrated security=True;multipleactiveresultsets=True;App=EntityFramework&quot;" 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=&quot;Data Source=|DataDirectory|\Data\LocalEmsDatabase.sdf&quot;" providerName="System.Data.EntityClient" /></connectionStrings>    
</configuration>

The configuration file containing the WCF settings: 包含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>

But the configuration doesn't work if I copy the content of both files together into one file. 但是,如果我将两个文件的内容一起复制到一个文件中,则该配置将不起作用。 It depends on which configuration is on top of the file which part of the configuration could be read. 取决于哪个配置位于文件顶部,可以读取配置的哪一部分。 If the WCF configuration is on top it will work but I couldn't connect to the database. 如果WCF配置位于最上面,则可以使用,但是我无法连接到数据库。 If the database connection is on top it will work but I couldn't open the service host. 如果数据库连接位于顶部,则可以使用,但是我无法打开服务主机。

The whole file: 整个文件:

<?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=&quot;data source=localhost\SQL2008EXPRESS;initial catalog=EmsDatabase;integrated security=True;multipleactiveresultsets=True;App=EntityFramework&quot;" 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=&quot;Data Source=|DataDirectory|\Data\LocalEmsDatabase.sdf&quot;" 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>

What do I have to do to put both configurations into one file and make it work? 我要怎么做才能将两种配置都放在一个文件中并使之工作?

I tried to find a simple way to use a custom WCF configuration file but I only found the following links that need a lot of code to be able to read the file. 我试图找到一种使用自定义WCF配置文件的简单方法,但我仅发现以下链接,需要大量代码才能读取该文件。 I couldn't believe that this amount of code is needed for the simple task to read configuration another file containing the same content. 我简直不敢相信,执行简单任务以读取配置另一个包含相同内容的文件时,就需要大量的代码。

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 从自定义源(非标准XML)加载WCF配置(用于服务器+客户端) 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/zh-CN/library/aa395224(v=vs.100).aspx

I hope there is a simple way to put both configurations into only one App.config file. 我希望有一种简单的方法可以将两种配置都放入一个App.config文件中。

如果您像下面那样访问它,应该没有问题-ConfigurationManager.ConnectionStrings [“ ConnectionStringName”]。ConnectionString

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM