gpt4 book ai didi

c# - 来自 app.config 的 "Could not load type"

转载 作者:太空宇宙 更新时间:2023-11-03 15:11:58 24 4
gpt4 key购买 nike

我继承了一个功能正常的程序,目前它作为服务运行。该服务所做的只是调用另一个类中的方法,因此我正在尝试编写一个可以运行的 winform 项目,以便我可以对其进行处理。我复制并粘贴了 app.config 文件并将项目名称更改为 servicetester,更改为新的非服务项目。我还从服务中复制了各种代码,然后全部构建。当我尝试使用该行运行项目时,我收到以下消息

var connectionManagerDataSection = ConfigurationManager.GetSection("CustomConfigurationSection") as clsCustomConfigurationSection;

留言

An error occurred creating the configuration section handler for CustomConfigurationSection: Could not load type 'servicetester.Configuration.CustomConfigurationSect
ion' from assembly 'servicetester'. (D:\\thepath\\servicetester\\bin\\Debug\\servicetester.vshost.exe.Config line 4)"

内部异常

"System.TypeLoadException: Could not load type 'servicetester.Configuration.CustomConfigurationSection' from assembly 'servicetester'.
at System.Configuration.TypeUtil.GetTypeWithReflectionPermission(IInternalConfigHost host, String typeString, Boolean throwOnError)
at System.Configuration.RuntimeConfigurationRecord.RuntimeConfigurationFactory.Init(RuntimeConfigurationRecord configRecord, FactoryRecord factoryRecord)
at System.Configuration.RuntimeConfigurationRecord.RuntimeConfigurationFactory.InitWithRestrictedPermissions(RuntimeConfigurationRecord configRecord, FactoryRecord factoryRecord)
at System.Configuration.RuntimeConfigurationRecord.CreateSectionFactory(FactoryRecord factoryRecord)
at System.Configuration.BaseConfigurationRecord.FindAndEnsureFactoryRecord(String configKey, Boolean& isRootDeclaredHere)

这是app.config,除了项目名称外,与原始服务项目相同

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="CustomConfigurationSection" type="servicetester.Configuration.CustomConfigurationSection, servicetester" />
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="Proj.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<CustomConfigurationSection>
<EwsConfiguration username="a@b.com" password="letmein" version="2" emailStoreLocation="\\serverpath\" url="https://emailplace" subscriptionConnectionLifetime="15" debug="true"/>
</CustomConfigurationSection>
<applicationSettings>
<Proj.Properties.Settings>
<setting name="Proj_Email_Log" serializeAs="String">
<value>http://path</value>
</setting>
</Proj.Properties.Settings>
</applicationSettings>
</configuration>

没有路径C:\Users\MYUSERNAME\AppData\Local\MYCOMPANY\MYAPP.exe_Url_longnastyhash9982749827349879\1.0.0.0\user.config或我读过的任何其他地方尝试删除

我已经清理并重建了一切

winform 应用程序与服务在.NET 框架方面的版本相同这些是谷歌所说的最常见的事情。还有其他想法吗?谢谢

最佳答案

您需要分区组和自定义分区的名称。

var confSection = (CustomSection)System.Configuration.ConfigurationManager.GetSection("MCustomConfigurationSection/applicationSettings");

这里有带有示例 customAppSetting 和 sectionGroup 的示例配置

应用配置

    <?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="CustomConfigurationSection">
<section name="customAppSettings" type="System.Configuration.AppSettingsSection, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</sectionGroup>
</configSections>
<customAppSettingsGroup>
<customAppSettings>
<add key="Key1" value="val1"/>
<add key="Key2" value="val2"/>
</customAppSettings>
</customAppSettingsGroup>
</configuration>

用法:

var section = ConfigurationManager.GetSection("CustomConfigurationSection/customAppSettings") as NameValueCollection;
var val1 = section["Key1"];
var val2 = section["Key2"];

关于c# - 来自 app.config 的 "Could not load type",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40765450/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com