Fixing Error: "SetConfigurationSettingPublisher needs to be called before FromConfigurationSetting can be used"



Have you spent your hours getting rid of the following exception ?

SetConfiSePubl1.gif

I have. So in this article, I am trying to save you time.
 
The first thing to do is to explain why this error occurs.

Azure application runs in Full IIS model from SDK 1.3 onwards.

In simple English we can say that from SDK 1.3 onward WebRole.cs and Web Application code run in a different application domain. So if we write or have code to Set Configuration setting in WebRole.cs then we are going to get the exception above.

How to solve?

  1. Make sure you have the Diagnostic connection string in settings of Web Role.

    SetConfiSePubl2.gif
     
  2. See if in your Web Role project Global.asax file exists. If it does not exist then right click on the Web Role project and add one.
     
  3. Open Global.asax.cs file and in Application_Start() add the code shown below:

    SetConfiSePubl3.gif

The bottom line is that to get rid of the exception, you need to copy the following code in Application_Start().

Code to copy

CloudStorageAccount.SetConfigurationSettingPublisher((configName, configSettingPublisher) =>
            {
                var connectionString = RoleEnvironment.GetConfigurationSettingValue(configName);
                configSettingPublisher(connectionString);
            }
           );


I hope this article may have saved you some time.

erver'>
Up Next
    Ebook Download
    View all
    Learn
    View all