Moving configuration outside of IIS root - asp.net-core

In regular ASP.NET/MVC development we could move configuration entries from web.config in the application/site directory to machine.config.
With ASP.NET Core is there something similar, now that we have json based configuration files?

Well, technically, you can pull in any JSON from any location. Just pass the full filesystem path, instead of just "appsettings.json".
However, really, if you're talking about externalizing your configuration, you should probably use environment variables or some service like Azure Key Vault.

Related

Asp.net Hosted Blazor - Can i use app.config or web.config?

Im looking to add a web.config (or app.config) so that i can have different settings (e.g. DEV build vs RELEASE build, for instance).
Can i simply add a .config file and expect to read from it?
I noticed that there is also an option to add a appsettings.json (App Settings File).
Which one of these should i be using for Blazor Client app (hosted via Asp.net)?
Thanks
Updates
Based on the following thread, i might just go with a settings
file on the Server side and feeding it to the Client via API.
Here is another reference for appsettings.json vs web.config
I was able to find a more concrete example int this article.
I believe you can use appsettings.Development.json and appsettings.json
Hope this helps...

in the .net core,it has Web.config Transformation Syntax for Web Application Project Deployment?

Like this(this is asp.net web The program):
While web.config transforms are still an option, Asp.Net Core seems to be moving away from them for application-level configuration and instead using things like environment variables and config.json files.
For the JSON configuration file option, you are able to create multiple JSON files for each environment, and use an environment variable at runtime to determine which file is loaded. More information can be found here: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration

Configure Azure Cache (<dataCacheClients>...) of WebRole in cscfg file?

Currently, I have my Azure Cache configuration
<dataCacheClients>
<dataCacheClient name="default">
...
inside my Web.config of my Azure WebRole. I would like to be able to easily modify the Azure Cache settings after the cspkg package has been created, i.e. I would like to move the configuration into the cscfg file.
I don't think that's possible without hand-coding all the possible configuration options of Azure Caches (and then interpreting those options and setting the configuration programmatically in the DataCacheFactory). Am I overlooking something?
in webrole (RoleEntryPoint derived class) onstart, you could overwrite web.config caching part with the relevant settings from your .cscfg file
also add eventhandler to the environment changing (RoleEnvironment.Changing += RoleEnvironmentChanging;) with e.Cancel = true so that the webrole restarts when ever you change .cscfg # the management portal
One walkarround is to rename your .cspkg file to .zip file. Then unzip the file and take a look to the files. You will find .cssx files (the larger files) representing each webrole / working role. rename to .zip and expand. Then you will find a "approot" folder. Your dlls, webconfig, etc are there. You can mess with the webconfig and zip+rename the way back.
It's not elegant at all, but sometimes is the best way to figure out what exactly are you publishing.

wcf client configuration

I have wcf client. It uses .NET 3.5.
When I compile the client I get two files:
client.exe and
client.exe.config.
The second file contains configuration for the wcf client.
In my case I need to prevent the user sitting on the computer to see the urls and change some other parameters from the config file.
So the requirements are, the end user not to see and modify the data stored in the config. The config file contains the same data as app.config. I need to forbid the person using the program to see the end point urls so easy.
Also I have a lot of configuration there so I do not like to code in the moment.
Is there any solution for the problem (embedded app.config of something else)?
Edit: I do not need configurable options. The config file is automatically created when adding service reference from the studio.
Regards
You can also create your proxies programatically instead of using the service reference feature.
Every parameter in the serviceModel config section can be represented in code as well.
The ChannelFactory class will help you create proxies to the service.
You can easily encrypt entire parts of your config files - and not just web.config in web scenarios, but also application config's in stand-alone apps.
Check out some resources on how to do this:
Encrypting web.config values
Encrypting passwords in .NET app.config file
Encrypting the app.config file for Winforms application

Relative paths in WCF service hosted in IIS

I'm throwing together a quick data service in WCF to be accessed by a public Silverlight 2.0 application. As my data is very static and relatively simple I'd like to just store it in local XML files (which is made easier as there are a VERY limited number of people who will ever edit it).
I'm wondering what the best way to find a relative path from within my service will be. In traditional ASP.NET I could use the Server.MapPath....within this WCF service nothing similar is available. This solution will ultimately be hosted at a hosting provider I have no control over so I can't hardcode any fixed locations. I'd much rather just get a relative path to some XML files in my AppData folder.
Any suggestions?
You could try using Environment.CurrentDirectory or AppDomain.CurrentDomain.BaseDirectory
Try using HostingEnvironment.ApplicationPhysicalPath.
The WCF services still have access to a lot of the same things as your ASP.NET pages (since, in the end there is still an HTTP request and response). You can still use Server.MapPath like so:
HttpContext.Current.Server.MapPath(...)
You could store the files in IsolatedStorage instead of in your folder for the application. Look at the example on the linked page to see how it works.
First, add an operation to the service to return the current directory. Have the new operation just return Environment.CurrentDirectory. In the client, check to see if you are surprised by what the current directory was. Adjust as needed.