from where to read keys in Class library/webapi - c#

i have a webapi project that uses cache library.I am using cache duration key that is used inside cache library.My question is what is the standard practice where should i put Cache duration key,from Where to pick the value of cache duration, inside appconfig of cache library or from web.config of webapi?

The web.config is the place to go in a web application. There's no such thing as app.config in an ASP.NET application. You could always build some mechanism to load configuration data from custom places but the first place a developer would look for such things is the web.config.

Related

How to add a custom JSON file into IConfiguration based on a setting?

I am working on ASP.NET Core 6.0 WebAPI and I need to get data from a CRM system (Sales Logix) using Sage SData API. We have different CRM environments (Production, Staging, Development) and I want to be able to connect (or test) any environment from my WebAPI project.
For that to work, I would like to add a configuration key (to indicate a particular CRM environment) either in appsettings.json (or launchsetting.json). For example when setting is "crmEnvironment": "Development", I want to include a custom json file, named crm-dev.json. Similarly for "crmEnvironment": "Staging", I want to include crm-staging.json.
Each custom json file ideally contains the CRM Url, Username and Password.
Please tell me how can I conditionally add json config files as mentioned above, or is there any better approach of achieving similar results, considering security in mind. Best if I could have custom config files encrypted without having to shift away from the standards, just like we did for Web.Config files by inheriting ProtectedConfigurationProvider.
My questions is similar to How can I add a custom JSON file into IConfiguration?. But since Program.cs no longer uses the Main() method, I am wondering what would be the correct way to add custom json config files.
If this may work, I use this approach.
.AddJsonFile($"Config\\appsettings.{hostContext.HostingEnvironment.EnvironmentName}.json", optional: true, reloadOnChange: true)
And I will have the Json file created for each environment. You can set the environment variable value either via IHost context or may be, simplifying your need by using Environment variable values from your respective environment.

Application_Start vs serviceAutoStartProviders

I am using IIS7.5 to force my web app to load automatically (startMode="AlwaysRunning"), and I now want to preload my cache data. I am a bit confused though because two approaches seem identical:
use Application_Start in global.asax
use serviceAutoStartProviders in IIS config files
They seem rather redundant and doing the same thing. If they are, I guess I would rather use Application_Start than create code dependencies in IIS configuration files. Any advice?
The Application_Start in the global.asax is fired when the application receives it's first request (first user or autostart) so it is not used to start the site.
Use serviceAutoStartProviders to start
http://www.asp.net/whitepapers/aspnet4#0.2__Toc253429241
The IIS Application Warm-Up Module is easier to use
http://www.iis.net/learn/get-started/whats-new-in-iis-8/iis-80-application-initialization

Load DataCache from Role Entry Point On Start

I want to access my windows Azure Data Cache from my Role Entry StartUp routine. However I keep getting this error:
{"ErrorCode:SubStatus:Server collection cannot be empty."}
However when I do the same from within my Controller class it loads the Data Cache fine and I can go ahead and do things with it.
Is there anything special for the Role Entry class that I have to do to access the Data Cache prior to my application starting?
Or can't I access the Cache in the Role StartUp ?
Cheers
Starting with Azure SDK 1.3, there is a major change - the Full IIS mode. Read this blog post to get full undertanding of full IIS and what is it.
In short - your RoleEntryPoint descendant (where your OnStart method is being executed) lives in whole another AppDomain (and process actually - WaIISHost.exe), while your actual web application just lives in IIS (w3wp.exe). That's why there is no way to do something in OnStart() that would affect your web applicatin or that would be able to directly read your web.config.
If you do read Azure Data Cache in OnStart to do some preload of data for the web application, just do in your Global.asax's Application_Start() event handler.
If you need to read Azure Data Cache in OnStart for reason's specific to the RoleEntryPoint, you have to load the configuration from web.config. Web.config is placed in "./bin/web.config" relative to your AppRoot folder. (there are two copies of your application when you use WebRoles with full IIS - one lives in AppRoot and one lives in SitesRoot).
Hope this helps!
WebRole's OnStart probably does not use your web.config where you probably have specified server names and access keys for your AppFabric DataCache provider.
I would try manually instrumenting the server connection configuration.

