1

I've seen some examples of how to create storage resources using ARM and C#. I'm assuming that the same is possible for apps. However, I can't find a working example. Ideally I'd like to have a c#/Web api app that would be able to deploy another app. The process should be fully automated. Basically, I'd like to create a new instance of the same app with its own configuration - the process would be triggered by a new customer signing up for my SaaS. Could someone please give some pointers on how to deal with the above? Thanks.

bdristan
  • 1,048
  • 1
  • 12
  • 36
  • Hi @bdristan, any updates? Are you able to manage Azure app service and deploy web application (deployment package) to Azure app service web app programmatically in C# code now? – Fei Han Sep 19 '17 at 07:13
  • Hi Fred Han, I haven't gotten to it yet. However, I think that solutions provided below will work for me. I'll need to tackle the issue in about two months. – bdristan Sep 22 '17 at 00:53

2 Answers2

2

It seems that you’d like to deploy web application (deployment package) to Azure app service web app programmatically in C# code, you can try to use Kudu Zip API that allows expanding zip files into folders. And the following sample code works fine for me, you can refer to it.

//get username and password from publish profile on Azure portal
var username = "xxxxx";
var password = "xxxxxxxxxxxxxxxxxxxx";
var AppName = "{your_app_name}";

var base64Auth = Convert.ToBase64String(Encoding.Default.GetBytes($"{username}:{password}"));
var file = File.ReadAllBytes(@"C:\Users\xxx\xxx\WebApplication1.zip");
MemoryStream stream = new MemoryStream(file);

using (var client = new HttpClient())
{
    client.DefaultRequestHeaders.Add("Authorization", "Basic " + base64Auth);
    var baseUrl = new Uri($"https://{AppName}.scm.azurewebsites.net/");
    var requestURl = baseUrl + "api/zip/site/wwwroot";
    var httpContent = new StreamContent(stream);
    var response = client.PutAsync(requestURl, httpContent).Result;
}

Besides, you can use Microsoft.Azure.Management.Fluent to manage your Azure app service.

Fei Han
  • 26,415
  • 1
  • 30
  • 41
1

You could grab the Azure Management Libraries for .Net from Github. Support for Azure App Service is in preview as of v1.2. This enables a very intuitive syntax.

var webApp = azure.WebApps.Define(appName)
    .WithRegion(Region.USWest)
    .WithNewResourceGroup(rgName)
    .WithNewFreeAppServicePlan()
    .Create();

There are lots of code samples for both Windows and Linux flavours of Azure App Service shown here: https://github.com/Azure/azure-sdk-for-net/tree/Fluent

There are also some worthwhile reading materials on how to handle data in this kind of scenario; various possibilities and design patterns are covered at https://learn.microsoft.com/en-us/azure/sql-database/sql-database-design-patterns-multi-tenancy-saas-applications

huysmania
  • 1,054
  • 5
  • 11