2012-10-03 2 views
1

여러 호출을 사용하는 WCF 웹 서비스가있는 웹 응용 프로그램을 사용하고 있습니다. 우리는 전화를 동일한 비즈니스 기능에 속하는 별도의 모듈으로 분할하려고합니다. 핵심 모듈은 모든 일반 설정을 수용합니다.WCF .config 파일에 연결 문자열을 중첩시킬 수 있습니까?

그러나 모듈 중 하나에 다른 데이터베이스 연결이 필요하지만 비즈니스 기능에만 해당되므로 핵심 .config에 포함되지 않은 경우가 있습니다.

<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 파일 각각은 서비스 모델이 각각 stubbed 된 sans-<connectionString>과 같은 방식으로 구성됩니다. 그들은 모두 올바르게 작동합니다. 연결 문자열을 도입하면 문제가 발생합니다. 연결 문자열을 루트 config로 이동하면 문제를 해결할 수 있으며 각 비즈니스 기능을 세그먼트 화하여 달성하고자하는 목표를 달성 할 수 있습니다. 어떤 힌트?

+0

여러 구성 파일 (모듈 당 구성)을 가지고 각 구성에서 connectionString을 분리하려고합니까? (이 모듈은 모두 동일한 응용 프로그램에 있다고 가정합니다)? –

답변

1

연결 문자열을 여러 구성 파일에서 분리하거나 단일 구성 파일의 별도 위치에 정의 할 수 없습니다.

응용 프로그램이 하나 있고 비즈니스 기능별 설정을 분리하려는 경우 사용자 지정 구성 섹션을 사용할 수 있습니다.

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" /> 

연결 문자열이 더 이상 connectionStrings 섹션에 있습니다 것이이 방법의 부정하지만, 당신이 얻을 자신의 섹션에서 "비즈니스 모듈"을 그룹화하고 비즈니스 구성을 자신의 파일로 옮기는 기능.

+0

이것은 WCF 웹 서비스에만 해당되는가요? 우리는 ASP.NET 웹 응용 프로그램에서 동일한 접근 방식을 사용하고 있습니다 (연결 문자열을 개별 구성으로 분리). 올바르게 작동합니다. – dstepan

+0

투표를하기에 충분한 담당자가 없지만이 솔루션이 마음에들 것입니다. – dstepan

+0

연결 문자열은 config 섹션에서 한 것과 같이 configSource 특성을 통해 별도의 파일 (단일 파일)에 넣을 수 있지만 이러한 여러 가지 방법을 알지 못합니다. AppSetting은 file 특성을 통해 mutiple을 가질 수 있으며, 다중을 단일로 병합합니다. 그러나 connectionStrings 섹션에는이 속성이 없습니다. http://stackoverflow.com/q/11121135/1027808 –

관련 문제