服务水平
在位于{YourService}\PackageRoot\ServiceManifest.xml 下的ServiceManifest.xml 中。
您将有一个条目 CodePackage 添加标签 EnvironmentVariables 在它下面加上您想要使用的 EnvironmentVariables 和一个空值。
<CodePackage Name="Code" Version="1.0.0">
<EntryPoint>
<!--...-->
</EntryPoint>
<EnvironmentVariables>
<EnvironmentVariable Name="Connectionstring" Value=""/>
</EnvironmentVariables>
</CodePackage>
要在您的代码中访问它,您可以使用
Environment.GetEnvironmentVariable("Connectionstring", EnvironmentVariableTarget.Process);
应用层
在您的ApplicationManifest.xml{YourApplication}\ApplicationPackageRoot\ApplicationManifest.xml 下
有一个部分<Parameters> 为您要在部署时设置的每个值添加一个参数。
<Parameters>
<Parameter Name="Connectionstring" DefaultValue="{your connectionstring}" />
</Parameters>
在同一个文件(应用程序清单)中有ServiceManifestImport,您需要为每个服务使用EnvironmentOverrides 扩展它。
<ServiceManifestImport>
<ServiceManifestRef ServiceManifestName="MyServicePkg" ServiceManifestVersion="1.0.0" />
<ConfigOverrides />
<EnvironmentOverrides CodePackageRef="code">
<EnvironmentVariable Name="Connectionstring" Value="[Connectionstring]" />
</EnvironmentOverrides>
</ServiceManifestImport>
方括号中的值,是被<Parameters>标签中定义的参数替换的值。
环境特定配置
在{YourApplication}\ApplicationParameters 文件夹下。为每个环境创建一个文件。以Test.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:/{YourApplication}" xmlns="http://schemas.microsoft.com/2011/01/fabric">
<Parameters>
<Parameter Name="Connectionstring" Value="{Your test connection string}" />
</Parameters>
</Application>
创建设置文件后,您需要创建使用此设置文件的发布配置文件。 Uner {YourApplication}\PublishProfiles\ 为环境创建一个文件。示例Test.xml
<?xml version="1.0" encoding="utf-8"?>
<PublishProfile xmlns="http://schemas.microsoft.com/2015/05/fabrictools">
<ClusterConnectionParameters ConnectionEndpoint="{YourEndpoint}" />
<!--Reference the settings file-->
<ApplicationParameterFile Path="..\ApplicationParameters\Test.xml" />
<UpgradeDeployment Mode="UnmonitoredAuto" Enabled="true">
<!--Specify additonal upgrade parameters here-->
<Parameters />
</UpgradeDeployment>
</PublishProfile>
完成此操作后,只需使用正确的发布配置文件部署应用程序即可。