1

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.

<?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.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Frank Q.
  • 6,001
  • 11
  • 47
  • 62
  • 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 '18 at 18:38
  • 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 '18 at 18:41
  • Can you provide an example how your application consumes **StorageConnectionString**? – Oleg Karasik Jun 27 '18 at 08: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 '18 at 16:42

2 Answers2

2

It's not straight forward, but here's a way:

  1. Specify a setup entry point (a new executable or script) to read the environment variable and write it to a file in the application's work directory.
  2. When the main entry point comes up, read the file from the work directory.
LoekD
  • 11,402
  • 17
  • 27
0

If you are passing the data via configuration files(or parameters) at deployment and accessing it via code using Context.CodePackageActivationContext.GetConfigurationPackage‌​Object("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.

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%.

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

...

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 user\process must have permission to read Environment Variable from Machine Scope

Diego Mendes
  • 10,631
  • 2
  • 32
  • 36
  • 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 '18 at 17:29
  • I've added extra info, please see the answer again – Diego Mendes Jun 29 '18 at 09:46