what is the replica for configuration context in entlib5.0

Topics: Data Access Application Block, Enterprise Library Core
Dec 20, 2011 at 11:55 AM

hi all,

 

I have recently upgraded my application to entlib 5.0 version from old version entlib v1.1. I did all the conversions successfully but strucked at this line. so can any one give me the soluvtion how to resolve this. I am getting this error while running the code:

" The type Database cannot be constructed. You must configure the container to supply this value"

 protected DatabaseProviderFactory ServerDatabaseFactory; ( this is the dbprovider factory object which i do created)

 

ServerDatabaseFactory = new PAG.Data.DatabaseProviderFactory(new ConfigurationContext(pagDBConfigFile));

 (need replica for this line in new version.)

Please help me.

 Thanks a lot in advance.

 

Thanks and regards

 

 Kiran

 

 

 

 

 

 


Dec 21, 2011 at 12:08 AM

ConfigurationContext was converted to Configuration Source for Enterprise Library 2.0.  See Enterprise Library 2 Architecture: Part I for a short discussion.

You can use FileConfigurationSource instead of the ConfigurationContext:

ServerDatabaseFactory = new DatabaseProviderFactory(
    new Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource(pagDBConfigFile));

Database db = ServerDatabaseFactory.CreateDefault(); 

The "newer" way to do what you are doing above is:

var configSource = new FileConfigurationSource(pagDBConfigFile);

var builder = new ConfigurationSourceBuilder();
builder.UpdateConfigurationWithReplace(configSource);

EnterpriseLibraryContainer.Current
  = EnterpriseLibraryContainer.CreateDefaultContainer(configSource);

Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com 

Dec 21, 2011 at 6:28 AM

hi randy,

 

Thanks for your reply.

I have implemented the same as you given above.

but I am getting a different error now :

Resolution of the dependency failed, type = "Microsoft.Practices.EnterpriseLibrary.Data.Database", name = "dbserver".
Exception occurred while: while resolving.
Exception is: InvalidOperationException - The type Database cannot be constructed. You must configure the container to supply this value.
-----------------------------------------------
At the time of the exception, the container was:

Resolving Microsoft.Practices.EnterpriseLibrary.Data.Database,dbserver

""

 

please find the code scenario which we have written below: 

protected DatabaseProviderFactory

ServerDatabaseFactory;

( this is the database factory object)

here i have initiated this serverdatabasefactory object



 

ServerDatabaseFactory = new PAG.Data.DatabaseProviderFactory(new Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource

(pagDBConfigFile));

 

// This refers to a string in the pag.config file which identifies a section that describes the server connection.

 

 // It is used along with the database factory above to create a correct connection to the server..

 

 

 

 

DatabaseServer = serverConfigSection;

we are calling this in the following method.

 

public SqlDatabase

GetADatabase()

{

 

try

{

 

//kiran-19/12/2011

 

//return ((PAG.Data.Sql.SqlDatabase)ServerDatabaseFactory.CreateDatabase(DatabaseServer));

 

 

return (PAG.Data.Sql.SqlDatabase

)ServerDatabaseFactory.Create(DatabaseServer);

//Error is coming on the above line.

}

 

catch (System.Exception

ex)

{

 

throw (new DASException("System Exception"

, ex));

}

}

 

we are calling this method here

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

public Procedure(DASDalDatabase db, string

spName)

{

 

 

// No exception possible. Already calculated. If this is zero then failed to fetch.

userSecurityId = db.UserSecurityId;

usingIdentity = db.UsingIdentity;

 

 

try

{

 

_sqlDB = db.GetADatabase();

 

 

 

// calls the database factory

 

 

// _sqlCW = (PAG.Data.Sql.SqlCommandWrapper)_sqlDB.GetStoredProcCommandWrapper(spName);

_sqlDC.CommandText = spName;

_sqlDC.CommandType =

 

CommandType

.StoredProcedure;

 

_sqlPC =

 

null

;

GetParametersFromDatabase();

 

 

//kiran-17/12/2011

 

 

// IDataParameterCollection pc = _sqlCW.Command.Parameters;

 

 

IDataParameterCollection

pc = _sqlDC.Parameters;

 

 

foreach (System.Data.Common.DbParameter param in

pc)

{

 

 

this[param.ParameterName] = (Object)System.DBNull

.Value;

}

}

 

 

catch (System.Exception

ex)

{

 

 

throw (new DASException("System Exception"

, ex));

 

 

}

}

 

So can you pelase give me the solution//

 

Its really screwing me up/

 

 Thanks in advance

 Thanks

 

 Kiran

Dec 21, 2011 at 6:41 AM
Edited Dec 21, 2011 at 2:04 PM

Do you have a connection string defined for "dbserver" in a config file?

I can reproduce your error with the following code:

string pagDBConfigFile = @"myFile.config";

DatabaseProviderFactory ServerDatabaseFactory = new DatabaseProviderFactory(
    new Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource(pagDBConfigFile));

Database db = ServerDatabaseFactory.Create("dbserver");

And there is no connection string "dbserver" defined.

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com 

Dec 21, 2011 at 7:06 AM
Edited Dec 21, 2011 at 7:09 AM

hi randy,

pagDBConfigFile represents the physiical  file path .

 

Please find the config file info.. below

 

 

 

xml version="1.0" encoding="utf-8"

 

dataConfiguration

 

xmlSerializerSection type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data"

 

enterpriseLibrary.databaseSettings xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" defaultInstance="DataAccessQuickStart" xmlns="http://www.microsoft.com/practices/enterpriselibrary/08-31-2004/data"

 

databaseTypes

 

databaseType name="Sql Server" type="Microsoft.Practices.EnterpriseLibrary.Data.Sql.SqlDatabase, Microsoft.Practices.EnterpriseLibrary.Data"

 

databaseTypes

 

instances

 

instance name="dbserver" type="Sql Server" connectionString="DASDbConnectionString"

 

instances

 

connectionStrings

 

connectionString name="DASDbConnectionString"

 

parameters

 

parameter name="database" value="DASDB" isSensitive="false"

 

parameter name="Integrated Security" value="true" isSensitive="false"

 

parameter name="server" value="svkiran" isSensitive="false"

 

parameters

 

connectionString

 

connectionStrings

 

enterpriseLibrary.databaseSettings

 

xmlSerializerSection

 

dataConfiguration

 

 

>

 

 

>

 

</

 

 

>

 

</

 

 

>

 

</

 

 

>

 

</

 

 

>

 

</

 

 

/>

 

</

 

 

/>

 

<

 

 

/>

 

<

 

 

>

 

<

 

 

>

 

<

 

 

>

 

<

 

 

>

 

<

 

 

/>

 

</

 

 

>

 

<

 

 

>

 

<

 

 

/>

 

</

 

 

>

 

<

 

 

>

 

<

 

 

>

 

<

 

 

>

 

<

 

 

?>

 

<

<?

Dec 21, 2011 at 2:16 PM
Edited Dec 21, 2011 at 2:17 PM

Have you migrated your configuration?  It looks like the configuration is still the v1.1 configuration.  You will need to use the v5 configuration.  It would look something like:

<configuration>
    <configSections>
        <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
    </configSections>
    <dataConfiguration defaultDatabase="dbserver" />
    <connectionStrings>
        <add name="dbserver" connectionString="Database=DASDB;Server=svkiran\SQLEXPRESS;Integrated Security=SSPI"
            providerName="System.Data.SqlClient" />
    </connectionStrings>
</configuration>

Also, a tip: when pasting code use the "insert code snippet" feature so that it is formatted and readable.

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com