"无法加载类型"来自app.config

时间:2016-11-23 13:10:01

标签: c# app-config

我有一个功能正常的程序,我继承了该程序目前的服务。所有这些服务都是在另一个类中调用一个方法,所以我试着编写一个我可以运行的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:\用户\ MyUserName输入\应用程序数据\本地\ MyCompany的\ MYAPP.exe_Url_longnastyhash9982749827349879 \ 1.0.0.0 \ user.config 或我读过的任何其他地方尝试删除

我已经清理并重建了一切

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

1 个答案:

答案 0 :(得分:0)

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

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

这里有示例配置,包含示例customAppSetting和sectionGroup

App.Config中

    <?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"];