7

*Version: Tridion 2009 *
I have added a Custom Configuration section in Tridion.ContentManager.Config for storing some values to be used by the Custom Resolver that I'm writing.

I will use this value to detect whether the current publish transaction is executing against the live publication target or not.

However, when I attempt to retrieve the values from the config file, I get "Object reference not set to an instance of an object." error.

This is how the configuration looks like:

    <configuration>
        <configSections>
            <!-- Other Config Section Declarations -->     
            <section name="My.ConfigSection" 
                     type="System.Configuration.NameValueSectionHandler" />
        </configSections>
        <My.ConfigSection>
                <add key="LivePublicationTargetId" 
                     value="tcm:0-10-65537" />
        </My.ConfigSection>
        <!-- Other Config Sections-->
    </configuration>

This is how I try to retrieve it from the IResolver class: (Please note that this is working for regular ASP.net websites.)

string LivePubTargetID = ((NameValueCollection)ConfigurationManager.
       GetSection("My.ConfigSection")).Get("LivePublicationTargetId");

This is the piece that returns null and causes the error: ConfigurationManager.GetSection("My.ConfigSection").

Any ideas about how to overcome this issue?

2
  • Check your config file for non-printing characters.
    – oic
    Commented Mar 30, 2013 at 13:01
  • thanx Oic, but the config file seems to be ok. I have eventually modified it with Visual Studi, plus the "custom resolving mappings" that are done in very same config file seem to be accurate. Commented Apr 2, 2013 at 12:14

4 Answers 4

3

Could you please break your code down by this way -

NameValueCollection section = (NameValueCollection)ConfigurationManager.GetSection("My.ConfigSection");

string liveTargetId= section["LivePublicationTargetId"];

By this way you can easily find out where exactly NULL exists, in “section” or “LivePublicationTargetId”.

Please update me about this. I will take next step on this behalf and update the answer here.

1
  • I have already done that. It's the "section" that doesn't exist (that is null) Commented Apr 2, 2013 at 12:07
3

As far as I know, Tridion.ContentManager.config isn't loaded by the ConfigurationManager class. Even if it was, I would not recommend putting your own stuff into it as it will likely get overwritten the next time you upgrade.

Put it in your own App.config file instead; that should work with the code you have.

2
  • Hi Peter, Thanks for the answer. That's what I have eventually what I've done (kind of). Since there was no App.config file available, I've created and used my own config file. Commented Apr 17, 2013 at 14:14
  • But the online docs, certainly for Tridion 2011 SP1 say "If your custom resolver also requires configuration, SDL Tridion expects to find such configuration in the same Tridion.ContentManager.config file".
    – Neil
    Commented Apr 21, 2013 at 20:37
2

I ended up using my own configuration file for this. I would like to share with you.

        string livePublicationTargetId = "";
        private string LivePublicationTargetId
        {
            get
            {
                if (String.IsNullOrEmpty(livePublicationTargetId))
                {

                    //get the dll path: first parse it as URI to get rid off ///file: prefix and then HTTP decode
                    string assemblyFile = System.Web.HttpUtility.UrlDecode((new System.Uri(Assembly.GetExecutingAssembly().CodeBase)).AbsolutePath);


                    ExeConfigurationFileMap configMap = new ExeConfigurationFileMap();

                    //set ExeConfigFile name : use the assemblyname but replace '.dll' with '.config'
                    configMap.ExeConfigFilename = assemblyFile.Substring(0, assemblyFile.LastIndexOf('.')) + ".config";

                    //init config object
                    Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);
                    if (!config.HasFile) throw new System.IO.FileNotFoundException(String.Format("Config file '{0}' doesn't exist", configMap.ExeConfigFilename));

                    //try reading the config value. It will throw error if it can't be found/read.             
                    livePublicationTargetId = ((ClientSettingsSection)config.GetSection("applicationSettings/MyClient.Tridion.CustomResolving.Properties.Settings"))
                    .Settings.Get("LivePublicationTargetId").Value.ValueXml.InnerText;
                }

                return livePublicationTargetId;
            }

        }
0

Try this way:

private void ReadSettings() {
    NameValueCollection myConfigSection = (NameValueCollection)ConfigurationSettings.GetConfig("My.ConfigSection");
    LivePubTargetID = myConfigSection["LivePublicationTargetId"].toString(); 
}

One more information, If you want to read the values from a file other than the app.config, you need to load it into the ConfigurationManager.

Try using this method: ConfigurationManager.OpenMappedExeConfiguration()

1
  • Nope, the outcome is not different. ConfigurationSettings.GetConfig() is obsolete and replaced with ConfigurationManager.GetSection() in .net 2.0. I gave it a try but doesn't work : ConfigurationSettings.GetConfig("My.ConfigSection") also returns null Commented Mar 30, 2013 at 8:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.