roblems getting a db connection using enterprise lib and app.config file

Topics: Data Access Application Block
Oct 12, 2009 at 3:11 PM

I am trying to use the Enterprise Library data access application block to get a database connection info out of an app.config in a dll (this is for a plugin for CRM).    To do this, the closest I have been able to come is to use the info from this older blog post:

http://davidhayden.com/blog/dave/archive/2006/01/08/2686.aspx

using this sample about instance provider factories:

---from post:

Instance Provider Factories

This method is a bit more involved, but gives you much more flexibility in terms of where the DatabaseProviderFactory gets its configuration information:

 

IConfigurationSource source =
new SystemConfigurationSource();
DatabaseProviderFactory factory
=
new DatabaseProviderFactory(source);
Database northwind
= factory.Create("Northwind");
---end snippet
Although this particular snippet doesn't work because it isn't looking for the app.config 
(perhaps because it is a dll?), so changing it to explicitly specify the app.config, seems to work better:
IConfigurationSource source =
new FileConfigurationSource("app.config");
DatabaseProviderFactory factory
=
new DatabaseProviderFactory(source);
Database myDatabase
= factory.Create("MasterSQLConnection");

insofar as reading from the "app.config" file in my CRM web server directory.  (It no longer complains 

about a lack of configuration.)

Problem is, I get the run-time error:

unable to cast object of type 'System.Configuration.DefaultSetting' to type 'Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings'

when I try to execute the plugin/dll at the factory.Create statement.

 

This is the relevant portion of my app.config in my CRM web server directory:

 

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="CrmServiceSoap" closeTimeout="00:01:00" openTimeout="00:01:00"
                    receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false"
                    bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                    maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                    messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
                    useDefaultWebProxy="true">
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    <security mode="None">
                        <transport clientCredentialType="None" proxyCredentialType="None"
                            realm="" />
                        <message clientCredentialType="UserName" algorithmSuite="Default" />
                    </security>
                </binding>
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://corp-devteam:5555/MSCrmServices/2007/CrmService.asmx"
                binding="basicHttpBinding" bindingConfiguration="CrmServiceSoap"
                contract="CrmService.CrmServiceSoap" name="CrmServiceSoap" />
        </client>
    </system.serviceModel>
