简体   繁体   English

如何从应用程序级别使用Azure中的服务配置?

[英]How can I use service configuration in Azure from application level?

I have some cloud service implemented in C#. 我在C#中实现了一些云服务。 Whole solution consists of Service Project with some settings, separate projects for multiple roles + one separate project with main logic of the application. 整个解决方案包括具有某些设置的Service Project,用于多个角色的单独项目+一个具有应用程序主要逻辑的单独项目。 Now, I need to distinguish application url depending on environment it is run on. 现在,我需要根据运行它的环境来区分应用程序URL。 I know (suspect might be better choice of words) I can modify ServiceConfiguration.<Cloud|Local>.cscfg file by adding <Setting name="domain" value="example.com" /> 我知道(怀疑是更好的选择),我可以通过添加<Setting name="domain" value="example.com" />来修改ServiceConfiguration.<Cloud|Local>.cscfg文件

Now, the question I'd like to ask is: how can access this configuration value from source code? 现在,我想问的问题是:如何从源代码访问此配置值?

Yes, it is certainly possible. 是的,肯定有可能。 So if you have a cloud project A and that has a setting <Setting name="domain" value="exampleA.com" /> and then you have cloud project B that has a setting <Setting name="domain" value="exampleB.com" /> and then you have main project which is used in both cloud projects A and B. 因此,如果您有一个云项目A且设置为<Setting name="domain" value="exampleA.com" /> ,则您有一个云项目B的设置为<Setting name="domain" value="exampleB.com" /> ,然后您拥有在云项目A和B中都使用的主项目。

When you run project A and read the setting domain from your main project you will get exampleA.com and when you run project B and read the same setting, you will get exampleB.com . 当您运行项目A并从主项目中读取设置domain ,您将获得exampleA.com而当您运行项目B并读取相同的设置时,您将获得exampleB.com

I added to the ServiceConfigurationCloud.cscfg and the ServiceConfigurationLocal.cscfg 我添加到ServiceConfigurationCloud.cscfg和ServiceConfigurationLocal.cscfg

<ConfigurationSettings>
<Setting name="DataBaseConnectionString" value="Data Source=MYNAME.database.windows.net;Initial Catalog=MYDATABASE;Persist Security Info=True;User ID=USERNAME;Password=PASSWORD"/>
</ConfigurationSettings>

Then I added to the ServiceDefinition.csdef file. 然后,我将其添加到ServiceDefinition.csdef文件中。

<ConfigurationSettings>
  <Setting name="Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString" />
  <Setting name="DataBaseConnectionString"/>
</ConfigurationSettings>

Within the code I was able to read from the file by using 在代码中,我能够通过使用从文件中读取

string DBCO = RoleEnvironment.GetConfigurationSettingValue("DataBaseConnectionString");

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

相关问题 如何从 .Net Frame Asp.Net 应用程序中的 Azure App Service 配置获取连接字符串? - How do I get a connection string from Azure App Service configuration in a .Net Frame Asp.Net application? 如何在C#控制台应用程序中使用配置替换 - How Can I Use Configuration Replacements for C# Console Application 如何在一个项目中使用多个应用程序配置文件? - How can I use several Application Configuration Files in one project? 如何在Web App Service中使用“Azure文件存储”? - How can I use “Azure File Storage” with Web App Service? 我可以使用来自 Web 应用程序的 Web API 作为服务引用吗? - Can I use a web api from a web application as a service reference? 如何使用 Service Principal/Managed Identity 访问 Azure App Configuration? - How to use Service Principal/Managed Identity to access Azure App Configuration? Azure 应用服务中的应用程序设置配置 - Application setting configuration in Azure App Service 如何检索和更新服务的Azure配置文件? - How do I retrieve and update an azure configuration file for a service? 如何从 Azure web 应用程序启动 Office 产品 - How can I launch a Office product from Azure web application 用户如何加密服务应用程序使用的配置文件? - How can a user encrypt a configuration file to be used by a service application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM