Dynamic Servicefabric Settings and Overrides

为君一笑 提交于 2019-12-06 15:50:46

There are many ways to configure a service fabric application, and each approach will bring you to a different challenges.

SF team recommend the approach in the docs, because you can have a better version control of configurations, and makes harder to commit mistakes, as it is explicitly declared in a file, I've used a few different approaches because of limitations like yours, the follwoing approach might solve your problem:

Configure like the original approach, but with complex types stored as JSON values: it is the closest solution to the recommended design and you still can keep control of the configuration versions on source control.

It would be something like:

Settings.xml:

<?xml version="1.0" encoding="utf-8" ?>
<Settings xmlns... namespaces here...>
  <Section Name="AuthorizationOptions">
    <Parameter Name="Policies"/>
  </Section>
</Settings>

ApplicationManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<ApplicationManifest ApplicationTypeName="ServiceFabric.ExampleType" ApplicationTypeVersion="1.0.0" xmlns:...namespaces....>
  <Parameters>
    <Parameter Name="Service.Example_ASPNETCORE_ENVIRONMENT" DefaultValue="" />
    <Parameter Name="Service.Example_InstanceCount" DefaultValue="-1" />
    <Parameter Name="Service.Example_AuthorizationOptions_Policies" DefaultValue="[]" />
  </Parameters>
  <ServiceManifestImport>
  <ServiceManifestImport>
    <ServiceManifestRef ServiceManifestName="Service.ExamplePkg" ServiceManifestVersion="1.0.0" />
    <ConfigOverrides>
      <ConfigOverride Name="Config">
        <Settings>
          <Section Name="AuthorizationOptions">
            <Parameter Name="Policies" Value="[Service.Example_AuthorizationOptions_Policies]" />
          </Section>
        </Settings>
      </ConfigOverride>
    </ConfigOverrides>
    <EnvironmentOverrides CodePackageRef="code">
      <EnvironmentVariable Name="ASPNETCORE_ENVIRONMENT" Value="[Service.Example_ASPNETCORE_ENVIRONMENT]" />
    </EnvironmentOverrides>
  </ServiceManifestImport>
  <DefaultServices>
    <Service Name="Service.Example" ServicePackageActivationMode="ExclusiveProcess">
      <StatelessService ServiceTypeName="Service.ExampleType" InstanceCount="[Service.Example_InstanceCount]">
        <SingletonPartition />
      </StatelessService>
    </Service>
  </DefaultServices>
</ApplicationManifest>

ApplicationParameters.xml

<?xml version="1.0" encoding="utf-8"?>
<Application Name="fabric:/ServiceFabric.Example" xmlns:...namespaces....>
  <Parameters>
    <Parameter Name="Service.Example_ASPNETCORE_ENVIRONMENT" Value="Development" />
    <Parameter Name="Service.Example_InstanceCount" Value="-1" />
    <Parameter Name="Service.Example_AuthorizationOptions_Policies" Value="[{'Name': 'User','Groups': ['Domain Users']}, {'Name': 'Admin','Groups': ['Administrators']}]" />
  </Parameters>
</Application>

In your service code:

public class Policy
{
    public string Name { get; set; }
    public string[] Groups { get; set; }
}


var settings = this.Context.CodePackageActivationContext.GetConfigurationPackageObject("Config").Settings;
var authOptions = settings.Sections["AuthorizationOptions"].Parameters["Policies"].Value;
var obj = JsonConvert.DeserializeObject<Policy[]>(authOptions);

You could go a level further and store the entire AuthorizationOptions as a JSON, but like said previously, more generic it become, easier will be to commit mistakes and harder to find configuration issues.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!