Accessing environment variable value from service fabric environment xml file
Accessing environment variable value from service fabric environment xml file
I have a scenario where I need to read an environment variable from the machine where my Service Fabric application is deployed.
More specifically in my cloud.xml (environment file) I want StorageConnectionString
to use the value from one of the environment variable that is set on the machine by some other external tool.
StorageConnectionString
<?xml version="1.0" encoding="utf-8"?>
<Application xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Name="fabric:/AppFabricName.ServiceFabric" xmlns="http://schemas.microsoft.com/2011/01/fabric">
<Parameters>
<Parameter Name="StorageConnectionString" Value="%ENVVARIABLE%" />
</Parameters>
</Application>
Is the above valid ? Did not work when I tried even though running SET
on cmd prompt did show that variable exists.
SET
I don’t have access to code. The SF application is simply given by external team to deploy while providing those values.
– Frank Q.
Jun 26 at 18:41
Can you provide an example how your application consumes StorageConnectionString?
– Oleg Karasik
Jun 27 at 8:06
It’s used to connect to SQL dB. It looks like SF doesn’t support replacing %envvariable% with actual environment variable.
– Frank Q.
Jun 27 at 16:42
2 Answers
2
It's not straight forward, but here's a way:
If you are passing the data via configuration files(or parameters) at deployment and accessing it via code using Context.CodePackageActivationContext.GetConfigurationPackageObject("Config")
, the right way should be using <Parameter Name="StorageConnectionString" Value="[ENVVARIABLE]" />
(note that it is encolsed by [ ]) and have a configuration file with a variable called ENVVARIABLE
and set the overrides to your service. You should not need to set environment variables, environment variables is other thing.
Context.CodePackageActivationContext.GetConfigurationPackageObject("Config")
<Parameter Name="StorageConnectionString" Value="[ENVVARIABLE]" />
ENVVARIABLE
Please take a look at this SO question to check if will solve the problem.
If your plan is getting an environment variable already set in the machine, unfortunately SF does not support Environment Variable Replacement using tokens like %ENVVARIABLE%
.
%ENVVARIABLE%
even though running SET on cmd prompt did show that variable exists.
The other process that changes the environment variable, if it just use the default set, it will set their own process environment variables, to be available to other processes, it has to update environment variable on Machine or User scope(assuming they run on same user), this is why you can't see from command running SET variablename
SET variablename
...
Assuming the variable is set correctly and it is a machine scope variable...
When the Environment Variable is set before the process start, I would suggest you setup an EntryPoint Script to set it at startup, like you would do to a guest executable.
See a NodeJs example here
You could maybe also do write to file and read from your app as Loek suggested, but I think it would be too complicate for too litle.
In case the environment variable is set after the process started, or if it changes later, you should get it from within your application directly instead, you could just use:
System.Environment.GetEnvironmentVariable(variableName,EnvironmentVariableTarget.Machine);
And you pass the variable name via config.
In both cases your userprocess must have permission to read Environment Variable from Machine Scope
The SF application reads the value from config section as follows:
Context.CodePackageActivationContext.GetConfigurationPackageObject("Config").Settings.Sections["UserDatabase"].Parameters["StorageConnectionString"];
So, updating environment variable from batch script will not help. The value is passed from the cloud.xml file and overridden in application manifest parameters.– Frank Q.
Jun 27 at 17:29
Context.CodePackageActivationContext.GetConfigurationPackageObject("Config").Settings.Sections["UserDatabase"].Parameters["StorageConnectionString"];
I've added extra info, please see the answer again
– Diego Mendes
Jun 29 at 9:46
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
if the environment variable is set by a different process, can't you just read it from your code instead of reading it from config?
– LoekD
Jun 26 at 18:38