configuration settings in asp.net

We have a static html/webform site, the site lacks search functionality, I was able to get yahoo BOSS (Build your Own Search Service) after a few hours yesterday, i got it working (still working on adding missing features like pagination) , I was wondering about the configuration options of the class, as I have a BossSearch.cs in App_Code, with some fields that are set at the top:
public class BossSearch
{
String sResultsPage = "~/searchResults.aspx";
String sSearchString="";
String sApiKey = ConfigurationSettings.AppSettings["BossApiKey"];
String sSite = "www.oursite.com"; //without http://
String sQuery = "http://boss.yahooapis.com/ysearch/web/v1/{0}%20+site:{1}?appid={2}&format=xml&start={3}&count={4}";
String sStart = "0";
Uri address;
WebProxy webproxy = new WebProxy("http://192.168.4.8:8080");
bool bUseProxy = true;
int nResultsPerPage = 10;
int nTotalResults = 0;
...
As you can see, i get the BossApiKey from the web.config file, but all others I have them in the declared in the class, should I put all of them in the web.config file? if I'm thinking of reusing the class (should i say class library?) in other websites as well? can I turn it into a dll and what would the advantages be? i read somewhere that a dll has its own config file, is this the way to store those settings?
Apologies for my ignorance, since I'm not that familiar with developing applications (still studying)
What you read about .NET assemblies having their own config files is not absolutely correct; a web site has web.config files, one in the root and zero/one in each subdirectory. If a .NET assembly that is in the application calls into the standard config API, it will get its data from that web.config.
The same goes for WinForms apps and the [appname].exe.config file; any assemblies included that use the standard config API will be getting their data from that.
All of that is not to say that any assembly could not define its own configuration mechanism which pulls its data from wherever it wants.
And yes; if you intend to reuse this code a good bit, you are thinking along the right lines; put the code in its own assembly, and have it get its data from Config files so you do not need to recompile it for each application.
If you declare all of them in a database or web.config, you don't need to recompile each time you reconfigure the search engine
I you're striving for reuse and ease of use, then I would recommend writing a custom configuration section for your control. This can be part of the dll you distribute to other application and will allow you to have the ultimate in flexibility and explicit portability to other .net apps.
Enjoy!
You should only store the value in a single place in your application. For an ASP.NET application, the web.config file is an appropriate place for these kind of things. You won't need to recompile your application if this value changes.
If you decide to put your code into a separate class library and still want to use a config file to store your api key, you should note that your appSetting key needs to be entered in the application or web site's config file - you can't define a config file for a class library.
One other approach that you might find useful would be to make a wrapper class to store your settings. You could have class with static methods to look up your appSettings key so that you get a nice, compile time, way to get the api key, rather than typing out the name of your appSettings key everywhere.

Is it possible to programmatically control c# health monitoring without using the web.config file?

I have developed my own custom provider for the health monitoring; however, I use parameters in the constructor and this is not allowed when using the health monitoring from the web.config file.
Does anyone know if I can turn on/off the monitoring and have it watch properly through code (possibly in my global.asax file on application startup).
Or, is it possible for me to create my own watcher that will do the same thing as the health monitor.
Or, finally - can I just pass variables from the web.config setup (i'm not familiar with the public token part of the provider type declaration).
Thanks in advance
I don't know if there are any other better ideas out there... after a lot of reading and trying I ended up using parameters in the provider to pass information into a custom bufferedwebeventprovider.
If you create a custom provider and include the override Initialize(name, config) method then all of your parameters from your web.config file will come through the config param of the Initialize command. Then in the initialize command you can pull them off one by one (and remove them) before passing the rest of the config property to the base.Initialize method.
I used this to save and pull off connection string info, timeouts, custom id's, etc.
I would still like to know anyway to control the health monitoring without having all of the info in the web.config (mostly because this is a database driven website with multiple users and multiple different settings). I'll probably end up having a procedure within the custom provider to check settings and only record entries as needed based on each user's settings.
any other thoughts are very welcome!!

Categories

Resources