Ortam değişkenlerini tıpkı diğer uygulamalar gibi kullanabilirsiniz ; bu, yerleşik hizmet yapısı çalışma zamanını gerektirdiğinden farklı olarak hizmet yapısı içinde konuk yürütülebilir ile de çalışır settings.xml.
Uygulamanız içinde, diğer herhangi bir .net uygulaması gibi GetEnvironmentVariable, Environmentsınıftaki yöntem olsa da ortam değişkenlerine erişebilirsiniz :
var baseUri = Environment.GetEnvironmentVariable("SuperWebServiceBaseUri");
Daha sonra bazı varsayılan ortam değişkenleri değerlerini ayarlamamız gerekir, bu ServiceManifest.xmlhizmetin manifest dosyasında yapılır .
<?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>
Bu ortam değişkeni daha sonra ApplicationManifest.xmlaşağıdaki kod kullanılarak dosya içinde geçersiz kılınabilir :
<?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>
Bu, daha sonra diğer uygulama bildirim ayarları gibi local.xmlve kullanılarak parametrelendirilebilir 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>
Daha sonra ApplicationManifest.xmlbu parametreleri desteklemek için güncellememiz gerekecek ;
<?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>