how to use gitlab variable value inside config file of c# - c#

I have declared variable in gitlab at project level,I want to use value of that variable inside web.config to set value of a key .
<appsettings>
<add key="portno" value="${GIT_PORT}">
</appSettings>
Gitlab variable is GIT_PORT.
I am not able to replace actual value from variable.
Can anyone suggest?

GitLab Project Level Variables will be available as Environment variables in GtiLab Runner when it is running your Job. And You cannot use Environment Variable directly in the web.config file.
But as a workaround, you can use scripts to replace the value with CI environment variables.
Example:
assume this is your web.config
<appsettings>
<add key="portno" value="Some-Text-To_Replace">
</appSettings>
Then, Run this command.
$ sed -i 's/Some-Text-To_Replace/${GIT_PORT}/g' web.config
Hope This will work.

Related

How to change the app.settings from Nunit console command line for tests

I have set of selenium tests developed with VS 2017,Nunit (Project type - class library using .net framework 452. In my OneTimeSetUp, I am reading app.config that define few things such web URL, database connection string, Web Login user ID/password etc.
example:
<appSettings>
<add key="Browser" value="IExplorer" />
<add key="User" value="xxx" />
<add key="Password" value="xxx" />
<add key="BaseURL" value="http://ccc.com" />
<add key="DefaultImplicitDriverWait" value="15" />
<add key="TestRailIntegrationValue" value="False" />
<add key="GenerateCustomReport" value="False" />
</appSettings>
<connectionStrings>
<add name="DB1" connectionString="Data Source=db_name;Initial Catalog=db_instance;User Id=userid;Password=pwd;" />
</connectionStrings>
Then to run tests, I use nunit command line -- example
cd "C:\path-to-repo location"
packages\NUnit.ConsoleRunner.3.7.0\tools\nunit3-console pathToDll --where "cat == Demo"
cmd /k
Now this works fine as long I target for one env. Now when I need to switch tests to different env, before running tests from command line or from VS 2017, I need to change the app.config bu pointing to different env variables.
So is there a way, i can pass these values as parameter in the command line that then update the app.config before executing any test?
If there any alternate solution/suggestion, I am happy to get all suggestion.
NUnit doesn't make use of or modify in any way your config file. All it does is make sure it's available to the tests.
This issue asks for the addition of a command-line option to change the config file when running NUnit. You may want to add your voice to the issue.
Meanwhile, the option available through NUNit is to specify the individual parameters using the --params option, for example:
--params "Browser=IExplorer"
If you are willing to read a config file yourself, you could even specify its name using --params.
Please, consider theese 2 approaches:
1) Look How to modify my App.exe.config keys at runtime?
You can use this approach. First set some env vars in your CI server, read them and change your app.config somewhere in [OneTimSetUp] method or whatever attribute you have in your test framework.
2) Look at this plugin https://marketplace.visualstudio.com/items?itemName=VisualStudioProductTeam.SlowCheetah-XMLTransforms and here http://www.c-sharpcorner.com/article/transform-config-using-slow-cheetah/
I used it and really like it. On CI in your job you just need change target from release to whatever you create and it works really fine. And another advantage - you can easily switch to any app.config in Visual Studio and run/debug test on any env just choosing your config from dropdown.
P.S. In both cases you don't need to change anything in Nunit console command
In Nunit 3 they added a --configfile option. See this small description.
Also here is some more information on the params flag:
--params|p=PARAMETER A test PARAMETER specified in the form NAME=VALUE for consumption by tests. Multiple parameters may be specified, separated by semicolons or by repeating the --params option multiple times. Case-sensitive.

Couldn't take value from web.config from other project

