Andy Danger Gagne
Andy Danger Gagne

Reputation: 2227

DataContractSerializer - Configuration system failed to initialize -- app config setting

I'm using the DataContractSerializer as a standalone piece to serialize some objects that use DataContract/Member... Unfortunately, it keeps throwing an exception if i keep a custom App.Config section added to my solution's config settings that has absolutely nothing to do with this process... For instance, my config is set like so:

<configuration>
    <appSettings>
         <!--stuff goes here -->
    </appSettings>

    <MyCustomSectionItDoesntLike>
        <!--stuff goes here -->
    </MyCustomSectionItDoesntLike>
</configuration>

I then take the object and try to write using a memorystream...

DataContractSerializer serializer = new DataContractSerializer(item.GetType());
using (MemoryStream memoryStream = new MemoryStream())
    {
           serializer.WriteObject(memoryStream, item);

    }

If i remove the MyCustomSectionItDoesntLike from the config settings, it works just fine, but when i put it back in, an exception gets triggered:

Message:

The type initializer for 'System.Runtime.Serialization.DiagnosticUtility' threw an exception.

Inner:

Unrecognized configuration section MyCustomSectionItDoesntLike. (D:\test\bin\x86\Debug\test.vshost.exe.config line 47)

Im unsure as to why it doesnt care what that setting is anywhere in the project except when im going to serialize... Is there a setting, or a config section i need to add for this to work properly?

thanks!

Update

total bonehead error.... had nothing to do with the serializer...

<configuration>
    <configSections>
         <!--This is where i blew it -->
         <section name="MyCustomSectionItDoesntLike" type="System.Stuff.Stuff" />
    </configSections>
    <appSettings>
         <!--stuff goes here -->
    </appSettings>

    <MyCustomSectionItDoesntLike>
        <!--stuff goes here -->
    </MyCustomSectionItDoesntLike>
</configuration>

Upvotes: 2

Views: 2100

Answers (1)

Bartosz
Bartosz

Reputation: 3358

You have to reference the class that implements your custom section in configSections in your app.config file.

Check http://msdn.microsoft.com/en-us/library/2tw134k3%28v=vs.100%29.aspx for more info.

Upvotes: 1

Related Questions