简体   繁体   English

用于获取控制台、Web 应用程序和 Service Fabric 微服务的配置参数的共享函数

[英]Shared function to get configuration parameter for console, web app and Service Fabric microservice

I want to have a shared function that returns the application setting of a parameter:我想要一个返回参数的应用程序设置的共享函数:

public static string GetAppSettings(string key)
{
  string sfAppName = Environment.GetEnvironmentVariable("Fabric_ApplicationName");
     if (string.IsNullOrEmpty(sfAppName)) // we are NOT in SF 
     {
         return ConfigurationManager.AppSettings[key];
     }
     else
     {
         ConfigurationPackage configurationPackage = FabricRuntime.GetActivationContext().GetConfigurationPackageObject("Config");
         return configurationPackage.Settings.Sections["appSettings"].Parameters[key].Value;
      }
}

So that this function gets the parameter from appSettings.config if the calling code is running in a standard web app, and from Local.1Node.xml if the calling code is running as a Service Fabric microservice.因此,如果调用代码在标准 Web 应用中运行,则此函数从 appSettings.config 获取参数,如果调用代码作为 Service Fabric 微服务运行,则从 Local.1Node.xml 获取参数。

When I put this function outside of the MicroServices "folder" (in the Shared project) I can't import当我将此功能放在微服务“文件夹”之外(在共享项目中)时,我无法导入

using System.Fabric;

Is there a way a way to do this?有没有办法做到这一点? And is it a good practice?这是一个好习惯吗?

Sure you can, in your shared project, add a reference to the Microsoft.ServiceFabric Nuget package.当然,您可以在共享项目中添加对Microsoft.ServiceFabric Nuget包的引用。

Install-Package Microsoft.ServiceFabric or dotnet add package Microsoft.ServiceFabric Install-Package Microsoft.ServiceFabricdotnet add package Microsoft.ServiceFabric

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM