Sunday, 15 August 2010

app config - Custom configuration properties - Entry has already been added -


I am developing a window service that reads the information from app.cconfig on start-up so that we get internal

MyConfigurationSection {[ConfigurationProperty ("Thread"):

I have created some custom configuration sections and elements as follows (implementation skipped):

] [Configuration Collection (typef (mathth collection), AddItemName = "addThread") & gt; Public {MyThreadCollection threads receive; }} Public class MyThreadCollection {safe override zero CreateNewElement (); Safe Override Object GetElementKey (Configuration Element Element); } Public class MyThreadElement {{ConfigurationProperty ("active", DefaultValue = true, IsRequired = false) & gt; Public Bull Active {get; Set; } [Configuration property ("Batchies", DefaultValue = 10, IiReaded = Fotal) & gt; Public at batch size {get; Set; } [Configuration property ("system", isiariad = true) & gt; Public string system {get; Set; } [Configuration Property ("Department", Israquired = true) & gt; Receive public string section { Set; } [Configuration property ("connection", icedred = true) & gt; Public MyThreadConnectionElement Connection {get; Set; }} Public Category MyThreadConnectionElement {{ConfigurationProperty ("Server", IsRequired = true) & gt; Public string server {received; Set; } [Configuration property ("database", ISRAQQ = true) & gt; Public string database {received; Set; } [Configuration property ("timeout", default value = 15, isiired = fotl) & gt; Public Int Timeout {Received; Set; }}

Then I add some element in app.config as follows:

   & Lt; / AddThread & gt; & Lt; AddThread Activated = "True" Batchism = "30" System = "MySystem 2" section = "Section 2" & gt; & Lt; ConnectionString Server = "MyServer" database = "Database2" /> & Lt; / AddThread & gt; & Lt; / Threads & gt; & Lt; / ConfigurationSection & gt;  

Everything works - the configuration is read, threads are created, and processes run.

The problem is, I should have both the threads same system name / value - both MySystem - but when I do this and program Run, I get a entry The 'MySystem' has already been added. Exceptions.

I thought it might be because the property has to be clearly configured in order to allow duplicates, but I do not know how and when I configuration property The class which can allow, except for IsKey , but it does not look like the reply from its description, and did not solve the problem by trying, am I here on the right track?

The property was initially named name and I think that this name may be rarely name Is considered as a unique identifier, so I changed it to system but it did not change anything.

I tried & lt; Clear />

Do I need to add another hierarchy in the configuration section - Configuration -> Department -> Config -> Thread instead of Thread? I would not like to take this view.

Thanks for any and all input.

I really got the problem and solution some time ago, but forgot to post the answer; Thanks @ to remind me

When you apply the configuration element code class, then GetElementKey (Configuration Element) method can be overridden. Immediately to understand what the method is, I override it and only returned the value of the system property, and, because more than one configuration element had the same system name, the technical In the form they had only one key, this is the reason for the error.

The solution for me was to return system and department values ​​to system.department as a result of unique keys.


No comments:

Post a Comment