Retrieving configuration settings from the host

Mar 4, 2008 at 8:47 AM
Hi all

I've set up a WCF service using the factory and now have a nice solution containing the generated projects.
My service uses DALs to access the database (nothing new...).
In order to initialize these DALs in need a connection string which I want to be configured through the web.config file of my host project.

Do I have to add the Configuration assembly to the service assembly references and use System.Configuration.ConfigurationManager.ConnectionStrings directly inside the service implementation that initializes the DALs?
Or (preferred), is there a clean way of sending parameters from the host to the underlying service (as string or anything else but not directly connected to the System.Configuration model)?

Thanks in advance

Mar 4, 2008 at 12:30 PM
Hi Vincent,

If you use EntLib DAAB to get access to your DAL (or inside your DAL), you only need to specify the connection string name when you create the DataBase object instance and EntLib will take care of getting the connection string from your current config file, in this case, the one located in your host that tipically manage the current AppDomain.
Now if you don't use EntLib DAAB, then you might use your first approach with Configuration API or some other way like Dependency Injection that will provide the appropiate decoupling from your config file.
If you ask me, I would go with DAAB and leave the config handling to EntLib.

Mar 4, 2008 at 1:58 PM
Thanks Charly

I just find it weird to have the DAL inspect the configuration to get its settings but if it's the regular way to do... fine by me.

If I want to create a test UI with the same service instanciated with different connection strings (ie the connection string does not come from the config files but rather the UI), how could I implement passing the connection string from the UI to the service?

Thanks in advance

Mar 5, 2008 at 6:27 PM
That depends who you get access to your connection string in the DAL. If you use DAAB or some other way that expect to find the cnnstr in a config file, you can only pass that using a different config file for each connection string. On the other hand, I wonder why you want to pass a cnnStr from a UI since it might no be a secure and recommended way to drive the DAL respository access from the UI. That's why it makes sense to put that information at the same level of the DAL (A host config might fit well in this picture).
Now if what you want is to test your service, then you have multiple ways to avoid such cnnStr handling. You may create a unit test that invoke the service and mock the DAL or if that's not possible, you can also have different config files and use the one you need for each test.