Home All Groups Group Topic Archive Search About

AppDomainSetup.SetConfigurationBytes

Author
6 Nov 2006 8:13 PM
jimchristopher
Hi Everyone;

I'm creating an application that hosts the workstation version of the
2.0.50727 CLR.  I've been trying to leverage the
AppDomainSetup.SetConfigurationBytes method so the host can generate
the configuration file for each app domain directly, instead of having
to write out a file for every domain.  The configuration data the host
provides appears to be ignored when the configuration subsystem looks
for its data.

For instance:

AppDomainSetup appDomainInfo = new AppDomainSetup();
appDomainInfo.SetConfigurationBytes( Encoding.UTF8.GetBytes(
@"<configuration>
    <appSettings>
        <add key=""message"" value=""internal settings loaded"" />
    </appSettings>
</configuration>"  ) );

AppDomain ad = AppDomain.CreateDomain( "mydomain", null, appDomainInfo
);
AppDomainManager adm = ( AppDomainManager )ad.DomainManager;
adm.ExecuteAssembly( "MyAssembly" );

The assembly being executed simply shows the value of the app setting:
....
string str;
try
{
    str = System.Configuration.ConfigurationSettings.AppSettings[
"message" ].ToString();
}
catch( Exception e )
{
    str = "unable to load config item: " + e.Message;
}

System.Windows.Forms.MessageBox.Show( str );
....

When I run this code as-is, the loaded assembly throws a null reference
exception when it attempts to load the configuration setting.  The data
*is* being saved, as shown by a simple sanity-check: after calling
SetConfigurationBytes, the GetConfigurationBytes method returns the
same byte array passed in via SetConfigurationBytes.

If the host saves the same XML to a file and sets the
AppDomainSetup.ConfigurationFile property to the saved file, the
setting is loaded as I would expect; this tells me that the XML itself
is perfectly valid.

One thought I had was that the configuration system was expecting the
byte array to conform to a specific text encoding; I've tried every
encoding readily available in the .NET 2.0, as well as specifying the
encoding used in the <?xml ?> processing-instruction.  The results are
always the same: the data is not there when I try to load the
application setting.

One interesting facet of this issue: the data obviously isn't
discounted outright, as the app domain creation will throw a type load
exception ( "The domain manager specified by the host could not be
instantiated." ) if the XML supplied to SetConfigurationBytes is
malformed in any way.

Any insight would be appreciated.  Anyone out there successfully used
SetConfigurationBytes?

Obliged,
Beefarino

AddThis Social Bookmark Button