2016-11-23 66 views
2

我有一个正在运行的程序,我继承了,现在作为服务工作。所有服务都是在另一个类中调用一个方法,所以我试图编写一个我可以运行的winform项目,这样我就可以处理它了。我复制并粘贴了app.config文件并将项目名称更改为servicetester,以将其更改为新的非服务项目。我还从服务中复制了其他代码中的各种代码,并且它们都构建完成了。我收到以下消息时,我尝试并运行该项目使用行“无法加载类型”从app.config

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="[email protected]" 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> 

还有就是不要 ℃的路径:\用户\ MyUserName输入\应用程序数据\本地\ MyCompany的\ MYAPP.exe_Url_longnastyhash9982749827349879 \ 1.0.0.0 \ user.config 或尝试删除

任何其他地方沿着线我读过

我已经清理并重建一切

的winform应用程序是相同的版本作为服务在.NET框架 而言这些是最常见的事情,谷歌称,它可以。任何其他想法? THANKs

+0

无论Configuration.CustomConfigurationSection是,它不是访问servicetester项目。您可能缺少一个包含,或者您可能需要将该类复制到servicetester项目。 – Kevin

+0

我已经复制了,并(希望)所有必要的。 – Glyn

+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"]; 
+0

给了一个去,但它带回相同的错误信息 – Glyn

+0

使用自定义部分,因为我显示它将工作。尝试编辑你的appconfig。 –

相关问题