DAAB from Data Tier for ASP.NET

Topics: Data Access Application Block
Jan 12, 2007 at 2:44 PM
When I try to use the DAAB (2.0) directly in the interface layer of my project all is well.

But, when I move this code to a data layer in a separate data project the database connection string is not found. I've traced through the code and found that calling the data layer from the interface layer (bad practice, I know) the DAAB is trying to load the configuration file from the web.config instead of the configuration file corresponding to the project representing the data layer.

Strangely if I create a Console application and call the exact same data layer project everything works as expected. The data layer project uses the data layer configuration file.

Is there anything that I need to do differently when calling the data layer from an ASP.NET application?

I've also found that I can't call the data layer project from a VSTS test project. I see similar behavior where the wrong configuration file is attempting to load.
Jan 12, 2007 at 4:49 PM
This is normal configuration file behavior for any application.

Unless you specify otherwise, the DAAB uses a SystemConfigurationSource to get the DAAB configuration, which looks for a web.config or app.config in the root of the application.

Therefore, for a web application, this will be a web.config in the root directory. For a console application, this will be an app.config in the same directory as the console application's exe.

If you want to specify a different configuration file than the application's default configuration file, you need to use a FileConfigurationSource and specify the name of the configuration file in its constructor.

IConfigurationSource source =
new FileConfigurationSource("My.config");

DatabaseProviderFactory factory =
new DatabaseProviderFactory(source);

Database northwind = factory.Create("Northwind");

See more here:

http://davidhayden.com/blog/dave/archive/2006/01/07/2684.aspx


Regards,

Dave

__________________

David Hayden
Microsoft MVP C#
http://www.davidhayden.com/
Jan 12, 2007 at 6:14 PM
Thanks David.

That works perfectly from the web application now.

The VSTS Unit Test still fails. When I trace into the EL Code it's using the configuration path of the execution test output where the test results are created.

Do you know how I can modify the test to locate configuration files in a different location?