services | platforms | author |
---|---|---|
app-service |
dotnet |
devigned |
This sample demonstrates how to manage your WebApps using the .NET SDK
On this page
-
If you don't have it, install the .NET Core SDK.
-
Clone the repository.
git clone https://github.com/Azure-Samples/app-service-web-dotnet-manage.git
-
Install the dependencies.
dotnet restore
-
Create an Azure service principal either through Azure CLI, PowerShell or the portal.
-
Add these environment variables to your .env file using your subscription id and the tenant id, client id and client secret from the service principle that you created.
export AZURE_TENANT_ID={your tenant id} export AZURE_CLIENT_ID={your client or application id} export AZURE_CLIENT_SECRET={your service principal secret} export AZURE_SUBSCRIPTION_ID={your subscription id}
-
Run the sample.
dotnet run
Main()
gets the environment variables that you set up for this sample and calls RunSample
.
RunSample
starts by setting up a WebSiteManagementClient
using those credentials.
// Build the service credentials and Azure Resource Manager clients
var serviceCreds = await ApplicationTokenProvider.LoginSilentAsync(tenantId, clientId, secret);
var resourceClient = new ResourceManagementClient(serviceCreds);
resourceClient.SubscriptionId = subscriptionId;
var webClient = new WebSiteManagementClient(serviceCreds);
webClient.SubscriptionId = subscriptionId;
The sample then sets up a resource group in which it will create the website.
Random r = new Random();
int postfix = r.Next(0, 1000000);
var resourceGroupName = "sample-dotnet-app-service-group";
var westus = "westus";
var serverFarmName = "sample-server-farm";
var siteName = "sample-site-name-" + postfix;
// Create the resource group
Write("Creating resource group: {0}", westus);
resourceClient.ResourceGroups.CreateOrUpdate(resourceGroupName, new ResourceGroup { Location = westus});
Create a server farm to host the website.
var serverFarm = webClient.ServerFarms.CreateOrUpdateServerFarm(resourceGroupName, serverFarmName, new ServerFarmWithRichSku{
Location = westus,
Sku = new SkuDescription{
Name = "S1",
Capacity = 1,
Tier = "Standard"
}
});
var site = webClient.Sites.CreateOrUpdateSite(resourceGroupName, siteName, new Site{
Location = westus,
ServerFarmId = serverFarm.Id
});
var gotSite = webClient.Sites.GetSite(resourceGroupName, siteName);
webClient.Sites.DeleteSite(resourceGroupName, siteName);
Please refer to Azure SDK for .NET for more information.