This tutorial describes how to a deploy a .NET Framework web application to
Compute Engine.

This tutorial is intended for developers and DevOps engineers who have basic
knowledge of Microsoft .NET and Compute Engine.

## Objectives

Deploy an ASP.NET Model-View-Controller (MVC) web-application that uses .NET
Framework 4 and runs on Windows to a single Compute Engine instance.

> [!NOTE]
> **Note:** For information on deploying an ASP.NET Core web application that uses .NET 6 and runs on Linux, see [Deploy an ASP.NET application to Compute Engine](https://docs.cloud.google.com/dotnet/docs/getting-started/getting-started-on-compute-engine-net-netframework.md.txt).

This tutorial shows you how to complete the following tasks to reach your objective:

- Deploy a Compute Engine VM
- Set up load balancing
- Deploy the ASP.NET application

## Costs


In this document, you use the following billable components of Google Cloud:


- [Compute Engine virtual machine (VM) instances](https://cloud.google.com/compute/all-pricing)
- [Compute Engine persistent disks](https://cloud.google.com/compute/all-pricing#disk)

<br />


To generate a cost estimate based on your projected usage,
use the [pricing calculator](https://docs.cloud.google.com/products/calculator).
New Google Cloud users might be eligible for a [free trial](https://docs.cloud.google.com/free).

<br />

When you finish the tasks that are described in this document, you can avoid
continued billing by deleting the resources that you created. For more information, see
[Clean up](https://docs.cloud.google.com/dotnet/docs/getting-started/getting-started-on-compute-engine-netframework-windows.md.txt#clean-up).

## Before you begin

## Deploy a Compute Engine VM

This section shows you how to create a Linux VM, or a Windows Server VM that runs
[Microsoft Internet Information Services (IIS) web servers](https://www.iis.net/)
on Compute Engine.

1. Set default values for your project ID and Compute Engine zone. This
   helps you save time.

   ```
   gcloud config set project PROJECT_ID
   gcloud config set compute/zone ZONE
   ```

   Replace the following:
   - `PROJECT_ID` with the ID of your Google Cloud project.
   - `ZONE` with the name of the zone that you're going to use for creating resources. If you are unsure about which zone to pick, use the zone geographically located closest to you.

   For example:

   ```
   gcloud config set project test-project-12345
   gcloud config set compute/zone us-central1-a
   ```
2. Create a VM instance:

   To create a Windows Server VM that runs IIS, do the following:
   1. Create a [startup script](https://docs.cloud.google.com/compute/docs/instances/startup-scripts/window)
      for the VM instance. This script will run during VM initialization and
      install IIS:

      ```
      "# Install IIS
      Enable-WindowsOptionalFeature -Online -FeatureName ``
          NetFx4Extended-ASPNET45, ``
          IIS-WebServerRole, ``
          IIS-WebServer, ``
          IIS-CommonHttpFeatures, ``
          IIS-HttpErrors, ``
          IIS-HttpRedirect, ``
          IIS-ApplicationDevelopment, ``
          IIS-HealthAndDiagnostics, ``
          IIS-HttpLogging, ``
          IIS-LoggingLibraries, ``
          IIS-RequestMonitor, ``
          IIS-HttpTracing, ``
          IIS-Security, ``
          IIS-RequestFiltering, ``
          IIS-Performance, ``
          IIS-WebServerManagementTools, ``
          IIS-IIS6ManagementCompatibility, ``
          IIS-Metabase, ``
          IIS-DefaultDocument, ``
          IIS-ApplicationInit, ``
          IIS-NetFxExtensibility45, ``
          IIS-ISAPIExtensions, ``
          IIS-ISAPIFilter, ``
          IIS-ASPNET45, ``
          IIS-HttpCompressionStatic
      Install-WindowsFeature Web-Mgmt-Service

      # Install WebDeploy
      [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType] 'Ssl3,Tls12'
      (New-Object Net.WebClient).DownloadFile(
          'https://download.microsoft.com/download/0/1/D/01DC28EA-638C-4A22-A57B-4CEF97755C6C/WebDeploy_amd64_en-US.msi',
          ""$env:Temp\webdeploy.msi"")
      & msiexec /qb! /i $env:Temp\webdeploy.msi | Out-Default
      " | Out-File -Encoding ASCII startup.ps1
      ```
   2. Create the VM instance and run the startup script `startup.ps1`:

      ```
      gcloud compute instances create clouddemo-1 `
          --image-family windows-2019 `
          --image-project windows-cloud `
          --machine-type n1-standard-2 `
          --boot-disk-type pd-ssd `
          --tags loadbalancer-backend `
          --metadata-from-file sysprep-startup-script-ps1=startup.ps1
      ```
3. Monitor the initialization process of the VM by viewing its serial port output:

   ```
   gcloud compute instances tail-serial-port-output clouddemo-1
   ```

   Wait about 5 minutes until you see the output `Instance setup finished` or
   `Startup finished`, and then press Ctrl+C. At this point, the installation
   of prerequisites is complete and the VM instance is ready to be used.

## Set up load balancing

To make your ASP.NET app available over the internet, you have to use an
[HTTPS load balancer](https://docs.cloud.google.com/load-balancing/docs/https.md.txt). To associate your VM instance
with the load balancer, create an [instance group](https://docs.cloud.google.com/compute/docs/instance-groups.md.txt)
and assign this instance group to the load balancer:

1. Create an [unmanaged instance group](https://docs.cloud.google.com/compute/docs/instance-groups/creating-groups-of-unmanaged-instances.md.txt)
   and add the VM instance:

   ```
   gcloud compute instance-groups unmanaged create clouddemo-1
   gcloud compute instance-groups unmanaged add-instances clouddemo-1 --instances clouddemo-1
   ```
2. Create a health check that checks if the web server is running:

   ```
   gcloud compute http-health-checks create clouddemo-health `
       --check-interval 5s `
       --unhealthy-threshold 2 `
       --request-path /
   gcloud compute instance-groups set-named-ports clouddemo-1 --named-ports=http:80
   ```
3. Create a load balancer backend service that uses the HTTP health check and
   the instance group that you created previously:

   ```
   gcloud compute backend-services create clouddemo-backend `
     --http-health-checks clouddemo-health `
     --port-name http `
     --protocol HTTP `
     --global
   gcloud compute backend-services add-backend clouddemo-backend `
     --instance-group clouddemo-1 `
     --global `
     --instance-group-zone $(gcloud config get-value compute/zone)
   ```
4. Create a front-end for the load balancer:

   ```
   gcloud compute url-maps create clouddemo-map --default-service clouddemo-backend
   gcloud compute target-http-proxies create clouddemo-proxy --url-map clouddemo-map
   gcloud compute forwarding-rules create clouddemo-frontend --global --target-http-proxy clouddemo-proxy --ports 80
   ```
5. Create a [firewall rule that allows the load balancer](https://docs.cloud.google.com/load-balancing/docs/https.md.txt#firewall-rules)
   to send HTTP requests to instances that have been annotated with the `loadbalancer-backend`
   tag.

   ```
   gcloud compute firewall-rules create loadbalancer-backend `
     --source-ranges "130.211.0.0/22,35.191.0.0/16" `
     --target-tags loadbalancer-backend `
     --allow tcp:80,tcp:5000
   ```
6. Lookup the IP address of the load balancer:

   ```
   gcloud compute forwarding-rules describe clouddemo-frontend --global --format "value(IPAddress)"
   ```

   Take note of the IP address. You will need it later.

## Deploy the ASP.NET application

1. Open a PowerShell console.

2. [Download and unzip](https://github.com/GoogleCloudPlatform/dotnet-docs-samples/archive/refs/heads/main.zip)
   or clone the sample repository from github:

   ```
   git clone https://github.com/GoogleCloudPlatform/dotnet-docs-samples.git
   ```
3. Build the deployment package:

   1. Switch to the directory that contains the sample application:

      ```
      cd dotnet-docs-samples\applications\clouddemo\net4
      ```
   2. Restore NuGet dependencies:

      ```
      nuget restore
      ```
   3. Build the solution and use the `PackageProfile` publishing profile to
      create a WebDeploy deployment package:

      ```
      msbuild /t:clean,rebuild CloudDemo.Mvc.sln /p:DeployOnBuild=true /p:PublishProfile=PackageProfile
      ```

      The `CloudDemo.Mvc\bin` folder now contains a file `CloudDemo.Mvc.zip`.
4. Copy the deployment package to the VM:

   1. [Create a username and password](https://docs.cloud.google.com/compute/docs/instances/windows/creating-passwords-for-windows-instances) for the VM instance.
   2. [Connect to the VM by using Remote Desktop](https://docs.cloud.google.com/compute/docs/instances/connecting-to-windows.md.txt#windows_gui) and log in using the username and password created in the previous step.
   3. Copy the file `CloudDemo.Mvc.zip` from the `CloudDemo.Mvc\bin` folder to a temporary location on the VM instance.
   4. In the Remote Desktop session, right-click on the **Start** button (or press **Win+X** ) and click **Windows PowerShell (Admin)**.
   5. Confirm the elevation prompt by clicking **Yes**.
   6. Deploy the deployment package:

          &"C:\Program Files\IIS\Microsoft Web Deploy V3\msdeploy.exe" -source:package=PACKAGE -verb:sync -dest:auto

      Replace `PACKAGE` with the path to the
      deployment package.
5. On your local computer, open a web browser and navigate to the following
   address:

       http://LOADBALANCER_IP/

   Replace `LOADBALANCER_IP` with the IP address that
   you obtained after deploying the load balancer.

   You now see the demo application and the title *This app is running on Compute Engine*.

## Clean up

After you finish the tutorial, you can clean up the resources that you created
so that they stop using quota and incurring charges. The following sections
describe how to delete or turn off these resources.

### Delete the project

The easiest way to eliminate billing is to delete the project that you created
for the tutorial.

> [!CAUTION]
> **Caution** : Deleting a project has the following effects:
>
> - **Everything in the project is deleted.** If you used an existing project for the tasks in this document, when you delete it, you also delete any other work you've done in the project.
> - **Custom project IDs are lost.** When you created this project, you might have created a custom project ID that you want to use in the future. To preserve the URLs that use the project ID, such as an `appspot.com` URL, delete selected resources inside the project instead of deleting the whole project.
>
>
> If you plan to explore multiple architectures, tutorials, or quickstarts, reusing projects
> can help you avoid exceeding project quota limits.

1. Delete a Google Cloud project:

```
gcloud projects delete PROJECT_ID
```

<br />

### Delete individual resources

You'll need to individually delete all the resources created for the project
(for example: instance groups, health checks, backend services, http proxy,
and forwarding rules). You can't delete the VM instances until delete all these
resources.

## What's next

- Learn more about [creating and running virtual machines on Google infrastructure](https://docs.cloud.google.com/compute/docs.md.txt).
- Review the best practices in the [Google Cloud Well-Architected Framework](https://docs.cloud.google.com/architecture/framework.md.txt).
- Explore reference architectures, diagrams, and best practices about Google Cloud. Take a look at our [Cloud Architecture Center](https://docs.cloud.google.com/architecture).