Reading connection string from single configuration file from both windows and web application

I have common class library which is used in both windows and web application. I want to have single method say ReadSeverinfo in the class library which reads the connection string from the common configaration file .

After googling , i could find the following solution but could not understand fully. can some expert please explain stepwise how to go about it

Common.Config for Class Library/Common configuration
<?xml version="1.0" encoding="utf-8" ?>
<appSettings>
    <add key="CommonDB" value="CommonDBValue" />    
</appSettings>

Web.Config for Website
<configuration>
    <appSettings file="D:\Common.config">
        <add key="WebDBConn" value="WebDBConnValue" />
        <add key="INPUT_PATH" value="INPUT_PATH_WEB" />
    </appSettings>
</configuration>

App.config for Windows Application
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <appSettings file="D:\Common.config">
        <add key="WindowDBConn" value="WindowDBConnValue" />
        <add key="INPUT_PATH" value="INPUT_PATH_WINDOW" />
    </appSettings>
</configuration>

Code behind
string configValue = ConfigurationSettings.AppSettings["CommonDB"];
vensaliAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Alexandre SimõesManager / Technology SpecialistCommented:
What you found is the solution to your problem.

What you're doing there is pointing to an external config source that will be merged with whatever you have in the appSetting node.
In your case, you're pointing to that Common.config (the name doesn't really matter, just make sure it points to the correct path). The contents of this file will be merged in the app.config/web.config.
As far as I remember, if 2 appSettings key match, whatever is directly in the app/web.config has "priority" over the common config.

The path can be relative too.

Cheers!
0
Michael FowlerSolutions ConsultantCommented:
0
Alexandre SimõesManager / Technology SpecialistCommented:
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

vensaliAuthor Commented:
To put it another way, I am referring same class library from both windows and web application. currently  I am reading the connection string from a txt file  from class library in windows application.  This can be  changed to app.config file

How to dynamically check whether it is windows  app or web application and select the connection string appropriately at run time.
0
Alexandre SimõesManager / Technology SpecialistCommented:
So at the end you don't want to share a configuration.
You want to have 2 separate connection strings depending on what application is consuming them.

Anyway, the solution is the same, just give them two different names.
As far as I understood, they are different, so you have to have them both in the Common.config file.
0
vensaliAuthor Commented:
Actually I found a workaround by myself. I have declared shared variable in class libaray and passing a string constant from the UI project. if it is WEBUI , one constant and WinUI another constant.  In getconstr method, depending on the constant value I am reading the connection string either from web.config or txtfile/app.config.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
vensaliAuthor Commented:
Actually I found a workaround by myself. I have declared shared variable in class libaray and passing a string constant from the UI project. if it is WEBUI , one constant and WinUI another constant.  In getconstr method, depending on the constant value I am reading the connection string either from web.config or txtfile/app.config.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.