Bạn chỉ có thể sử dụng các biến môi trường giống như bất kỳ ứng dụng nào khác, điều này cũng hoạt động với tệp thực thi của khách trong cấu trúc dịch vụ không giống settings.xmlnhư điều này yêu cầu thời gian chạy cấu trúc dịch vụ tích hợp sẵn.
Trong ứng dụng của bạn, bạn có thể truy cập các biến môi trường giống như bất kỳ ứng dụng .net nào khác thông qua GetEnvironmentVariablephương thức trên Environmentlớp:
var baseUri = Environment.GetEnvironmentVariable("SuperWebServiceBaseUri");
Sau đó, chúng ta cần thiết lập một số giá trị biến môi trường mặc định, điều này được thực hiện trong ServiceManifest.xmltệp kê khai của dịch vụ.
<?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>
Sau đó, biến môi trường này có thể được ghi đè trong ApplicationManifest.xmltệp bằng cách sử dụng mã sau:
<?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>
Sau đó, điều này có thể được tham số hóa giống như bất kỳ cài đặt tệp kê khai ứng dụng nào khác bằng cách sử dụng local.xmlvà 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>
Sau đó, chúng tôi sẽ phải cập nhật ApplicationManifest.xmlđể hỗ trợ các tham số này;
<?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>