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

Question!

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.



Answers
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.

<ItemGroup>
    <PublishPackages > 
        <WebConfigXml>
            $(YourProjectPath)web.config
        </WebConfigXml>
        <WebJobConfig>
            $(YourWebJobPath)app.config
        </WebJobConfig>
    </PublishPackages>
</ItemGroup>
<Target Name="InjectConfigs">
    <XmlPeek 
        XmlInputPath="%(PublishPackages.WebConfigXml)" 
        Query="//parent/someelement">
        <Output TaskParameter="Result" ItemName="Peeked" />
    </XmlPeek>
    <XmlPoke 
        XmlInputPath="%(PublishPackages.WebJobConfig)"
        Query="//parent/someelement"
        Value="$(Peeked)"
        Condition =" '$(Peeked)'!=''" 
        />
</Target>
</Project>

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