Partager via


Deploying a web app with .NET

Overview

A walkthrough on creating a new web app from a Console app using .NET. This is not to be confused with deploying new code to an existing web app, it is a programmatic way to create web apps in a similar fashion to ARM or creation through the portal.

Walkthrough

1. To begin, create a new C# console application in Visual Studios.

2. Next add the required packages. For the Microsoft.Azure packages, I found the easiest way to quickly install them was via npm in the VS Console. You can copy the desired package and search for it on nugget.org.

using Microsoft.Azure.Management.ResourceManager;
using Microsoft.Azure.Management.Authorization;
using System;
using Microsoft.Azure.Management.Compute;
using Microsoft.Rest.Azure.Authentication;
using Microsoft.Azure.Management.WebSites;
using Microsoft.Azure.Management.WebSites.Models;
using System.Threading.Tasks;
using System.Collections.Generic;

3. The next step is to create a service principal that can be used to authenticate the requests.

  • Creating a service principal using the portal
  • Create a service principal using PowerShell

The information created in PowerShell or via the portal will be added in this portion of the code:

static string _tenantId = "";
private static string _clientId = "";
private static string _clientSecret = "";
private static string _subscriptionId = "";

4. The last step is implementing the code. Instead of trying to update the entire SiteConfig section in one go, I found that updating the individuals sections such as the connection strings and the app settings, separately, was easier.
Example:

AppSettingsResponse = webClient.WebApps.UpdateApplicationSettings
connectionstringResponse = webClient.WebApps.UpdateConnectionStrings

Example Program.cs

 using Microsoft.Azure.Management.ResourceManager;
using Microsoft.Azure.Management.Authorization;
using System;
using Microsoft.Azure.Management.Compute;
using Microsoft.Rest.Azure.Authentication;
using Microsoft.Azure.Management.WebSites;
using Microsoft.Azure.Management.WebSites.Models;
using System.Threading.Tasks;
using System.Collections.Generic;

namespace SimplifiedWebAppCreate
{
    class Program
    {

        private static string _tenantId = "****";
        private static string _clientId = "****";
        private static string _clientSecret = "****";
        private static string _subscriptionId = "****";

        static void Main(string[] args)
        {
            Site newWebApp = null;

            string resourceGroupName = "*ResourceGroupName*";
            string siteName = "*MyWebAppName*";
            string location = "southcentralus";
            string serverFarmId = "/subscriptions/*SubscriptionId*/resourceGroups/*ResourceGroupName*/providers/Microsoft.Web/serverfarms/*ServerFarmName*";

            try
            {
                var serviceCreds = Task.Run(() => ApplicationTokenProvider.LoginSilentAsync(_tenantId, _clientId, _clientSecret)).Result;
                var webClient = new WebSiteManagementClient(serviceCreds);

                webClient.SubscriptionId = _subscriptionId;

                //CREATE the web app
                var site = webClient.WebApps.CreateOrUpdate(resourceGroupName, siteName, new Site
                {
                    Location = location,
                    ServerFarmId = serverFarmId,
                });

                //ADD App Settings
                const string settingName = "Application Setting1", settingValue = "Setting Value 1";
                var appSetting = new StringDictionary { Name = settingName, Location = location, Properties = new Dictionary<string, string> { { settingName, settingValue } } };
                var appSettingsResponse = webClient.WebApps.UpdateApplicationSettings(
                    resourceGroupName,
                    siteName,
                    appSetting);

                //ADD Conneciton Strings
                var connectionStringValuePair = new ConnStringValueTypePair("*ConnectionStringValue*", ConnectionStringType.MySql);
                var connectionString = new ConnectionStringDictionary { Location = location, Properties = new Dictionary<string, ConnStringValueTypePair> { { "*Connection String Name*", connectionStringValuePair } } };
                var connectionstringResponse = webClient.WebApps.UpdateConnectionStrings(resourceGroupName, siteName, connectionString);

                appSettingsResponse = webClient.WebApps.ListApplicationSettings(resourceGroupName, siteName);
                connectionstringResponse = webClient.WebApps.ListConnectionStrings(resourceGroupName, siteName);

                Console.Write("Getting a site by name");
                newWebApp = webClient.WebApps.Get(resourceGroupName, siteName);
                Console.Write("Found site named {0}", newWebApp.Name);
            }
            catch (Exception ex)
            {
                Console.Write(ex.Message);
            }
        }
    }
}