J Weezy
J Weezy

Reputation: 3957

How to configure C# WinForms application to get EnvironmentName that it is deployed to on Azure?

This question is related to: .NET Core console application, how to configure appSettings per environment?

Question How do I make a WinForms application environment-aware based on the different subscriptions that we have setup?

Context

I am finding very little documentation and examples on how to handle the deployment of Winforms applications to an Azure VM across multiple subscriptions. For example, if I deploy the Winforms application to a subscription that is marked as development, then it should use both the security groups and the SQL Server that is tied to the Development subscription. Rinse and repeat for UAT and Production.

Currently, we have a WinForms application that has multiple appsettings.<env>.json files for development, UAT, and production. We are using SlowCheetah to transform the appsettings.json file based on what the Configuration Manager says. This appears to be tied to the buildConfiguration parameter in the MsBuild pipeline task. We have different environment values in the appsettings files for the SQL Server connection string and security groups.

The application it intended to be deployed through a virtual machine and the environments are subscription-specific. How do we configure each subscription to contain the corresponding environment name and then pass that to the application on runtime? Would we still use SlowCheetah and set the environment through the buildConfiguration parameter in the MsBuild task, or would we do something else?

It seems like we can either go that route or we can use the Environment Variables route, but I don't see how the environment variable can be set for a virtual machine in Azure. Any help, documentation, or examples that I can pursue is greatly appreciated!

Note: for a web-application, this seems pretty straight-forward. Just setup the ASPNETCORE_ENVIRONMENT variable in both the project and in the Configuration within the App Service on Azure.

Upvotes: 0

Views: 648

Answers (1)

Dai
Dai

Reputation: 155250

As clarified in the comments, and if I understand you correctly, you want to set environment options in Azure Portal and have them visible to programs running within the VM.

Note that Azure Virtual Machines are just Infrastructure as a Service - they're kinda bare with minimal Azure services provided on-top, consequently there's no notion of a VM being in Production or Staging - that's all up to you to implement yourself.

That said, you can set some configuration values in the Azure Portal (and through PowerShell) which are passed-on to the VM: this can be done using the "User Data" area of your VM's configuration area.

Note this only applies to ARM (Azure Resource Manager) VMs, not "Classic" VMs or "Cloud Service" AMs.

enter image description here

Upvotes: 1

Related Questions