How to read site's web.config file from Azure WebJob?


I am using custom web.config configuration elements to configure complex application services for my ASP.NET MVC application. I need this configuration data in my Azure WebJob which is deployed alongside the web app. I am trying to avoid copying these sections from website's web.config to webjob's app.config file.

So, apart from connection strings and application settings, is there a way for an Azure WebJob to read the website's web.config file?

Ideally, I would like to use ConfigurationManager.GetSection to create my custom ConfigurationSection instances.

Here is a solution for a build time. Probably this sample Msbuild script would insert a peeked xml elements from your web.config into another xml file. Please note it is not tested.

    <PublishPackages > 
<Target Name="InjectConfigs">
        <Output TaskParameter="Result" ItemName="Peeked" />
        Condition =" '$(Peeked)'!=''" 

These docs can help: Configuring Parameters for Web Package Deployment, XmlPeek task And if XmlPoke and XmlPeek won't do the thing another way is to use XmlFile task. Hope it helps!

UPDATE. Changed to use only XmlPeek and XmlPook without XmlFile.

By : Artyom

Here is a solution for installation time. There is a technology called Web Deploy that allows to create packages which can be run from command line and install services. And it allows parameterization of .config files by using one xml file called Parameters.xml. This one can be shared by your web site and Web Job I believe. The howto manual on doing it.

By : Artyom

Most web apps + web jobs have sensitive settings that you don’t want in a config file. You can deploy those secrets using ARM/PS see deploying secrets to Azure. So just add all your settings to your ARM/PS script.

Azure App settings apply to the web job and the web app, this is the recommended approach.

This video can help you solving your question :)
By: admin