Sharing appsettings.json configuration files between different solutions - asp.net-core

I have two Visual Studio solutions SolutionA and SolutionB, each of them containing several web application projects in ASP.NET Core 1.1. The configuration of the projects is handled by the commonly used appsettings.json.
I'd like to outsource some of the config keys that are used in common from the projects of the both solutions, so that the common values don't have to be stored redundantly in both solutions. Is there any way to reach this goal? While I found some way to do it for projects that belong to the same solution (see Andrew Lock's blog post), I didn't find any way to do it for projects that are stored in different solutions.

If you know where your AppSettings.json file is located, you can use the following code:
var settingPath = Path.GetFullPath(Path.Combine(#"../../appsettings.json")); // get absolute path
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile(settingPath, optional:false, reloadOnChange:true);
In your project there would be an automaticall reload if the file changes. Hope you're searching for this solution

Related

how to use different profile for dev and production in office vsto add-in development

When we dev/test our add-in, we need to request request http://dev.xx.com. and after release, in production we need to change the url to http://prod.xx.com.
How can we do this?
===
I'm new to vsto development and c#. I looked config file(in fact Settings.settings) way. But in fact i don't want the users to "config" it. It't better if I can build out two different versions in one click ervery time, and give one to testers and another to the end user.
When I devloping server-side springboot applications, I use 3 different config file and one single jar. And give the different config file and same sigle jar to different persons. This way at least we can confirm that the applicaiton (jar) is the same for test and deploy.
But the "Settings.settings" seems binded to the "solution" in vs. The pre processor seems not good enough, seems that it binded to the "solution" too. I have to change it in the "Properties" every time before I build the project? Or I need to create more "Solutions"?
Maybe I did't look that deep enough, I will keep looking
=== SOLVED
We finaly solved (partly) using Conditional compilation
#if DEBUG
const string baseUrl = "192.168.20.101:8001/api";
#endif
#if (!DEBUG)
const string baseUrl = "xxx.com/api";
#endif
one small issue is that we need to distribute debug package to our test team.

How to configure cache folder for SourceDiskCache?

I understand from the documentation that the SourceDiskCache folder cannot be configured using the XML configuration file and is only available "through code installation". However I can't figure out how!
I need to configure a custom folder. I have tried a few different things, with different results (both in Application_Start):
This doesn't throw an error, but uses the default folder (/cache)
var sourceDiskCachePlugin = new SourceDiskCachePlugin {VirtualCacheDir = "~/App_Data/cache"};
Config.Current.Plugins.GetOrInstall(sourceDiskCachePlugin);
This (and most other variations I have tried) throws the error "SourceDiskCache settings may not be adjusted after it is started."
new SourceDiskCachePlugin().Install(Config.Current);
Config.Current.Plugins.Get<SourceDiskCachePlugin>().VirtualCacheDir = "~/App_Data/cache";
How can I configure this?
Also, the documentation states that SourceDiskCache is in beta - is this still the case, and will XML configuration ever be available?
This would be the normal way to configure and install it:
var plugin = new SourceDiskCachePlugin()
plugin.VirtualCacheDir = "~/App_Data/cache";
plugin.Install(Config.Current);
If your code is running more than once, use Config.Current.Plugins.GetOrInstall(plugin); It's best if you only install the plugin during Application_Start.
However, approach #1 from your question should work equally well, as long as you've set the right NTFS permissions on App_Data.

Can MSBuild (via Microsoft.Build namespace) load a project from memory?

I'm working on a service that will download a .NET solution from a repository and build it (not unlike a continuous-integration build service). What I want to know is, using MSBuild programmatically via the Microsoft.Build namespace classes, can I can load the solution and project(s) into memory and build it without first saving them to disk in a temporary folder?
I'm still learning MSBuild and trying things, but I figure someone on Stack Overflow has tried this and has some insight.
I can't speak to whether this is a good idea or not, but it's possible to do.
ProjectInstance has a constructor that accepts a ProjectRootElement, which can be constructed via the Create(XmlReader) method. And as you may know, XmlReader can be attached to various Streams including a MemoryStream.
Here's how something like this may look:
var xmlReader = XmlTextReader.Create([your existing memory stream]);
var project = ProjectRootElement.Create(xmlReader);
var buildParams = new BuildParameters();
var buildData = new BuildRequestData(new ProjectInstance(project),
new string[] { "Build", "Your Other Target Here" });
var buildResult = BuildManager.DefaultBuildManager.Build(buildParams, buildData);
After researching MSBuild and all the involved components, it appears as though it's necessary to have the entire project set up on the file system before being able to build it. Unfortunately, what I'm trying to do just can't be done with the provided toolset.

Load App.Config at runtime

I am creating a test project using IBM's Rational Functional Tester(RFT) tool(we are using the VB version of it and it only supports till .net 3.5).In this project I am making all database queries using the entity framework.The problem is entity framework retrieves "Metadata", "Connection String" etc from the App.Config file,and RFT won't let me add a App.Config to the project(I guess it is designed that way -- I googled adding an App.Config file to an rft project and came up with nothing), and the Entity Framework requires you to have the app.config file at the entry point.I was building the string to pass to entity framework in code, but my boss really does not like that.
So looking at my options I think any of the below 2 solutions should suffice(or if you have a better solution please advise).
Is their any way to load an App.Config during run-time.
Is their any way to add an App.Config to an RFT project(should
really be my first question).
If you guys could help me with this it will be great.
Thanks in advance.
After a lot of research i found you can load the config file at runtime by using,
ExeConfigurationFileMap map = new ExeConfigurationFileMap();
map.ExeConfigFilename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Assembly.GetExecutingAssembly().ManifestModule.Name + ".config");
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
var configValue = config.ConnectionStrings.ConnectionStrings["refrenceNameinConfigfile"].ConnectionString;
just make sure your App.config is within the folder where your exe file is running

What is the best way for modifying web.config in SharePoint using code

What is the best way for modifying web.config in SharePoint using code? I could use SPWebConfigModification or powershell.
If you want to do it using code, SPWebConfigModification will be a good choice because that will take care of web.config modifications across all front end web servers in your farm. Doing it via PowerShell is almost like modifying it manually which is discouraged practice for SharePoint enviornment.
If you are using FEATURES, you may want to put your code in Feature Receivers.
If declarative modifications serves your need, you may prefer that (over code approach) as well. Entries like SafeControl are natively supported by Feature framework which should be the first choice for such entries. Another variation of declarative modifications is via supplemental .config file as described below:
http://msdn.microsoft.com/en-us/library/ms460914.aspx
You can do web.config modifications from VS 2010 code. See below code for sample of the same. I have implemented
//Add tagMapping
string modificationName = string.Format(#"add[#tagType='Microsoft.SharePoint.WebControls.PeopleEditor'][#mappedTagType='PeopleEditor.CustPeopleFind,{0}']",
Assembly.GetExecutingAssembly().FullName);
SPWebConfigModification modification = new SPWebConfigModification //(modificationName, "configuration/system.web/pages/tagMapping");
{
Path = "configuration/system.web/pages/tagMapping",
Name = modificationName,
Value = string.Format(#"<add tagType=""Microsoft.SharePoint.WebControls.PeopleEditor"" mappedTagType=""PeopleEditor.CustPeopleFind"" />"),
Owner = ownerID,
Sequence = 1,
Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode
};
webApp.WebConfigModifications.Add(modification);
//Save changes
webApp.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
//Serialize and propagate changes across farm
webApp.Update();
//Create the persistent objects
Common.CreatePersistentObjects(webApp);
But, I should also warne you, many SharePoint experts do not recommend modifications done to web.config through code.
All the best
-Vighnesh