Using ConfigurationManager.GetSection in a Class Library's App.config - c#

I am writing a class library which has settings in its app.config and which will ultimately be called by a small number of other .NET applications. In order to get settings from it I'm using ConfigurationManager.GetSection such as this:
MyConfiguration process = (MyConfiguration)ConfigurationManager.GetSection("MyGroup/processes");
I've discovered though that the calling application has to have the same app.config inside it's own project in order for this to work, otherwise the class library will throw a NullReferenceException. I'm just wondering if this is normal behavior or if there's any way to ensure that only the class library needs to have app.config available?
Thanks :)

Your class library will always attempt to read from the app.config of the main application that references it. It will not use your class library config file at all.

Related

Global.cs file replacement in .Net Standard Class library

I am new to .net standard/.net core/blazer. In .net framework class library we used to have Global.cs file to store global variables, needs to do the same in .net standard class library, whats the Microsoft recommended way?
OR to summarise above:
where to store Global variables in .Net standard class library project?
In Class library projects you put it in Web.config (or App.config) and it will be available from the class library as well. You basically add manually such file (app.config/webconfig).
If you're going to use a class library, I would recommend having any variables etc passed in via configuration parameters/configuration objects, and if you need defaults - have defaults created in your constructors.
I would personally shy away from having a class lib access web.config/appSettings.json, as you're then tieing your application to a specific implementation.

Connection String in Class Library Web Config file in C#

I have assigned a number of connection strings in the web config file for my class library.
I decided to change one of the databases in the tag, but when the library is built and add to the reference of my project it still refers back to the old table.
I have tried cleaning both the project and the class library and rebuilding the class library.
Also, I have removed the reference and re-added it to the project, but it still insists on calling the old table in the tag.
Does anyone know what is causing this or know of a fix?
The web.config file that is used is the one pertaining to the ASP.NET (Core) application and not the class library. Edit the one in your website's web.config and the changes will be reflected.

Can a class library read the app.config file of a Coded UI Test that is being executed?

I've developed a little utility library that deals with a lot of the configuration and logging needed to by all of my Coded UI test projects. I have the class library set up to read the app.config file using the following to read the values:
ConfigurationManager.AppSettings["KeyName"]
I understand that when you include this class library in another project, the app.config of the new project should be used during execution. Actually, that's precisely what I want, it just doesn't appear to be happening.
My other project is a CUIT but it seems as though my class library it includes isn't reading any sort of app.config. Not its own, not the CUIT's.
Is this even possible?
Looks like I was missing the following line in my class library. After adding it things worked as I expected.
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

How I can use an app.config file with a comvisible class library?

I'm working on this ComVisible class library that uses a third party component.This component needs some settings to be added to the config file.Since the application that uses my dll is a VB 6 application I don't know where should I put the config file? is there anyway that we can load a config file at runtime?
You can put the app.config in the same directory as the VB6 app (named (yourapp).exe.config). If you want to load it at runtime, you'll have to write an unmanaged shim that starts the CLR and loads your managed code in a new appdomain- then you can tell it to load whatever config file you want for the new appdomain. This is a pain though (having done it a few times)... There are some options for managed shims as well (see the AppDomainManager class for details).
If you just need "a config file" and not specifically the app.config, this post shows a simple class model that I use to store configuration values:
(.Net) suggestions on making a config file for a program?

Including a service reference from a class library

I have a C# class library and a startup project (a console app). The class library includes a service reference to a web service. When I try to run the project, I get an InvalidOperationException because the startup project isn't reading the class library's app.config, and it's ignoring the service reference. To get it working, I'm forced to add the same service reference to the startup project. Is there any way I can avoid this? Can I make the startup project recognize the class library's service reference and app.config without having to copy it to the startup project?
I've tried adding a link to the app.config from the class library, but that doesn't work. The class library isn't very portable if it requires anyone who uses it to add that service reference to the startup project.
Think about what you are trying to do - you have two assemblies that you are building:
Library
ConsoleApp
Both of these assemblies have configuration files - I would imagine they look something like this:
Library
app.config
ConsoleApp
ConsoleApp.exe.config
When you run ConsoleApp it has no way of reading from or knowing aboout app.config from your Library assembly. The only configuration file that it knows or cares about is ConsoleApp.exe.config. Now it is possible to have configuration files reference each other but this is not the proper solution for what you are trying to do.
Since your Library assembly has no entry point, it will never be loaded into an AppDomain. Since it will never be loaded into an AppDomain its application configuration file will never be used.
What you ought to do is reference Library in ConsoleApp via a project reference. Then move all the relevant configuration data from app.config into ConsoleApp.exe.config as this is the configuration file that will be used by your application.
This will allow you to have to two things you need to invoke methods on your web service
The code in Library that can send and receive SOAP messages.
The configuration metadata that is required by Library to function.
An alternative to using a service reference in the class library and then copying the config would be to use build events that call svcutil.exe. The thing I like about this is that you don't have to make "update service reference" when the service changes. It will be updated automatically.
In the class library, use a build event that only generates the proxy code:
svcutil.exe net.tcp://localhost:3315/MyService/mex /noConfig
In the application, use a build event that generates the config. You can use the /mergeConfig option to merge it into an existing app.config.
svcutil.exe net.tcp://localhost:3315/MyService/mex
/config:App.config /mergeConfig
If you don't want to get a build error if the service is not running, put this in your project file and you will get a warning instead of an error:
<Target
Name="PreBuildEvent"
Condition="'$(PreBuildEvent)'!=''"
DependsOnTargets="$(PreBuildEventDependsOn)">
<Exec WorkingDirectory="$(OutDir)"
Command="$(PreBuildEvent)"
ContinueOnError="true" />
</Target>
You just need to copy the config key, pointing to the service, from your class library config file to your console app's config file.
You can copy the relevant portions of the app.config from the class library's configuration into the app.config for the console application.
Alternatively, if you're really trying to make this truly portable, you'll need to think about another way of referencing the address for the specific service reference from within the class library.
I'd think it more confusing if you had multiple configuration files running around.
If a library has configurable items, I would fully expect to have to put that configuration in my config file to properly consume the library.

Categories

Resources