我有一个应用程序清单.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>"LoadFrom=[Prop_AliasesPath]|[Prop_BehavioursPath]"</Arguments>
<ConsoleRedirection FileRetentionCount="5" FileMaxSizeInKb="2048"/>
</ExeHost>
</EntryPoint>
</CodePackage>
</ServiceManifest>
这显然不起作用,因为进入参数的属性被视为逐字,而不是从参数值中解析。
我真正想做的是能够启动一个服务并为Prop_BehavioursPath和Prop_AliasesPath传递不同的值。是否有更好的方法可以在 Service Fabric 中执行此操作?
正在运行的应用程序不知道 Service Fabric,将配置推送到它的唯一方法是通过命令参数。
看起来你做不到...相反,您可以尝试一种解决方法,即编写一个小型 .NET 包装器来读取 sf 配置,然后启动来宾可执行文件。您可以从子进程重定向 stdin/stdout,并挂接到它的退出事件,以便主进程在子进程终止时终止。
与参数类似,您可以在 ServiceManifest 中定义环境变量(例如 Prop_AliasesPath andProp_BehavioursPath.xml然后在 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/