ApplicationSettings for Libraries

I am trying to abstract all my database code into a separate library and then use that library in all of my code. All database connections are made using typed table adapters, which I create by dragging and dropping on datasets in VS2005 using the connection string from appSettings.

The problem I haven't been able to solve is that .Net doesn't propagate the appSettings libraries to another project that uses it.

Long story short, I have a database level library MyProgram.DbLayer that is used by other projects like MyProgram.Client, etc. When I had all datasets in .Client, the connectionString was in MyProgram.Client.exe. config so that I can change it after build. When I moved it to MyProgram.DbLayer, this option is not available to me after the binaries have been generated.

EDIT: This seems to be a more general problem with ApplicationSettings.

I noticed that if I manually add a library-only parameter it reads correctly. The only thing I need now is that the parameter is automatically included in the .config file.

0


source to share


2 answers


AppSettings / ConnectionStrings will always be read from the currently running application pool.

By this I mean:

If I have A.exe

one that has a class DAL.cs

. DAL.cs reads the connection string from config and returns "abc"

as expected.

Then I move the DAL.cs to my own project, and therefore my own assembly. I can still call the connection string from app.config, however I will need to "place" the assembly in the running application and add the connection string to the app configuration . So I create a new app.config and put the connection string in it "xyz"

, it runs as expected.

Now if I changed the link in the project A.exe

to use the new one DAL.dll

, what connection string do you think it will have? "xyz"

? Nope! It will use "abc"

as before because it is still configured in the application config file for A.exe

.

I know this works because I have used generic DAL code for many Windows web applications and .



If it's unclear or doesn't help your problem, please let me know by commenting on this answer.

Update next comment from OP

By "host" I mean an application that calls shared code. It can be Windows or web application, it is mainly the application context.

You will need to create entries in the configuration files for each application using the shared code. If I misunderstood your original question (it may have been a long day!) And you want to centralize the configuration as well, then you would need to:

  • Create some form of centralized repository (be it XML, DB, whatever).
  • Increase your shared code to have defaults so it can connect to the centralized repository.
  • The code can then be customized based on the information in the configuration store.

Hope it helps :)

+1


source


If I understand your problem correctly, it looks like you need

1.) General application context for all data access calls

or

2.) Another way to access configuration settings



1: Wrap your data layer in a service that runs under the same context (IIS, Windows service, etc.)

2: Don't use the configuration management mechanism you provided. Use properties files in a specific location instead.

Remember that configuration options are cascading from different levels. For example ... if you add a parameter to machine.config then every application running on that machine will use that parameter unless it is overridden at a lower level ... It might be a good way to set up a standardized parameter in your files configuration.

+1


source







All Articles