<configSections>
<!--<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>  -->
<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data" />
</configSections>
<connectionStrings>
<!---PARTITION-->
<!--<add name="MasterSQLConnection" connectionString="Data Source=172.29.1.43; Initial Catalog=ClientController; Persist Security Info=True;Connect Timeout=180; UID=sa; PWD=b1gd0geat:)" providerName="System.Data.SqlClient"/>-->
<!--<add name="MasterSQLConnection" connectionString="Data Source=172.29.1.43; Initial Catalog=ClientControllerDev1; Persist Security Info=True;Connect Timeout=180; UID=sa; PWD=b1gd0geat:)" providerName="System.Data.SqlClient"/>-->
<add name="MasterSQLConnection" connectionString="Data Source=172.28.1.16; Initial Catalog=ClientControllerDev2; Persist Security Info=True;Connect Timeout=180; UID=sa; PWD=b1gd0geat:)" providerName="System.Data.SqlClient"/>
<!-- <add name="MasterSQLConnection" connectionString="Data Source=192.168.60.212; Initial Catalog=AB_ClientController; Persist Security Info=True;Connect Timeout=180; UID=sa; PWD=b1gd0geat:)" providerName="System.Data.SqlClient"/> -->
<!-- <add name="MasterSQLConnection" connectionString="Data Source=172.28.1.16; Initial Catalog=ClientControllerTest2; Persist Security Info=True;Connect Timeout=180; UID=sa; PWD=b1gd0geat:)" providerName="System.Data.SqlClient"/>-->
<!--<add name="MasterSQLConnection" connectionString="Data Source=172.29.3.20; Initial Catalog=ClientControllerTMFSDev2; Persist Security Info=True;Connect Timeout=180; UID=sa; PWD=Grn7K32" providerName="System.Data.SqlClient"/>-->
<add name="172.28.1.16Dev1TMAdvisor1" connectionString="Data Source=172.28.1.16; Initial Catalog=Dev1TMAdvisor1; Persist Security Info=True;Connect Timeout=180; UID=sa; PWD=b1gd0geat:)" providerName="System.Data.SqlClient"/>
<add name="172.28.1.16Dev1TMAdvisor2" connectionString="Data Source=172.28.1.16; Initial Catalog=Dev1TMAdvisor2; Persist Security Info=True;Connect Timeout=180; UID=sa; PWD=b1gd0geat:)" providerName="System.Data.SqlClient"/>
<add name="172.28.1.16Dev2" connectionString="Data Source=172.28.1.16; Initial Catalog=Dev2; Persist Security Info=True;Connect Timeout=180; UID=sa; PWD=b1gd0geat:)" providerName="System.Data.SqlClient"/>
<add name="172.29.1.44Test1TMAdvisor2" connectionString="Data Source=172.29.1.44; Initial Catalog=Test1TMAdvisor2; Persist Security Info=True;Connect Timeout=180; UID=sa; PWD=b1gd0geat:)" providerName="System.Data.SqlClient"/>
<add name="172.29.1.43Test1TMAdvisor1" connectionString="Data Source=172.29.1.43; Initial Catalog=Test1TMAdvisor1; Persist Security Info=True;Connect Timeout=180; UID=sa; PWD=b1gd0geat:)" providerName="System.Data.SqlClient"/>
<add name="172.28.1.16Test2TMAdvisor1" connectionString="Data Source=172.28.1.16; Initial Catalog=Test2TMAdvisor1; Persist Security Info=True;Connect Timeout=180; UID=sa; PWD=b1gd0geat:)" providerName="System.Data.SqlClient"/>
<add name="172.28.1.16Test2TMAdvisor2" connectionString="Data Source=172.28.1.16; Initial Catalog=Test2TMAdvisor2; Persist Security Info=True;Connect Timeout=180; UID=sa; PWD=b1gd0geat:)" providerName="System.Data.SqlClient"/>
<add name="172.29.1.43TMAdvisor1" connectionString="Data Source=172.29.1.43; Initial Catalog=TMAdvisor1; Persist Security Info=True;Connect Timeout=180; UID=sa; PWD=b1gd0geat:)" providerName="System.Data.SqlClient"/>
<add name="172.29.1.44TMAdvisor2" connectionString="Data Source=172.29.1.44; Initial Catalog=TMAdvisor2; Persist Security Info=True;Connect Timeout=180; UID=sa; PWD=b1gd0geat:)" providerName="System.Data.SqlClient"/>
<add name="172.29.3.20TMFSDev2" connectionString="Data Source=172.29.3.20; Initial Catalog=TMFSDev2; Persist Security Info=True;Connect Timeout=180; UID=sa; PWD=Grn7K32" providerName="System.Data.SqlClient"/>
<add name="192.168.60.212AB_TMAdvisor2" connectionString="Data Source=192.168.60.212; Initial Catalog=AB_TMAdvisor2; Persist Security Info=True;Connect Timeout=180; UID=sa; PWD=b1gd0geat:)" providerName="System.Data.SqlClient"/>
<add name="192.168.60.212AB_TMFS" connectionString="Data Source=192.168.60.212; Initial Catalog=AB_TMFS; Persist Security Info=True;Connect Timeout=180; UID=sa; PWD=b1gd0geat:)" providerName="System.Data.SqlClient"/>
<!--/PARTITION-->
<add name="SupportSQLConnection" connectionString="Data Source=DEVSQL1;  Initial Catalog=HELPDESK3; Persist Security Info=True;Connect Timeout=1000;UserID=sa;Password=Tam2000" providerName="System.Data.SqlClient"/>
</connectionStrings>
<dataConfiguration defaultDatabase="MasterSQLConnection">
<providerMappings>
<add databaseType="Microsoft.Practices.EnterpriseLibrary.Data.Sql.SqlDatabase, Microsoft.Practices.EnterpriseLibrary.Data, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" name="System.Data.SqlClient"/>
</providerMappings>
</dataConfiguration>
</configuration>

 

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

<configSections>

<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data" />

</configSections>

<connectionStrings>

<!---PARTITION-->

<add name="MasterSQLConnection" connectionString="Data Source=000.00.00.0; Initial Catalog=DevDBName; Persist Security Info=True;Connect Timeout=180; UID=sa; PWD=password" providerName="System.Data.SqlClient"/>

</connectionStrings>

<dataConfiguration defaultDatabase="MasterSQLConnection">

<providerMappings>

<add databaseType="Microsoft.Practices.EnterpriseLibrary.Data.Sql.SqlDatabase, Microsoft.Practices.EnterpriseLibrary.Data, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" name="System.Data.SqlClient"/>

</providerMappings>

</dataConfiguration>

</configuration>

 

 

Any ideas?

 

thanks in advance,

-stacy

Oct 13, 2009 at 3:06 AM

Are you sure that your instance of FileConfigurationSource is pointing to the correct config file?  (And yes, the first one didn't worked because it is a class library)  Because the error seems like in your config, you have something like:

<section name="dataConfiguration" type="System.Configuration.DefaultSetting" />

 

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com