這項功能會在 Service Fabric 應用程式設計中引進 StartupServices.xml 檔案。 此檔案裝載 ApplicationManifest.xml的 DefaultServices 區段。 透過此實作,DefaultServices 和服務定義相關參數會從現有的 ApplicationManifest.xml 移至名為 StartupServices.xml的新檔案。 此檔案會用於 Visual Studio 中的每個功能 (Build/Rebuild/F5/Ctrl+F5/Publish)。
StartupServices.xml 僅適用於Visual Studio部署。 此安排可確保使用 Visual Studio 部署的套件(StartupServices.xml)與 ARM 部署的服務沒有衝突。
StartupServices.xml 未作為應用程式套件的一部分進行封裝。 DevOps 管線不支援它,客戶應該 透過ARM 或透過具有所需設定 的 Cmdlet ,在應用程式指令清單中部署個別服務。
現有的 Service Fabric 應用程式設計
針對每個 Service Fabric 應用程式,ApplicationManifest.xml 是應用程式所有服務相關信息的來源。 ApplicationManifest.xml 包含所有參數、服務清單導入和預設服務。 在 ApplicationParameters 底下的 Cloud.xml/Local1Node.xml/Local5Node.xml 檔案中會提及組態參數。
在應用程式中新增新的服務時,會在 ApplicationManifest.xml內新增服務參數、ServiceManifestImport 和 DefaultServices 區段。 組態參數會新增在 ApplicationParameters 底下的 Cloud.xml/Local1Node.xml/Local5Node.xml 檔案中。
當使用者在 Visual Studio 中選取 [建置/重建] 函式時,會在 ApplicationManifest.xml中修改 ServiceManifestImport、Parameters 和 DefaultServices 區段。 組態參數也會在 ApplicationParameters 下的 Cloud.xml/Local1Node.xml/Local5Node.xml 檔案中編輯。
當使用者觸發 F5/Ctrl+F5/Publish 時,會根據 ApplictionManifest.xml中的資訊部署或發佈應用程式和服務。 組態參數會從 ApplicationParameters 底下的任何 Cloud.xml/Local1Node.xml/Local5Node.xml 檔案使用。
範例 ApplicationManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<ApplicationManifest ApplicationTypeName="SampleAppType"
ApplicationTypeVersion="1.0.0"
xmlns="http://schemas.microsoft.com/2011/01/fabric"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Parameters>
<Parameter Name="Web1_ASPNETCORE_ENVIRONMENT" DefaultValue="" />
<Parameter Name="Web1_MinReplicaSetSize" DefaultValue="3" />
<Parameter Name="Web1_PartitionCount" DefaultValue="1" />
<Parameter Name="Web1_TargetReplicaSetSize" DefaultValue="3" />
</Parameters>
<!-- Import the ServiceManifest from the ServicePackage. The ServiceManifestName and ServiceManifestVersion
should match the Name and Version attributes of the ServiceManifest element defined in the
ServiceManifest.xml file. -->
<ServiceManifestImport>
<ServiceManifestRef ServiceManifestName="Web1Pkg" ServiceManifestVersion="1.0.0" />
<ConfigOverrides />
<EnvironmentOverrides CodePackageRef="code">
<EnvironmentVariable Name="ASPNETCORE_ENVIRONMENT" Value="[Web1_ASPNETCORE_ENVIRONMENT]" />
</EnvironmentOverrides>
</ServiceManifestImport>
<DefaultServices>
<!-- The section below creates instances of service types, when an instance of this
application type is created. You can also create one or more instances of service type using the
ServiceFabric PowerShell module.
The attribute ServiceTypeName below must match the name defined in the imported ServiceManifest.xml file. -->
<Service Name="Web1" ServicePackageActivationMode="ExclusiveProcess">
<StatefulService ServiceTypeName="Web1Type" TargetReplicaSetSize="[Web1_TargetReplicaSetSize]" MinReplicaSetSize="[Web1_MinReplicaSetSize]">
<UniformInt64Partition PartitionCount="[Web1_PartitionCount]" LowKey="-9223372036854775808" HighKey="9223372036854775807" />
</StatefulService>
</Service>
</DefaultServices>
</ApplicationManifest>
使用 StartupServices.xml 進行新的 Service Fabric 應用程式設計
在此設計中,服務等級資訊(例如服務定義和服務參數)和應用層級資訊(ServiceManifestImport 和 ApplicationParameters)之間有明確的區別。 StartupServices.xml 包含所有服務層級資訊,而 ApplicationManifest.xml 包含所有應用層級資訊。 另一個變更是在 StartupServiceParameters 下新增 Cloud.xml/Local1Node.xml/Local5Node.xml,這些僅用於服務參數的組態。 ApplicationParameters 下的現有 Cloud.xml/Local1Node.xml/Local5Node.xml 只包含應用層級參數組態。
在應用程式中新增新的服務時,應用程式層級參數和 ServiceManifestImport 會在 ApplicationManifest.xml中新增。 應用程式參數的組態會新增在 ApplicationParameters 底下的 Cloud.xml/Local1Node.xml/Local5Node.xml 檔案中。 服務資訊和服務參數會新增在 StartupServices.xml 中,服務參數的組態會新增到 StartupServiceParameters 下的 Cloud.xml/Local1Node.xml/Local5Node.xml。
在專案的建置/重建過程中,ServiceManifestImport 的修改會導致應用程式參數在 ApplicationManifest.xml中發生變更。 應用程式參數的組態也會在 ApplicationParameters 底下的 Cloud.xml/Local1Node.xml/Local5Node.xml 檔案中編輯。 服務相關信息會在 StartupServices.xml 中編輯,且在 StartupServiceParameters 下的 Cloud.xml/Local1Node.xml/Local5Node.xml 中編輯服務參數。
在 Visual Studio 中觸發 F5/Ctrl+F5/Publish 時,應用程式會根據來自 ApplictionManifest.xml 的資訊,以及來自 ApplicationParameters 下任何 Cloud.xml/Local1Node.xml/Local5Node.xml 檔案的資訊來部署/發佈應用程式。 每個服務都會個別啟動,並且使用來自 StartupServices.xml 的服務資訊和來自 StartupServiceParameters 底下任一 Cloud.xml/Local1Node.xml/Local5Node.xml 檔案的服務參數配置。
發佈應用程式之前,可以編輯這些服務參數和應用程式參數(按滑鼠右鍵->發佈),如圖所示。
新設計中的範例 ApplicationManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<ApplicationManifest ApplicationTypeName="SampleAppType"
ApplicationTypeVersion="1.0.0"
xmlns="http://schemas.microsoft.com/2011/01/fabric"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Parameters>
<Parameter Name="Web1_ASPNETCORE_ENVIRONMENT" DefaultValue="" />
</Parameters>
<!-- Import the ServiceManifest from the ServicePackage. The ServiceManifestName and ServiceManifestVersion
should match the Name and Version attributes of the ServiceManifest element defined in the
ServiceManifest.xml file. -->
<ServiceManifestImport>
<ServiceManifestRef ServiceManifestName="Web1Pkg" ServiceManifestVersion="1.0.0" />
<ConfigOverrides />
<EnvironmentOverrides CodePackageRef="code">
<EnvironmentVariable Name="ASPNETCORE_ENVIRONMENT" Value="[Web1_ASPNETCORE_ENVIRONMENT]" />
</EnvironmentOverrides>
</ServiceManifestImport>
</ApplicationManifest>
範例 StartupServices.xml 檔案
<?xml version="1.0" encoding="utf-8"?>
<StartupServicesManifest 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">
<Parameters>
<Parameter Name="Web1_InstanceCount" DefaultValue="-1" />
</Parameters>
<Services>
<!-- The section below creates instances of service types, when an instance of this
application type is created. You can also create one or more instances of service type using the
ServiceFabric PowerShell module.
The attribute ServiceTypeName below must match the name defined in the imported ServiceManifest.xml file. -->
<Service Name="Web1" ServicePackageActivationMode="ExclusiveProcess">
<StatelessService ServiceTypeName="Web1Type" InstanceCount="[Web1_InstanceCount]">
<SingletonPartition />
</StatelessService>
</Service>
</Services>
</StartupServicesManifest>
SF SDK 5.0.516.9590 版和更新版本中的所有新專案都已啟用 startupServices.xml 功能。 使用舊版 SDK 建立的專案與最新的 SDK 完全回溯相容。 不支援將舊專案移轉至新設計。 如果使用者想要在較新版本的 SDK 中建立 Service Fabric 應用程式而不 StartupServices.xml,則使用者應該選取 [協助我選擇專案範本] 連結,如下圖所示。