有没有一种简单的方法可以通过线程安全的接口访问自定义的基于System.Configuration的配置数据,而不需要每个执行上下文加载/重新加载配置信息,这在计算上会很麻烦?
与Microsoft的.Net库文档中的大多数(所有?)其他类一样,System.Configuration类使用以下线程安全信息进行注释:
此类型的任何公共静态(在Visual Basic中为Shared)成员都是线程安全的.任何实例成员都不保证是线程安全的.
通过我的阅读,不能假定ConfigurationSection从ConfigurationManager.GetSection(string)和其他类似方法(例如OpenExeConfiguration(string exePath).GetSection(string))返回的对象是线程安全的,因此不应该被多个执行上下文使用.这禁止ConfigurationSection在单例中存储否则是线程安全的,因为虽然对节对象的访问可能是安全的,但对象本身的成员本身并不安全.
GetSection但是,多次调用可能需要重新解析配置文件并分配ConfigurationSection具有高开销的新实例,因为初始化后配置不太可能发生变化.此外,将配置数据复制到另一个已成为线程安全的对象似乎打败了首先使用内置配置包的主要好处之一(轻松访问经过类型转换和验证的配置信息,而无需太多样板码).
那么,是否有一种方法可以System.Configuration以线程安全的方式使用而无需过多地解析和分配配置节?ConfigurationSection即使您通过System.Configuration接口访问它,实现您自己也可以免除Microsoft提供的保证(如果是这样的话,当您需要访问基础ConfigurationSection索引器时,如何实现它是线程安全的?访问配置的数据)?
我(希望)设置我自己的设计的ConfigurationElementCollection,电子邮件作为键.怎么办?很难在网上找到.我如何能:
通过它迭代?
查看是否存在特定元素?
得到一个特定的元素?
...给出:
YourConfigElement config =
ConfigurationManager.GetSection("YourSectionName") as YourConfigElement;
Run Code Online (Sandbox Code Playgroud)
部分答案
1.
foreach (X x in config.XCollection)
<code here>
Run Code Online (Sandbox Code Playgroud)
2.用"替换此处的代码"代替
{
if (x.Y == needle)
{
hasIndeed = true;
break;
}
}
Run Code Online (Sandbox Code Playgroud)
3.用"替换此处的代码"代替
{ if (x.Y == needle)
cameUpWith = x;
break;
}
Run Code Online (Sandbox Code Playgroud)
微小的气味.
AppSettingsReader类与.Net 3.5中ConfigurationManager类的AppSettings成员之间是否存在实质性差异?
我正在构建一些遗留代码,而以前的开发人员使用AppSettingsReader.GetValue(),而我更倾向于使用ConfigurationManager.AppSettings.Get().
看看内部,AppSettingReader似乎更安全,但它的用法似乎稍微冗长.如果我正在检索的应用程序设置众所周知并且相当静态,那么使用AppSettingsReader是否有任何好处?
还有一个很大的问题和答案在这里,说明如何创建自定义配置部分,它能够解析以下形式进入.NET对象的配置:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="CustomConfigSection" type="ConfigTest.CustomConfigSection,ConfigTest" />
</configSections>
<CustomConfigSection>
<ConfigElements>
<ConfigElement key="Test1" />
<ConfigElement key="Test2" />
</ConfigElements>
</CustomConfigSection>
</configuration>
Run Code Online (Sandbox Code Playgroud)
我的问题是,有没有人知道如何创建没有ConfigElements元素的相同自定义配置部分?例如,一个将解析以下CustomConfigSection元素代替上面显示的元素:
<CustomConfigSection>
<ConfigElement key="Test1" />
<ConfigElement key="Test2" />
</CustomConfigSection>
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是,类型CustomConfigSection需要从ConfigurationSection和ConfigurationElementCollection继承,这当然在C#中是不可能的.我发现的另一种方法要求我实现IConfigurationSectionHandler,它从.Net v2开始不推荐使用.有谁知道如何达到预期的效果?谢谢.
当引用的.NET Framework 2.0程序集试图在IIS托管的WCF服务中执行以下代码行时,我收到错误:
错误信息:
不在独立的exe内部运行时必须指定exePath.
源代码:
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
Run Code Online (Sandbox Code Playgroud)
有没有人遇到过这个问题,他们知道如何解决这个问题吗?
编辑:我的问题是,从向后兼容.NET 2.0程序集的WCF服务打开配置文件(app.config和web.config)的最佳方法是什么?
我在使用 Visual Studio 2019 的 .NET 5.0 项目中收到此错误(在社区/学生版以及专业版上进行了尝试)。
“在命名空间‘System.Configuration’中找不到类型名称‘ConfigurationManager’。此类型已被转发...”
我发现的许多答案都说您需要添加“using System.Configuration;” 以及通过Project -> Add Reference添加程序集命名空间作为引用。这个答案对我不起作用,因为“项目”选项卡中没有“引用”文件夹或任何“添加引用”选项。参考管理器中也没有“程序集”选项卡。仅“项目”、“共享项目”、“COM”和“浏览”。
如何添加 System.Configuration 作为引用以便我可以使用 ConfigurationManager?
configurationmanager system.configuration asp.net-core visual-studio-2019 asp.net-core-5.0
我创建了一种动态添加SettingsProperty到.NET app.config文件的方法.这一切都很好,但是当我下次启动我的应用程序时,我只能看到设计器中创建的属性.如何加载属性运行时?
我创建的代码SettingsProperty如下所示:
internal void CreateProperty<T>(string propertyName)
{
string providerName = "LocalFileSettingsProvider";
System.Configuration.SettingsAttributeDictionary attributes = new SettingsAttributeDictionary();
System.Configuration.UserScopedSettingAttribute attr = new UserScopedSettingAttribute();
attributes.Add(attr.TypeId, attr);
System.Configuration.SettingsProperty prop;
SettingsProvider provider = ApplicationEnvironment.GlobalSettings.Providers[providerName];
prop = new System.Configuration.SettingsProperty(
propertyName,
typeof(T),
provider,
false,
default(T),
System.Configuration.SettingsSerializeAs.String,
attributes,
false,
false
);
ApplicationEnvironment.GlobalSettings.Properties.Add(prop);
ApplicationEnvironment.GlobalSettings.Reload();
}
Run Code Online (Sandbox Code Playgroud)
下次运行时,我要求设置属性,我找不到任何创建的属性.无论我是否致电ApplicationEnvironment.GlobalSettings.Reload();.
在使用第三方DLL时,我遇到以下异常:
不在独立的exe内部运行时必须指定exePath
具有以下痕迹
System.Configuration.ConfigurationManager.OpenExeConfigurationImpl(ConfigurationFileMap fileMap,Boolean isMachine,ConfigurationUserLevel userLevel,String exePath).
我找到的原因是它正在寻找app.config,我在web.config中提供了详细信息.我的问题是:为什么system.configuration会区分web.config和app.config?有什么想法吗?
所以我有一个ConfigurationSection/ConfigurationElementCollection,其配置如下:
<mimeFormats>
<add mimeFormat="text/html" />
</mimeFormats>
Run Code Online (Sandbox Code Playgroud)
以下是我处理mimeFormats的方法:
public class MimeFormatElement: ConfigurationElement
{
#region Constructors
/// <summary>
/// Predefines the valid properties and prepares
/// the property collection.
/// </summary>
static MimeFormatElement()
{
// Predefine properties here
_mimeFormat = new ConfigurationProperty(
"mimeFormat",
typeof(MimeFormat),
"*/*",
ConfigurationPropertyOptions.IsRequired
);
}
private static ConfigurationProperty _mimeFormat;
private static ConfigurationPropertyCollection _properties;
[ConfigurationProperty("mimeFormat", IsRequired = true)]
public MimeFormat MimeFormat
{
get { return (MimeFormat)base[_mimeFormat]; }
}
}
public class MimeFormat
{
public string Format
{
get
{
return …Run Code Online (Sandbox Code Playgroud) c# system.configuration configurationsection .net-4.0 type-conversion
我有这个使用 WMIC 获取 Windows 版本的代码段
(from x in new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem").Get().OfType<ManagementObject>()
select x.GetPropertyValue("Version")).FirstOrDefault().ToString();
Run Code Online (Sandbox Code Playgroud)
由于 WMI 现在被认为是遗留系统,并且System.Configuration 的 .NET Core 实现需要最新的 .NET Framework,因此似乎需要使用 MMI 检索此信息。
令人惊讶的是,每个需要 WMI 访问权限的 Windows 问题都有类似上面的代码片段。看起来 MMI 的采用率还没有那么高,而且要找到一个好的 MMI 示例并不容易。
我如何使用Microsoft.Management.Infrastructure来完成上面的代码?
背景:
我有一个在 netcoreapp3.1 和 azure 版本 v3 中运行的 Azure 函数 C# 项目。这是 csproj 的一个片段...
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<AssemblyName>someproj</AssemblyName>
<RootNamespace>someproj</RootNamespace>
<Product>someproduct</Product>
<AzureFunctionsVersion>v3</AzureFunctionsVersion>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<RunPublishAfterBuild>true</RunPublishAfterBuild>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.ApplicationInsights" Version="2.15.0" />
<PackageReference Include="Microsoft.Applications.Telemetry.Server">
<Version>1.1.264</Version>
<NoWarn>NU1701</NoWarn>
</PackageReference>
<PackageReference Include="System.Configuration.ConfigurationManager" Version="5.0.0-preview.8.20407.11">
<NoWarn>NU1701</NoWarn>
</PackageReference>
</ItemGroup>
</Project>
Run Code Online (Sandbox Code Playgroud)
运行后异常:
'Could not load file or assembly 'System.Configuration.ConfigurationManager,
Version=5.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one
of its dependencies. The system cannot find the file specified.'
Run Code Online (Sandbox Code Playgroud)
怀疑原因:
问题是在通过 Visual Studios 构建 azure 函数时,它会创建以下文件夹结构。(原谅我可怕的插图)
bin/
| - Debug/
| | -bin …Run Code Online (Sandbox Code Playgroud) c# system.configuration asp.net-core azure-functions netcoreapp3.1
如果应用程序在.net 4.0上运行,那么获取.net 2.0 machine.config文件路径的最佳方法是什么?
一种方法是进行字符串操作和文件系统访问,用v2.0*替换v4.0*,
new ConfigurationFileMap().MachineConfigFilename;然后将其传递给ConfigurationManager.OpenMappedMachineConfiguration(new ConfigurationFileMap(<HERE>)).如果没有更好的可用性,我将采用这种解决方案.
我有这个声明,应该在我的配置中设置一个键的值:
ConfigurationManager.AppSettings["Volume"] = volumeNumSlider.Value.ToString();
Run Code Online (Sandbox Code Playgroud)
但是当我重新启动应用程序时,它不会保存该值.
这是我的app.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="Volume" value="7"/>
<add key="Keyval" value="Z"/>
</appSettings>
</configuration>
Run Code Online (Sandbox Code Playgroud) c# ×9
.net ×5
asp.net-core ×2
.net-3.5 ×1
.net-4.0 ×1
.net-core ×1
app-config ×1
asp.net ×1
type-safety ×1
wcf ×1
windows ×1
winforms ×1