微信号:cnmsdn

介绍:微软中国MSDN开发社区官方微信.

如何使用 .NET SDK 管理你的 Web App

2017-01-11 23:04 微软中国MSDN

This sample demonstrates how to manage your WebApps using the .NET SDK

On this page

  • Run this sample

  • What does Program.cs do?

    • Create a server farm

    • Create a website

    • Get website details

    • Delete a website

Run this sample

  1. If you don't have it, install the .NET Core SDK.

  2. Clone the repository.

    git clone https://github.com/Azure-Samples/app-service-web-dotnet-manage.git
  3. Install the dependencies.

    dotnet restore
  4. Create an Azure service principal either through Azure CLIPowerShell or the portal.

  5. 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}
  6. Run the sample.

    dotnet run

What does Program.cs do?

Main() gets the environment variables that you set up for this sample and calls RunSampleRunSample starts by setting up aWebSiteManagementClient using those credentials.

// Build the service credentials and Azure Resource Manager clientsvar 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 groupWrite("Creating resource group: {0}", westus);
resourceClient.ResourceGroups.CreateOrUpdate(resourceGroupName, new ResourceGroup { Location = westus});

Create a server farm

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"
    }
});

Create a website

var site = webClient.Sites.CreateOrUpdateSite(resourceGroupName, siteName, new Site{
    Location = westus,
    ServerFarmId = serverFarm.Id
});

Get details for the given website

var gotSite = webClient.Sites.GetSite(resourceGroupName, siteName);

Delete a website

webClient.Sites.DeleteSite(resourceGroupName, siteName);


想获得GitHub地址请点击“阅读原文”一键直达

 
微软中国MSDN 更多文章 微软 Azure IoT 产品新功能发布 面向开发人员的 Azure 数据分析 如何使用 .NET SDK 管理你的 Web App 如何使用 .NET SDK 管理你的 Web App 微软 Azure IoT 产品新功能发布
猜您喜欢 【人物】李道兵:辍学的首席架构师 移动APP区别于PC设计的十大差异 Netflix支付生态系统迁移到AWS的实践 Paxos原理与实现(一)原理篇 不是技术牛人,如何拿到国内IT巨头的Offer