I have added web.Config file in my main project (where exe file resides), I need to access a value from this web.cofig file from other project.
<appSettings>
<add key="Name" value="TestProject" />
</appSettings>`
I am taking value using WebConfigurationManger as follows,
WebConfigurationManager.AppSettings["Name"]
But it always returns null. What I am missing here ?
First of all you have to create project Dependency base on your requirement. then you can access your parent project web.config property.
System.Configuration.ConfigurationManager.AppSettings["Name"]

Merger app.config from custom location

I have a scenario where I need to read an environment variable on start, then depending on the value of the ENV variable I need to merge a config file with the App.config file.
E.g.
MyApp.exe
MyApp.exe.config
Stage\MyApp.exe.config
Live\MyApp.exe.config
On start
-if the Environment variable = Stage, then Merge the config file in the Stage folder
-if the Environment variable = Live, then Merge the config file in the Live folder
If I had the following in the default MyApp.exe.config
<appSettings>
<add key="SomeKey" value="SomeValue">
</appSettings>
and then had the following in the Stage\MyApp.exe.config
<appSettings>
<add key="SomeKey" value="Some NEW Value">
</appSettings>
I'd expect my application to read the value for SomeKey as "Some NEW Value".
I can't seem to find a clean way to implement this exact approach.
Thanks
Warrick
You might want to use Build Events in Visual Studio.
http://msdn.microsoft.com/en-us/library/42x5kfw4(v=vs.90).aspx
You can use the variable to copy the overwrite the app.config in the pre-build event:
$(ConfigurationName)
If the config name is DEV, then copy the app.config.DEV over the app.config
If the config name is PROD, then copy the app.config.PROD over the app.config

Hiding private details from open source projects

I have a .net github project that is basically a wrapper around a web API. In the test project, I am calling to the API using an API key. I need to keep this key private, how do I accomplish this in a visual studio project?
In some other projects, like python, I can have git ignore the file (config.py) and use something like config.example.py. But in visual studio's case, the project will not compile because of the missing file Config.cs. What is the proper way to solve this? I'm thinking of using this same method of ignoring the file and have them execute a build script that should rename Config.example.cs to Config.cs?
This is the perfect for .config files. Depending on whether its a web or console application, you will have a web.config or app.config file in your project.
You can use the appSettings section to store your API key.
To make things even easier, you can actually have this section read from another file, ie: specialappsettings.config and then just ignore that single file from your repository.
Modify your web.config (or app.config):
<configuration>
<appSettings file="specialappsettings.config">
</appSettings>
<system.web>
<!-- standard web settings go here -->
</system.web>
</configuration>
Create a new specialappsettings.config file:
<appSettings>
<add key="APIKey" value="YourApiKeyValue" />
<add key="AnotherKey" value="AnotherValue" />
</appSettings>
This can be accessed in your code via:
var apiKey = ConfigurationManager.AppSettings["APIKey"];
Notes:
You can keep your settings within the original web.config file as
well but this lets you ignore just the specific settings file from
your git repository without affecting the rest of the project's
necessary configuration details.
The same "key" can be saved in
either file however the external file will override the original
web.config file value.
You are probably looking for the App.config file for a project. It will be copied to <application>.exe.config when you compile it. Users can edit that config file as needed.
In that config file, you can add your API keys:
<configuration>
<appSettings>
<add key="APIKey" value="12345"/>
</appSettings>
</configuration>
Then you can access it from your code using ConfigurationManager.AppSettings:
string apiKey = ConfigurationManager.AppSettings["APIKey"];
One option is to use .config files instead of having secret keys hardcoded in sources.
More info Using Settings in C# and step-by-step guide
<configuration>
<appSettings>
<add key="SecretKey" value="0" />
</appSettings>
</configuration>
var secretKey = ConfigurationManager.AppSettings.Get("SecretKey");
Perhaps you can store the key outside of the Config.cs file and load it at run time.
Bonus, other people using your code won't have to recompile the project to change to their API key.

web.config transform elements not available through System.Configuration.ConfigurationManager

I have a web.config file that's transformed using a web.debug.config file. Trying to get access to these values through System.Configuration.ConfigurationManager.AppSettings is yielding nothing.
My web.config appSettings are empty.
<configuration>
<appSettings>
</appSettings>
</configuration>
My web.debug.config transform that's applied. Here's a sample.
<configuration>
<appSettings>
<add key="CommonURL" value="localhost/mysite/" xdt:Transform="Insert" />
</appSettings>
</configuration>
Here's the code to try and get this value, which returns null.
var cu = System.Configuration.ConfigurationManager.AppSettings["CommonURL"];
Any idea on why this could be?
Assuming that System.Configuration.ConfigurationManager.AppSettings is yielding nothing when you run from Visual Studio, then that is the expected behavior. You should add the CommonURL appSetting to your web.config file and change the entry in the web.debug.config to:
<add key="CommonURL" value="localhost/mysite/"
xdt:Transform="Replace" xdt:Locator="Match(key)" />
These changes will allow you to get the value specified in the web.config when you run from Visual Studio, and will allow that value to be replaced with what is defined in the web.debug.config when you execute the transform (for example, through the Publish function or through a custom MSBuild script). Keep in mind that the transforms are applied when you publish, not when you start running a debug or release build. The values that are in the web.config file are always the ones that are honored.

Categories