对于本地和云环境,我应该如何设置自定义设置或参数,例如Sql数据库、存储账户等资源...理想情况下,只需要在代码中调用一个参数名称来指向特定的数据库,而这个参数在本地或云环境的配置中是不同的。谢谢。
为了在本地和云端运行Service Fabric时有各自的环境变量,您需要执行以下操作:
<?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>
<ServiceManifestImport>
元素。在该元素下面将会有一个<ConfigOverrides>
元素,在其中我们将声明哪些配置值将被本地和云xml文件中每个环境设置的值所替换。在同一ApplicationManifest.xml文件中,您需要添加在本地 和云xml文件中存在的参数,否则它们将在构建时被覆盖。继续上面的示例,以下是如何设置:
<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>
在 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>
var configurationPackage = Context.CodePackageActivationContext.GetConfigurationPackageObject("Config");
var connectionStringParameter = configurationPackage.Settings.Sections["UserDatabase"].Parameters["UserDatabaseConnectionString"];
ServiceManifestImport
(ApplicationManifest
的子级)上面定义参数,但是ConfigOverrides
必须放在它里面(ServiceManifestImport
的子级)。 - Mardoxx您可以像其他应用程序一样使用环境变量,这在服务织物中的客户可执行文件中也适用,不同于settings.xml
,因为它需要内置服务织物运行时。
在您的应用程序中,您可以使用Environment
类上的GetEnvironmentVariable
方法访问环境变量,就像任何其他.net应用程序一样:
var baseUri = Environment.GetEnvironmentVariable("SuperWebServiceBaseUri");
接下来我们需要设置一些默认的环境变量值,这是通过服务的ServiceManifest.xml
清单文件完成的。
<?xml version="1.0" encoding="utf-8" ?>
<ServiceManifest Name="MyServicePkg" Version="1.0.0" xmlns="http://schemas.microsoft.com/2011/01/fabric" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<!-- snip -->
<CodePackage Name="Code" Version="1.0.0">
<!-- snip -->
<EnvironmentVariables>
<EnvironmentVariable Name="SuperWebServiceBaseUri" Value="http://localhost:12345"/>
</EnvironmentVariables>
</CodePackage>
<!-- snip -->
</ServiceManifest>
这些环境变量可以在ApplicationManifest.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" ApplicationTypeName="ChileTargetType" ApplicationTypeVersion="1.0.0" xmlns="http://schemas.microsoft.com/2011/01/fabric">
<Parameters>
<!-- snip -->
</Parameters>
<ServiceManifestImport>
<ServiceManifestRef ServiceManifestName="MyServicePkg" ServiceManifestVersion="1.0.0" />
<EnvironmentOverrides CodePackageRef="Code">
<EnvironmentVariable Name="SuperWebServiceBaseUri" Value="https://the-real-live-super-base-uri.com/"/>
</EnvironmentOverrides>
</ServiceManifestImport>
<!-- snip -->
</ApplicationManifest>
然后,就像任何其他应用程序清单设置一样,可以使用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="MyService_SuperWebServiceBaseUri" Value="https://another-base-uri.com/" />
</Parameters>
</Application>
那么我们就需要更新 ApplicationManifest.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" ApplicationTypeName="ChileTargetType" ApplicationTypeVersion="1.0.0" xmlns="http://schemas.microsoft.com/2011/01/fabric">
<Parameters>
<Parameter Name="MyService_SuperWebServiceBaseUri" DefaultValue="https://the-real-live-super-base-uri.com/" />
</Parameters>
<ServiceManifestImport>
<ServiceManifestRef ServiceManifestName="MyServicePkg" ServiceManifestVersion="1.0.0" />
<EnvironmentOverrides CodePackageRef="Code">
<EnvironmentVariable Name="SuperWebServiceBaseUri" Value="[MyService_SuperWebServiceBaseUri]"/>
</EnvironmentOverrides>
</ServiceManifestImport>
<!-- snip -->
</ApplicationManifest>
必须这样做,因为服务旨在是自包含的。它们应该在链接到任何应用程序时默认运行。独立于应用程序的清单。因此,服务只能依赖于参数,在其自己的配置中至少是预定义的。
然后,这些预设可以被应用程序覆盖。这是唯一通用的方法。