bdristan
bdristan

Reputation: 1070

How to deploy an app to azure using ARM and C#?

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.

Upvotes: 1

Views: 587

Answers (2)

Fei Han
Fei Han

Reputation: 27793

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.

Upvotes: 2

huysmania
huysmania

Reputation: 1064

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

Upvotes: 1

Related Questions