如何自定义Service Manifest文件的创建?

时间:2016-04-21 00:48:00

标签: azure-service-fabric

当我向Service Fabric项目添加新Actor时,该服务会自动添加到我的 ApplicationManifest.xml ServiceManifest.xml 文件中,因为我们有 UpdateServiceFabricManifestEnabled 设置为true。对于某些项目,我们需要要求服务具有 PlacementConstraints ,以便将它们部署到适当的节点。

如何挂钩此流程以便我可以指定 PlacementConstraints ,而不必记住编辑任何清单文件?

1 个答案:

答案 0 :(得分:2)

The service manifest file gets automatically populated with the actor service types as part of the build. There's an MSBuild target that gets run after the built-in "Build" target which does this. You can tack on your own logic that gets run after this. In that logic, you can make any necessary changes to the service manifest file. Here's an example that ensures that placement constraints are added to all the service types in the service manifest file. It uses an inline MSBuild task but you could rewrite this to be contained in a compiled assembly or whatever you wanna do.

This sample should be placed at the end of the file in your Actor service project:

<UsingTask TaskName="EnsurePlacementConstraints" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll">
  <ParameterGroup>
    <ServiceManifestPath ParameterType="System.String" Required="true" />
  </ParameterGroup>
  <Task>
    <Reference Include="System.Xml" />
    <Reference Include="System.Xml.Linq" />
    <Using Namespace="System.Xml.Linq" />
    <Code Type="Fragment" Language="cs">
    <![CDATA[
const string FabricNamespace = "http://schemas.microsoft.com/2011/01/fabric";
XDocument serviceManifest = XDocument.Load(ServiceManifestPath);
IEnumerable<XElement> serviceTypes = serviceManifest.Root.Element(XName.Get("ServiceTypes", FabricNamespace)).Elements();
bool changesMade = false;
foreach (XElement serviceType in serviceTypes)
{
  XName placementConstraintsName = XName.Get("PlacementConstraints", FabricNamespace);
  if (serviceType.Element(placementConstraintsName) == null)
  {
    XElement placementConstraints = new XElement(placementConstraintsName);
    placementConstraints.Value = "(add your contraints here)";
    serviceType.AddFirst(placementConstraints);
    changesMade = true;
  }
}

if (changesMade)
{
    serviceManifest.Save(ServiceManifestPath);
}
    ]]>
    </Code>
  </Task>
</UsingTask>

<Target Name="EnsurePlacementConstraints" AfterTargets="Build">   
  <EnsurePlacementConstraints ServiceManifestPath="$(MSBuildThisFileDirectory)\PackageRoot\ServiceManifest.xml" />
</Target>