c# - app.config custom configuration - 'System.TypeInitializationException' -
i tried following instruction app.config custom configuration
however, got stuck. app.config code:
<?xml version="1.0" encoding="utf-8"?> <configuration> <configsections> <section name="configuration" type="dp.configuration, myassembly" /> </configsections> <startup> <supportedruntime version="v4.0" sku=".netframework,version=v4.5.2"/> </startup> <configuration inputloc="c:\input" /> </configuration>
here configuration class:
using system.configuration; namespace dp { public class configuration : configurationsection { private static readonly configuration settings = configurationmanager.getsection("configuration") configuration; public static configuration settings { { return settings; } } [configurationproperty("inputloc", isrequired = true)] [stringvalidator(invalidcharacters = " ~!@#$%^&*()[]{}/;’\"|", minlength = 1, maxlength = 256)] public string inputlocation { { return (string)this["inputloc"]; } set { this["inputloc"] = value; } } } }
and when call inputfolder = clientconfiguration.settings.inputlocation;
system.typeinitializationexception
.
error code:
an unhandled exception of type 'system.typeinitializationexception' occurred in dp.exe additional information: type initializer 'dp.configuration' threw exception. system.typeinitializationexception unhandled _hresult=-2146233036 _message=the type initializer 'dp.configuration' threw exception. hresult=-2146233036 istransient=false message=the type initializer 'dp.configuration' threw exception. source=dp typename=dp.configuration stacktrace: @ dp.configuration.get_settings() @ dp.program.main(string[] args) in c:\program.cs:line 1012 @ system.appdomain._nexecuteassembly(runtimeassembly assembly, string[] args) @ microsoft.visualstudio.hostingprocess.hostproc.runusersassembly() @ system.threading.executioncontext.runinternal(executioncontext executioncontext, contextcallback callback, object state, boolean preservesyncctx) @ system.threading.executioncontext.run(executioncontext executioncontext, contextcallback callback, object state, boolean preservesyncctx) @ system.threading.executioncontext.run(executioncontext executioncontext, contextcallback callback, object state) @ system.threading.threadhelper.threadstart() innerexception: system.configuration.configurationerrorsexception _hresult=-2146232062 _message=an error occurred creating configuration section handler configuration: not load file or assembly 'myassembly' or 1 of dependencies. system cannot find file specified. hresult=-2146232062 istransient=false message=an error occurred creating configuration section handler configuration: not load file or assembly 'myassembly' or 1 of dependencies. system cannot find file specified. (c:\dp.vshost.exe.config line 4) source=system.configuration baremessage=an error occurred creating configuration section handler configuration: not load file or assembly 'myassembly' or 1 of dependencies. system cannot find file specified. filename=c:\dp.vshost.exe.config line=4 stacktrace: @ system.configuration.baseconfigurationrecord.findandensurefactoryrecord(string configkey, boolean& isrootdeclaredhere) @ system.configuration.baseconfigurationrecord.getsectionrecursive(string configkey, boolean getlkg, boolean checkpermission, boolean getruntimeobject, boolean requestishere, object& result, object& resultruntimeobject) @ system.configuration.baseconfigurationrecord.getsection(string configkey) @ system.configuration.configurationmanager.getsection(string sectionname) @ dp.configuration..cctor() in c:\\configuration.cs:line 7 innerexception: system.io.filenotfoundexception _hresult=-2147024894 _message=could not load file or assembly 'myassembly' or 1 of dependencies. system cannot find file specified. hresult=-2147024894 istransient=false message=could not load file or assembly 'myassembly' or 1 of dependencies. system cannot find file specified. source=system.configuration filename=myassembly fusionlog==== pre-bind state information === log: displayname = myassembly (partial) wrn: partial binding information supplied assembly: wrn: assembly name: myassembly | domain id: 1 wrn: partial bind occurs when part of assembly display name provided. wrn: might result in binder loading incorrect assembly. wrn: recommended provide specified textual identity assembly, wrn: consists of simple name, version, culture, , public key token. wrn: see whitepaper http://go.microsoft.com/fwlink/?linkid=109270 more information , common solutions issue. log: appbase = file:///c://x64/debug/ log: initial privatepath = null calling assembly : system.configuration, version=4.0.0.0, culture=neutral, publickeytoken=b03f5f7f11d50a3a. === log: bind starts in default load context. log: using application configuration file: c:\dp.vshost.exe.config log: using host configuration file: log: using machine configuration file c:\windows\microsoft.net\framework64\v4.0.30319\config\machine.config. log: policy not being applied reference @ time (private, custom, partial, or location-based assembly bind). log: attempting download of new url file:///c://x64/debug/myassembly.dll. log: attempting download of new url file:///c://x64/debug/myassembly/myassembly.dll. log: attempting download of new url file:///c://x64/debug/myassembly.exe. log: attempting download of new url file:///c://x64/debug/myassembly/myassembly.exe. stacktrace: @ system.configuration.typeutil.gettypewithreflectionpermission(iinternalconfighost host, string typestring, boolean throwonerror) @ system.configuration.runtimeconfigurationrecord.runtimeconfigurationfactory.init(runtimeconfigurationrecord configrecord, factoryrecord factoryrecord) @ system.configuration.runtimeconfigurationrecord.runtimeconfigurationfactory.initwithrestrictedpermissions(runtimeconfigurationrecord configrecord, factoryrecord factoryrecord) @ system.configuration.runtimeconfigurationrecord.createsectionfactory(factoryrecord factoryrecord) @ system.configuration.baseconfigurationrecord.findandensurefactoryrecord(string configkey, boolean& isrootdeclaredhere) innerexception:
see question: configurationsection-stringvalidator-failing
it looks need add default value attribute in configurationproperty attribute, ie.
[configurationproperty("inputloc", isrequired = true, defaultvalue="something")]
Comments
Post a Comment