applicationSettings for a web application - c#

I know similar flavors of this question has been asked before, but I am asking something a little different. I know how to use applicationSettings and such, but what I am trying to do is to make it into a config. i.e., I have a web application which as a reference to a class library. This class library has a reference to a web service. In doing that, it created a .settings file. When I am using this class library in the web application, everything works fine locally. However, in staging and production, I would like to have different URLs for the web service - the only way to do that seems to take the applicationSettings section, and put it in the web.config of the web application project. I'm trying to avoid muddying up the web.config, so is there a way to have this applicationSettings section in another files referenced by the web.config?

<appsSettings configSource="appSettings.config" />
This moves the whole app settings section to a separate file.

Related

IIS Application name in NLog.config

I am integrating NLog logging in a WCF service hosted in IIS 7.5. Multiple instances (different versions) of the service are installed under the same site, so I would like the Application name to be part of the logging path.
I am aware of the NLog.Web extension and the ${iis-site-name} but since the site is common to all instances it is the application name below site level I would like to use in the log file path.
Each of the applications are installed from separate baseDirs but I would prefer to have logs in a folder structure away from the baseDir.
Any hints to getting the application name? When searching for answers most threads leads to HostingEnvironment.SiteName but I have not been able to find similar ways to get the application name.
Thanks in advance.
You can use the HostingEnvironment.ApplicationVirtualPath property and then hookup an NLog layout renderer.
This should work for NLog 4.4 and newer:
LayoutRenderer.Register("iis-application-name", (logEvent) => HostingEnvironment.ApplicationVirtualPath);
Then you can use ${iis-application-name} in your NLog config file.
Just remember to register the renderer before doing anything else, that is do it before you start loggin.

How to customize class library config settings across projects & clients

Apologies if this has been asked before; after two days of searching I can only find partial answers that don't fully relate to my situation, and are difficult to follow with my lack of experience.
I have a solution that contains four projects:
Class library (containing database connection strings, email server settings, plus lots of other settings)
Web application (web forms)
Web application (MVC)
Web API
Projects 2,3 & 4 all reference the class library, and use the database connection strings, etc, to function. These projects also contain their own additional settings in web.config, bespoke to that project.
Everything works great so far... However, I now need to publish client-specific versions of my solution, e.g. the solution for ClientABC requires different settings for each project than for ClientXYZ. All other aspects remain the same, it is simply the config settings across the four projects that need to change.
From my research, I hit upon something called SlowCheetah which transforms the config files based on the publish profile. That sounded promising, but then I get this problem, where the class library settings aren't pushed into the other projects. I can see bits of useful info in this question, but don't have the experience to apply it to my problem. I'd rather not duplicate the settings into respective project's config file if possible, as that feels messy.
Can anyone please offer me some help as to what's best here? I don't even know if I'm taking the right approach, but am pretty sure I can't be the first ask this?
but then I get this problem, where the class library settings aren't pushed into the other projects
you have to keep in mind that the configuration file is readed by the SturtUp application, your client. Class Library can't run directly, but inside a WebApp or WinApp or ConsoleApp
So, any settings that you put in your ClassLibrary configuration file must be copied in the configuration file of your WebApp.
Generally, I copy some settings from app.config to web.config but, if you search on internet, you can find a method to automate this operation.
I now need to publish client-specific versions of my solution
You can create many configuration profile and use a web.config transformation:
From ToolBar or Build Menu, select Configurazion Manager...
Create all configuration you need for clients
Now you can see different web.configuration files
Now you can specify different configurazion transformation for your ClientABC, ClientXYZ and publish them with specific configuration
EDIT:
So, you can adopt this solution for your Class Library too, or external config file, and include external file in your web.config: External Config

Can a C# class-library read the web.config of a asp-classic web site?

I have a website which is build in Classic ASP. This website has a web.config file. I never created it, and never knew Classic ASP sites could have a web.config file, but this might be from IIS7 to store settings there.
This website uses a C# class library (DLL) to do some stuff. But here comes the tricky part. Should the DLL be able to read the web.config of this website?
I've copied the configSections from my app.config (from within the solution) to the web.config from the site. And also copied some settings (connectionstrings and applicationsettings). But when I changed the connectionstring to a faulty one, it would just keep running without a problem.
When I placed the configsections on the bottom of my web.config, the website wouldn't load because of a faulty web.config, so I the webserver is doing something with it. But can a C# DLL read this file also eventhough it's not a .NET website?
Thanks in advance.
As Chris said, asp classic does not use it, so it must have been created by IIS7 (or someone putting it there as a joke). That its not used if confirmed by your changing settings having no effect.
When you copied sections, the file becomes an invalid config; it's probably that which is causing it not to run, though what's checking it I don't know.
Another possibility is that the ASP website is using a C# library via COM Interop, and the run-time is checking its validity when it starts.

Calling WCF service from class library: App.Config vs Web.Config?

From an existing WebApplication I use to make calls to WCF services. Proxies for these services was created using Add Service Reference menu. Thus generating >> Web.config in this project.
I have added another class library project to the solution. This project also adds reference to the service. Thus generating >> App.Config file in this project.
I understand, in an N-Tier application, we should have common gateway to the service. Just out of curiosity I would like to know -
For WCF calls originated in WebApp, propagated to class library which config file (App/Web) would be referred
for locating client endpoint configurations ?
The web.config file will be used for locating the service.
The reason is because in this case, the app domain belongs to the web app, not the class library, and the default config file for this app domain is the web.config.
#musefan is correct. It is the web.config that is used.
If you want to, you can split some confuguration sections into seperate files and reference them from the main web.config. You might want to do this so you can maintain the WCF client and server config in a single place to ensure they are consistent.
foe example, if you want to seperate out the <client> section, you would do this:
<client configSource="client.xml" />
Where client.xml is a file containing the relevant client config information.
This blog post tells you how to do it in a bit more detail.
http://blog.andreloker.de/post/2008/06/keep-your-config-clean-with-external-config-files.aspx

DLL settings from another DLL

I have a MVC project that uses foo.dll, but that foo.dll uses smu.dll so basically I never reference smu.dll within my MVC project. The problem I´m faced with is that within my MVC project I want to be able to store ssettings in web.config so that smu.dll will read.
We have tried to use both applicationsettings and appsettings without effect, when I refernce smu.dll directly from MVC or any other program it picks up the settings without a problem. Do settings for smu.dll have to go into foo.dll app.config?
EDIT
Hello again friends, and thank you for your answers.
I found out what I was doing wrong after I read the comments. In MVC there is a web.config within the Views-folder and one web.config in the root of the project. When I used sectionGroup and applicationSettings in the "Views"-web.config the config was not read by the smu.dll. I then changed the smu.dll to read appsettings instead, whereas I then put the appSettings into to "root"-web.config, then the config file was read by the dll. I hope this makes sense but I am new to this and am still learning.
Anyway, this is finally working now :)
So, you have an MVC project, and two class libraries (foo & smu) ?
MVC -> Foo (via project reference)
Foo -> Smu (via project reference)
within AppSettings of web.config, you have a setting that smu should pick up?
Shouldn't Smu just accept a param, but not care where it comes from e.g.
if Smu was a data-access class, and it needed a connection string - all it needs to allow the user to do is send it a connection string? so, you could then send that from Foo (as this class can access your config settings) ?

Categories

Resources