您在哪里设置和访问服务结构的每个环境的运行时配置参数? [英] Where do you set and access run-time configuration parameters per environment for service fabric?

查看:32
本文介绍了您在哪里设置和访问服务结构的每个环境的运行时配置参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于本地和云两种环境,我将如何为Sql数据库、存储帐户等资源设置自定义设置或参数...理想情况下,它会在代码中调用一个参数名称来表示,指向一个DbContext对于特定的数据库,本地或云环境的配置是不同的.谢谢你.

For two environments, local and cloud, how would I set up custom settings or parameters for resources such as Sql databases, storage accounts, etc... Ideally it would be one parameter name called in code to say, point a DbContext towards a particular database, that in configurations for either a local or cloud environment be different. Thank you.

推荐答案

为了拥有用于在本地和云中运行 Service Fabric 的每个环境变量,您必须执行以下操作:

In order to have per environment variables for running Service Fabric locally and in the cloud this is what you must do:

  1. 将您的自定义配置部分和参数添加到 Service/Actor 项目的 Settings.xml 文件(位于项目根目录中的 PackageRootConfigSettings.xml).将参数留空,因为我们将在每个环境的其他地方设置这些参数.这是一个示例.

<?xml version="1.0" encoding="utf-8" ?>
<Settings 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">
<!-- Add your custom configuration sections and parameters here -->
    <Section Name="UserDatabase">
        <Parameter Name="UserDatabaseConnectionString" Value="" />
    </Section>
</Settings>

  1. 在 Service Fabric 项目的 ApplicationManifest.xml 文件中,每个包含的项目都有 元素.在其下方将是一个 元素,我们将在其中声明我们的配置的哪些值将被 Service Fabric 项目中 ApplicationParameters 下的本地和云 xml 文件中每个环境设置的值所取代.在同一个 ApplicationManifest.xml 文件中,您需要添加将出现在本地和云 xml 文件中的参数,否则它们将在构建时被覆盖.
  1. In the ApplicationManifest.xml file of your Service Fabric project, there will be <ServiceManifestImport> elements for each of your included projects. Underneath that will be a <ConfigOverrides> element where we will declare what values for our configs will be supplanted by values set per environment in the local and cloud xml files underneath ApplicationParameters in our Service Fabric project. In that same ApplicationManifest.xml file, you'll need to add the parameter that will be present in the local and cloud xml files, otherwise they'll be overwritten upon build.

继续上面的例子,这就是它的设置方式.

Continuing with the example above, this is how it would be set.

<Parameters>
    <Parameter Name="ServiceName_InstanceCount" DefaultValue="-1" />
    <Parameter Name="UserDatabaseConnectionString" DefaultValue="" />
</Parameters>
<ConfigOverrides>
    <ConfigOverride Name="Config">
        <Settings>
            <Section Name="UserDatabase">
                <Parameter Name="UserDatabaseConnectionString" Value="[UserDatabaseConnectionString]" />
            </Section>
        </Settings>
    </ConfigOverride>
</ConfigOverrides>

  1. 在 Service Fabric 项目中 ApplicationParameters 下的 local.xml 和 cloud.xml 文件中,您将像这样指定环境特定变量.

<?xml version="1.0" encoding="utf-8"?>
<Application xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Name="fabric:/AppFabricName.ServiceFabric" xmlns="http://schemas.microsoft.com/2011/01/fabric">
    <Parameters>
        <Parameter Name="ServiceName_InstanceCount" Value="1" />
        <Parameter Name="UserDatabaseConnectionString" Value="Server=(localdb)MsSqlLocalDb;Database=Users;User=ReadOnlyUser;Password=XXXXX;" />
    </Parameters>
</Application>

  1. 最后,在您的服务/Actor 中,您可以像这样访问这些每个环境的配置变量.

var configurationPackage = Context.CodePackageActivationContext.GetConfigurationPackageObject("Config");

var connectionStringParameter = configurationPackage.Settings.Sections["UserDatabase"].Parameters["UserDatabaseConnectionString"];

这篇关于您在哪里设置和访问服务结构的每个环境的运行时配置参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