thanks heaps Sloan, i knew there must have been something i was missing.
No problem.
Keep in mind my sample is not a direct KB on "how to implement a custom
handler".
I actually have written one to use with my smtp settings thingy.
//Quote from article//
You can download the code HERE. (Right-Click and "Save As" works best)
//End Quote
After you get the code, find this class:
EmailSmtpSettingsHandler : IConfigurationSectionHandler
and throw a break point in there.
You're basically taking xml, and writing xpath statements to populate some
other object.
Aka, you gotta write the glue code which takes XML and converts it to a real
object.
Now, in your case, you're gonna take the xml, and then try and use it for a
deserialization.
Thus the second URL might come in handy.
............
> thanks heaps Sloan, i knew there must have been something i was missing.
>
[quoted text clipped - 83 lines]
>>>
>>> </configuration>
sam01m - 23 Oct 2007 02:55 GMT
I just went through this myself... here is the solution that I came up with...
.........................................App.config
..............................................
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="MyCustomSection" type="My.Custom.Section.Handler,
My.Custom.Section.Handler.Assembly, Version=1.0.0.0,Culture=neutral,
PublicKeyToken=bd1505632153fa83" />
</configSections>
<MyCustomSection Username="MyUserName" Password="MyPassword" />
</configuration>
...................................My.Custom.Section.Handler.Assembly......................................
using System;
using System.Configuration;
using System.Collections.Generic;
using System.Text;
using System.Xml;
namespace My.Custom.Section
{
public class Handler: ConfigurationSection
{
[ConfigurationProperty("Username", DefaultValue = "", IsRequired =
true)]
public string Username
{
get
{
return (string)this["Username"];
}
set
{
this["Username"] = value;
}
}
[ConfigurationProperty("Password", DefaultValue = "", IsRequired =
true)]
public string Password
{
get
{
return (string)this["Password"];
}
set
{
this["Password"] = value;
}
}
}
}
.........................................Using the Custom
Section..........................................
using My.Custom.Section;
public class MyClass
{
public void MyMethod()
{
Handler section = (Handler)config.Sections["MyCustomSection"];
string username = section.Username;
string password = section.Password;
}
}
.............................Conclusion...................................
You could also do things like section encryption, etc... like this
System.Configuration.Configuration config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
Handler section = (Handler)config.Sections["MyCustomSection"];
if (!section.SectionInformation.IsProtected)
{
section.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider");
section.SectionInformation.ForceSave = true;
config.Save(ConfigurationSaveMode.Full);
}
// this is the information I wish I had when I was looking for answers, hope
it helps
ink - 24 Oct 2007 10:48 GMT
Thanks for this Sam.
i have managed to get it working by putting a Dummy Handler in that does
nothing and the Desterilizing the Raw XML of the section.
Your way is allot more elegant so i think i may implement your solution.
thanks,
ink
>I just went through this myself... here is the solution that I came up
>with...
[quoted text clipped - 91 lines]
> hope
> it helps