英文:
Access User Provided Service variables from PCF using .NET core
问题
我在PCF上运行着一个.NET Core WebAPI,创建了一个用户提供的服务(CUPS),其中包含了我的数据库凭据。
cf cups my-proj-db -p '{ "username":"dba", "password":"dbapass", "server":"whateverserver:1433", "database":"schools"}'
当我登录PCF并转到“Settings->Environment Variables->View Env Vars”时,我可以在“system_env_json”下看到用户名、数据库等信息:
"system_env_json": {
"VCAP_SERVICES": {
"user-provided": [...]
}
}
现在我想要检索数据库的值,所以在我的.NET代码中,我这样做:
Environment.GetEnvironmentVariable("vcap.services.my-proj-db.credentials.server", EnvironmentVariableTarget.Process)
我还尝试过 EnvironmentVariableTarget.Machine & EnvironmentVariableTarget.User
,但是值总是为空。我知道在Java中可以这样检索它,但在.NET Core中应该怎么做呢?
另外注意,如果我直接在PCF上添加一个自定义变量,比如:
cf set-env my-app-name ENV_VAR_NAME MyFunkyName
我可以通过以下方式获取该值:
Environment.GetEnvironmentVariable("ENV_VAR_NAME", EnvironmentVariableTarget.Process)
感谢帮助!
英文:
I have a .NET core webapi running on PCF, Ive Create a user-provided service (cups) that contain my database credentials
cf cups my-proj-db -p '{"username":"dba", "password":"dbapass", "server":"whateverserver:1433", "database":"schools"}'
when I log onto PCF and go to
> Settings->Environment Variables->View Env Vars
I am able to see the username, db, etc under
"system_env_json": {
"VCAP_SERVICES": {
"user-provided": [...
Now i want to retrieve the DB values so in my .NET code I do
Environment.GetEnvironmentVariable("vcap.services.my-proj-db.credentials.server",EnvironmentVariableTarget.Process)
I also tired EnvironmentVariableTarget.Machine & EnvironmentVariableTarget.User
but the value is always null, I know in Java this is how they retrieve it, but how do i do it in .NET core?
Just a side note, if i add a costume variable directly to PCF like
cf set-env my-app-name ENV_VAR_NAME MyFunkyName
I am able to get the value by
Environment.GetEnvironmentVariable("ENV_VAR_NAME", EnvironmentVariableTarget.Process)
Appreciate the help!!
答案1
得分: 0
最终,使用Steeltoe扩展成功使其工作。以下是所需的全部步骤。
安装以下NuGet软件包:
Steeltoe.Common.Hosting
Steeltoe.Extensions.Configuration.CloudFoundryCore
Steeltoe.Extensions.Logging.DynamicSerilogCore
Steeltoe.Management.CloudFoundryCore
然后是Startup.cs文件:
public void ConfigureServices(IServiceCollection services)
{
services.ConfigureCloudFoundryOptions(Configuration);
}
接着是Program.cs文件:
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseCloudHosting() // 启用监听环境提供的端口
.AddCloudFoundryConfiguration() // 将Cloud Foundry环境变量添加为配置源
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
然后,在任何要访问这些值的类中,你需要执行以下步骤:
using Microsoft.Extensions.Options;
using Steeltoe.Extensions.Configuration.CloudFoundry;
public class EnvironmentManager
{
private CloudFoundryApplicationOptions _appOptions { get; set; }
private CloudFoundryServicesOptions _serviceOptions { get; set; }
public EnvironmentManager(IOptions<CloudFoundryApplicationOptions> appOptions, IOptions<CloudFoundryServicesOptions> serviceOptions)
{
_appOptions = appOptions.Value;
_serviceOptions = serviceOptions.Value;
}
}
随后,无论何处,你都可以通过以下方式访问这些值:
var value = _serviceOptions.Services["user-provided"]
.First(q => q.Name.Equals("my-proj-db"))
.Credentials["username"].Value;
英文:
so finally, was able to get this to work using Steeltoe extension. Here is what all is required.
install the following nuget packages
Steeltoe.Common.Hosting
Steeltoe.Extensions.Configuration.CloudFoundryCore
Steeltoe.Extensions.Logging.DynamicSerilogCore
Steeltoe.Management.CloudFoundryCore
then Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.ConfigureCloudFoundryOptions(Configuration);
}
then in program.cs
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseCloudHosting() //Enable listening on a Env provided port
.AddCloudFoundryConfiguration() //Add cloud-foundry environment variables as a configuration source
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
then in which ever class you want to access the values you have to do following
using Microsoft.Extensions.Options;
using Steeltoe.Extensions.Configuration.CloudFoundry;
public class EnvironmentManager
{
private CloudFoundryApplicationOptions _appOptions { get; set; }
private CloudFoundryServicesOptions _serviceOptions { get; set; }
public EnvironmentManager(IOptions<CloudFoundryApplicationOptions> appOptions, IOptions<CloudFoundryServicesOptions> serviceOptions)
{
_appOptions = appOptions.Value;
_serviceOptions = serviceOptions.Value;
}
you can then wherever you want to access the values you could just do
var value = _serviceOptions.Services["user-provided"]
.First(q => q.Name.Equals("my-proj-db"))
.Credentials["username"].Value;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论