1

What is the best way to determine if my web application code is running in a Azure web role (or emulator)?

I need to configure a dependency injection container and do not want to take a dependency on any Azure specific assemblies unless I'm running in Azure.

Both answers to this question require referencing Azure specific assemblies which I'd like to prevent for my on-premise scenario.

Update: I'm really looking for something in the runtime environment that I can query from my web application code.

Community
  • 1
  • 1
MvdD
  • 22,082
  • 8
  • 65
  • 93
  • Add a compilation symbol that will only appear for Azure deployment, then use `#if AZURE` in your dependency configuration. – haim770 May 26 '15 at 17:41
  • I would rather not have to generate separate assemblies for on-premise and Azure. – MvdD May 26 '15 at 22:03
  • See http://stackoverflow.com/questions/25678419/how-to-check-if-code-is-running-on-azure-websites/25695126#25695126 – Brendan Green May 26 '15 at 22:21
  • @BrendanGreen I want to know if my code is running as a PaaS web role, not an Azure web site. There don't seem to be any Azure specific environment variables set in a web role. – MvdD May 27 '15 at 05:03
  • Can you be more specific about the kind of dependency you're asking about? – John Saunders May 27 '15 at 05:22
  • @JohnSaunders Not sure I get your question. I'm trying to configure my authentication OWIN middleware to use WS-Federation for on-premise and OpenID Connect in the cloud. I use Unity with XML configuration for late binding. I want to prevent having to ship Azure specific assemblies to my on-premise deployment. – MvdD May 27 '15 at 05:28
  • What's the rationale behind not wanting a reference to an azure assembly? This feels like it's introducing unnecessary complexity where it isn't needed. – Brendan Green May 27 '15 at 06:12
  • @BrendanGreen Maybe I'm making this more difficult than needed. It just felt wrong to have to deploy Azure assemblies when running on-prem. Especially since these are used from GAC'd assemblies and thus require to be in the GAC themselves. – MvdD May 27 '15 at 15:02
  • I'm not sure I understand - the `CloudConfigurationManager` class in in `Microsoft.WindowsAzure.Configuration`, which is a nuget package that doesn't need to go into the GAC. Maybe you are referring to the Service Runtime (which is not needed in this case)? – Brendan Green May 27 '15 at 22:01
  • I did indeed not realize that the CloudConfigurationManager was in a redistributable assembly. Thanks. – MvdD May 28 '15 at 17:25

2 Answers2

1

You could do this with via a web.config / app.config / ServiceConfiguration.cscfg setting that you only set in the specific environments that you want.

Add a parameter, for example:

<Setting name="PaaS" value="true" />

Then, at application startup, get the value:

public bool IsPaaS
{
    get
    {
        var res = false;
        var val = CloudConfigurationManager.GetSetting("PaaS") ?? "false";
        Boolean.TryParse(val, out res);
        return res;
    }
}

CloudConfigurationManager.GetSetting will fall back if there is no cscfg setting present, and check the web.config and/or app.config.

Set this in the WebRole, but not in the web.config when running under local IIS to get the behavior you are looking for.

Brendan Green
  • 11,676
  • 5
  • 44
  • 76
  • Yes, this would work, but would also require me to reference Azure specific assemblies to get CloudConfigurationManager. Also, I'd like to determine if I'm running in Azure from the runtime environment, not application configuration. – MvdD May 27 '15 at 05:29
  • How about setting an environment variable as the role starts up? https://msdn.microsoft.com/en-us/library/azure/gg432991.aspx – Brendan Green May 27 '15 at 05:36
  • Yeah, I tried that. It did not work. Will try again tomorrow morning. – MvdD May 27 '15 at 05:43
0

You can set an application setting for your web app through the portal (or ARM template), then check for its value within your code.

App settings through the Azure Portal

var isPaaSSetting = Environment.GetEnvironmentVariable("IS_PAAS");
var isPaaS = !string.IsNullOrEmpty(isPaaSSetting) && isPaaSSetting.ToUpper() == "TRUE";
if (isPaaS) {...}