J’ai pas été assez clair, je l’avoue.
Donc ici mon problème (ou plutôt mon interrogation car mon programme fonctionne très bien sur monoproc, biproc, ou quadriproc) ne porte que sur l’accès à une de mes classes.
“Plutôt que des mots, démo” :
Je commence de zéro: le fichier de configuration.
[code]<?xml version="1.0" encoding="utf-8"?>
alacon@cafzone.net
alacon@gmail.com
[/code]
Les classes qui seront désérialisées de la section de configuration (version simplifiée) :
[code]using System;
using System.Xml.Serialization;
namespace PMLServiceLib.Configuration
{
[Serializable()]
[XmlRoot(Configuration.XmlElementName, IsNullable = false)]
public class Configuration
{
public const string XmlElementName = “serviceConfiguration”;
private Logger.Logger m_Logger;
[XmlElement(IsNullable = false)]
public Logger.Logger Logger
{
get { return m_Logger; }
set { m_Logger = value; }
}
public Configuration()
{
m_Logger = new Logger.Logger();
}
}
}[/code]
Avec les petits frères en fichiers joints (version complète) :
[attachment=473:attachment]
[attachment=472:attachment]
[attachment=471:attachment]
[attachment=470:attachment]
Le SectionHandler (version simplifiée, tiré d’ici) qui va désérialiser ma section de configuration XML :
[code]using System;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Serialization;
using System.Configuration;
namespace PMLServiceLib.Configuration
{
public class SectionHandler : IConfigurationSectionHandler
{
public object Create(object parent, object configContext, XmlNode section)
{
XPathNavigator xpn = section.CreateNavigator();
string tn = xpn.Evaluate(“string(@type)”).ToString();
if (tn == string.Empty)
throw new ConfigurationErrorsException(string.Format(“The type attribute is not present on the root node of the <{0}> configuration section.”, section.Name), section);
Type t = Type.GetType(tn);
if (t == null)
throw new ConfigurationErrorsException(string.Format(“The type attribute ‘{0}’ specified in the root node of the the <{1}> configuration section is not a valid type.”, tn, section.Name), section);
XmlSerializer xs = new XmlSerializer(t);
XmlNodeReader xnr = new XmlNodeReader(section);
try
{
return xs.Deserialize(xnr);
}
catch (Exception ex)
{
string s = ex.Message;
Exception innerException = ex.InnerException;
while (innerException != null)
{
s += " " + innerException.Message;
innerException = innerException.InnerException;
}
throw new ConfigurationErrorsException(string.Format(“Unable to deserialize an object of type ‘{0}’ from the <{1}> configuration section: {2}”, tn, section.Name, s), ex, section);
}
}
}
}[/code]
Puis la classe qui va appeller indirectement le processing de la section :
[code]using System;
using System.Configuration;
namespace PMLServiceLib
{
public class ServiceConfiguration
{
private static Configuration.Configuration m_Root;
public static Configuration.Configuration Root
{
get { return m_Root; }
}
static ServiceConfiguration()
{
m_Root = GetServiceConfiguration(Configuration.Configuration.XmlElementName);
}
private static Configuration.Configuration GetServiceConfiguration(string xpath)
{
Configuration.Configuration c = null;
try
{
c = ConfigurationManager.GetSection(xpath) as Configuration.Configuration;
}
catch (ConfigurationErrorsException ex)
{
throw new ApplicationException(“Une erreur s’est produite lors du chargement du fichier de configuration.”, ex);
}
catch (Exception ex)
{
throw new ApplicationException(“Une erreur non gérée s’est produite lors du chargement du fichier de configuration.”, ex);
}
finally
{
if (c == null) throw new ApplicationException(“Le fichier de configuration est correct, mais il ne contient aucune configuration valide.”);
}
return c;
}
}
}[/code]
Enfin, dans le code du main, ou des threads (provenant d’un ThreadPool pour la plupart, ou sinon gérés également à la mimine avec new ThreadStart(), Thread.Start(), etc…) j’appelle en lecture ma configuration dans ce genre là:
if (ServiceConfiguration.Root.Logger.Mailer.Enabled)
{
// get beer
}
Donc accès en lecture, puisque que seul le SectionHandler a instancié ma classe “Configuration”, et a mis à jour les propriétés via désérialisation XML.
J’espère que ça suffira pour comprendre mon problème.