在 Service Fabric 中,我可以使用应用程序参数更改 ServiceManifest.xml 文件中的参数吗?

In Service Fabric, can I alter the Arguments in the ServiceManifest.xml file using Application Parameters?

我有一个 ApplicationManifest.xml 文件,如下所示:

<?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" xmlns="http://schemas.microsoft.com/2011/01/fabric"
   ApplicationTypeName="ServiceFabricTestType" ApplicationTypeVersion="1.9">
   <Parameters>
     <Parameter Name="Prop_BehavioursPath" DefaultValue="behaviours.yml"/>
     <Parameter Name="Prop_AliasesPath" DefaultValue="aliases.yml"/>
   </Parameters>
  <ServiceManifestImport>
  <ServiceManifestRef 
    ServiceManifestName="SummaryGenerator" 
    ServiceManifestVersion="1.9.0.0" 
    />
  </ServiceManifestImport>
</ApplicationManifest>

我想使用参数来调整我的来宾托管服务的参数,在 ServiceManifest.xml 文件中声明如下:

<?xml version="1.0" encoding="utf-8"?>
<ServiceManifest xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/2011/01/fabric"
   Name="SummaryGenerator" Version="1.9.0.0">
   <ServiceTypes>
     <StatelessServiceType ServiceTypeName="SummaryGenerator" UseImplicitHost="true"/>
   </ServiceTypes>
   <CodePackage Name="code" Version="1.9.0.0">
   <EntryPoint>
     <ExeHost>
        <Program>MyProgram.exe</Program>
        <Arguments>&quot;LoadFrom=[Prop_AliasesPath]|[Prop_BehavioursPath]&quot;</Arguments>
        <ConsoleRedirection FileRetentionCount="5" FileMaxSizeInKb="2048"/>
     </ExeHost>
   </EntryPoint>
  </CodePackage>
</ServiceManifest>

这显然行不通,因为进入参数的属性被逐字处理,而不是从参数值中解析出来。

我真正想做的是能够启动服务并为 Prop_BehavioursPath 和 Prop_AliasesPath 传递不同的值。在 Service Fabric 中是否有更好的方法来执行此操作?

运行 应用程序不知道 Service Fabric,将配置推送给它的唯一方法是通过命令参数。

看起来您做不到...相反,您可以尝试一种变通方法,即编写一个小型 .NET 包装器来读取 sf 配置,然后启动来宾可执行文件。您可以从子进程重定向 stdin/stdout 并挂钩到它的退出事件,以便主进程在子进程终止时终止。

与参数类似,您可以在 ServiceManifest.xml 中定义环境变量(比如 Prop_AliasesPath andProp_BehavioursPath),然后在 ApplicationManifest.xml 中覆盖它们的值。那么你有两个选择:

选项 1: 即使您的入口点 MyProgram.exe 不支持服务结构,它也可以读取环境变量。

选项 2: 为避免读取 MyProgram.exe 内的环境变量,您可以使用批处理文件作为入口点并从中调用 "MyProgram.exe LoadFrom=%Prop_AliasesPath%%Prop_BehavioursPath%"

有关覆盖环境变量的更多信息: https://dzimchuk.net/using-code-package-environment-variables-in-service-fabric/