You can simply use environment variables just like any other application, this also works with guest executables in the work structure as opposed to settings.xml because it requires a built-in runtime environment.
In your application, you can access environment variables, like any other.net application, although the GetEnvironmentVariable method in the Environment class:
var baseUri = Environment.GetEnvironmentVariable("SuperWebServiceBaseUri");
Then we need to set some default environment variable values, this is done in the ServiceManifest.xml manifest file of the service.
<?xml version="1.0" encoding="utf-8" ?> <ServiceManifest Name="MyServicePkg" Version="1.0.0" xmlns="http://schemas.microsoft.com/2011/01/fabric" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <CodePackage Name="Code" Version="1.0.0"> <EnvironmentVariables> <EnvironmentVariable Name="SuperWebServiceBaseUri" Value="http://localhost:12345"/> </EnvironmentVariables> </CodePackage> </ServiceManifest>
You can then override this environment variable in the ApplicationManifest.xml file using the following code:
<?xml version="1.0" encoding="utf-8"?> <ApplicationManifest xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ApplicationTypeName="ChileTargetType" ApplicationTypeVersion="1.0.0" xmlns="http://schemas.microsoft.com/2011/01/fabric"> <Parameters> </Parameters> <ServiceManifestImport> <ServiceManifestRef ServiceManifestName="MyServicePkg" ServiceManifestVersion="1.0.0" /> <EnvironmentOverrides CodePackageRef="Code"> <EnvironmentVariable Name="SuperWebServiceBaseUri" Value="https://the-real-live-super-base-uri.com/"/> </EnvironmentOverrides> </ServiceManifestImport> </ApplicationManifest>
This parameter can then be parameterized, like any other application manifest parameter, using local.xml and cloud.xml .
<?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="MyService_SuperWebServiceBaseUri" Value="https://another-base-uri.com/" /> </Parameters> </Application>
Then we will need to update ApplicationManifest.xml to support these parameters;
<?xml version="1.0" encoding="utf-8"?> <ApplicationManifest xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ApplicationTypeName="ChileTargetType" ApplicationTypeVersion="1.0.0" xmlns="http://schemas.microsoft.com/2011/01/fabric"> <Parameters> <Parameter Name="MyService_SuperWebServiceBaseUri" DefaultValue="https://the-real-live-super-base-uri.com/" /> </Parameters> <ServiceManifestImport> <ServiceManifestRef ServiceManifestName="MyServicePkg" ServiceManifestVersion="1.0.0" /> <EnvironmentOverrides CodePackageRef="Code"> <EnvironmentVariable Name="SuperWebServiceBaseUri" Value="[MyService_SuperWebServiceBaseUri]"/> </EnvironmentOverrides> </ServiceManifestImport> </ApplicationManifest>
Kevin Smith Jan 10 '17 at 9:42 on 2017-01-10 09:42
source share