0

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.

J Weezy
  • 3,507
  • 3
  • 32
  • 88
  • An Azure VM is "just" a VM - so it's just like running on a normal computer - it's not like App Services, Functions/"Serverless" or the now-legacy Azure Cloud Services - so it's up to you to define what "production" is and to deploy and configure your software appropriately. However it's a runtime setting, so you don't set in MSBuild. – Dai Jun 16 '21 at 17:45
  • The VMs are tied to the various subscriptions. – J Weezy Jun 16 '21 at 17:46
  • *Everything* in Azure is tied to a subscription - so that point is irrelevant. – Dai Jun 16 '21 at 17:46
  • The .NET Core "Environment" value is typically set via an Environment Variable. For ASP.NET Core it's done with the `ASPNETCORE_ENVIRONMENT` environment-variable. But that doesn't apply to WinForms, ofc. – Dai Jun 16 '21 at 17:47
  • Hmm, maybe I was inarticulate with the problem that I am trying to solve. How do I make the application environment-aware based on the different subscriptions that we have setup? – J Weezy Jun 16 '21 at 17:51
  • https://stackoverflow.com/questions/40394569/how-to-get-subscription-id-from-the-vm-in-azure – Dai Jun 16 '21 at 19:23
  • That is not quite what I am looking for. Is it possible to create some kind of environment variable that automatically maps to the environment variable? Or, is what I am trying to do neither possible nor feasible? For example, it is easy to make a web app environment-aware by simply creating the environment variable in the Configuration section of the App Service and then setting its value. Azure then maps that value into the running web app. I was hoping this could be done through some configuration or via the related link that I referenced at the top of the OP. – J Weezy Jun 16 '21 at 19:35
  • Well, there is the "User Data" area under Azure Portal > Virtual Machines > (your VM) > Settings > Configuration> User Data. – Dai Jun 16 '21 at 19:48

1 Answers1

1

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

Dai
  • 141,631
  • 28
  • 261
  • 374