使用 .NET Framework 4.8 作为网站的 API 应用程序。
//web config
<add key="id" value="123" />
//code inside main project solution, this works
var Id = ConfigurationManager.AppSettings["id"];
//code inside extra class library project, this is NULL
var Id = ConfigurationManager.AppSettings["id"];
我们正在发布到 AppServices,但我在访问变量时遇到一些问题。
所以我们的项目有一个web.config文件。我知道我可以在此处为本地开发人员设置变量,然后将每个变量的相同名称添加到应用程序服务的配置部分,这将覆盖本地的应用程序服务,这很好并且工作正常。
在我们的解决方案中,我们有许多额外的类库项目,它们是整个解决方案的一部分,但不是实际主项目本身的一部分。
访问这些类库项目之一时,我无法从原始项目中的 web.config 访问变量。 (当前该值是在代码中设置的,但我们希望将其更改为配置设置,以便我们可以从应用程序服务配置中使用它) (我知道我们可以使用 keyVault,这可以解决访问问题,但我们不使用这个方法)
我可以将变量添加到类库项目的 app.config 中,但是它会在应用程序服务配置设置中被拾取吗?我假设这只适用于 web.config 文件
我尝试了很多方法,我能够使用 web.config 文件的硬编码位置(来自类库项目内部)来:
var path = @"C:\Users\PathNameOnLocalMachine";
string file = Path.GetFileName(path);
System.Configuration.Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
string id = config.AppSettings.Settings["id"].Value;
这有效,但当我们转到应用程序服务时我无法使用它我想过尝试在应用程序服务上找到 web.config 的位置并使用它,但谷歌拒绝了。
我要问的问题是:我是否可以在应用程序服务中的配置设置的应用程序设置中设置一个变量,并且在网络配置文件中没有提到它,但我可以在类库解决方案中使用它代码。 (这将在本地开发上返回 null)但在应用程序服务上显示正确的值。
我已经尝试过了,但没有成功,仍然返回 null。看来 var 必须在 appService 的 web.config 和配置设置中提及。
还有其他解决方法吗?或者有什么想法吗?谢谢您的建议
你有:
[ Main Project (web.config) ] ---> [ Class Library Project ]
|
Azure App Service Config
在主项目中,访问AppSettings按预期工作:
var Id = ConfigurationManager.AppSettings["id"]; // Works in Main Project
但是,相同的代码在类库项目中返回
null
:
var Id = ConfigurationManager.AppSettings["id"]; // Returns NULL in Class Library
因此,您需要访问类库中的
id
设置,而无需将其复制到库的 app.config
中,并确保它在本地和部署到 Azure 应用服务时都能正常工作。尝试在主项目中创建一个配置访问层。该层将作为访问配置设置的单点事实。这样,您将确保应用程序的所有部分(包括类库)通过一致的接口检索配置值。
public static class ConfigurationHelper
{
public static string GetSetting(string key)
{
return ConfigurationManager.AppSettings[key];
}
}
然后,您可以使用依赖项注入 (DI)(比静态访问模式更灵活且可测试的方法)为您的类库项目提供配置值。通过主项目中的
id
访问
ConfigurationHelper
设置:var Id = MainProject.ConfigurationHelper.GetSetting("id");
确保类库项目引用主项目。如果使用依赖项注入,请根据需要注入配置设置。 (DI) 意味着类库不应该直接引用主项目。相反,它应该定义一个主项目实现并注册到 DI 的接口。
举个例子,在你的类库项目中,定义一个表示配置访问机制的接口:
// In your Class Library project
public interface IConfigurationService
{
string GetSetting(string key);
}
在主项目中实现此接口,您可以在其中访问
ConfigurationManager
或您正在使用的任何其他配置源:
// In your Main Project
public class ConfigurationService : IConfigurationService
{
public string GetSetting(string key)
{
return ConfigurationManager.AppSettings[key]; // Or use IConfiguration for .NET Core/5+
}
}
在主项目的启动类或配置服务的任何位置,将
ConfigurationService
注册到 DI 容器:
// In your Startup.cs or Program.cs (for .NET Core/5+)
public void ConfigureServices(IServiceCollection services)
{
// Other service registrations
services.AddSingleton<IConfigurationService, ConfigurationService>();
}
在类库中,需要访问配置设置的地方,注入
IConfigurationService
:
// In your Class Library project
public class SomeClass
{
private readonly IConfigurationService _configService;
public SomeClass(IConfigurationService configService)
{
_configService = configService;
}
public void DoSomething()
{
var id = _configService.GetSetting("id");
// Use the id as needed
}
}
确保在类库中实例化需要
IConfigurationService
的类时,通过尊重 DI 的机制来实现(例如,使用如上所示的构造函数注入)。
如果您使用 ASP.NET Core 或 .NET 5/6+,配置系统有点不同,您可以将IConfiguration
直接注入到您的服务实现中:
public class ConfigurationService : IConfigurationService
{
private readonly IConfiguration _configuration;
public ConfigurationService(IConfiguration configuration)
{
_configuration = configuration;
}
public string GetSetting(string key)
{
return _configuration[key]; // Accessing configuration directly
}
}
服务的注册和类库中
IConfigurationService
的使用保持不变。
在 Azure 应用服务中,在配置部分中添加的设置会在运行时覆盖 web.config 中应用程序的设置。
这意味着如果在 Azure 应用服务配置中指定了设置,则无需复制 web.config 中的设置。
部署到 Azure 应用服务时,请确保在“配置”部分中添加
id
设置。该设置将自动覆盖
web.config
的 id
设置,无需在主项目的 web.config
文件中指定。