问题
I would like to write a web app that allows users to write C# scripts and execute them using Azure Functions.
I checked Azure SDK documentation and didn't find any nuget packages for managing Function Apps.
Is there a way I can:
- retrieve list of available azure functions
- deploy azure function
- update azure function
- delete azure function
using Azure SDK? If not, what would be another way to do it?
Update on Jul 8, 2019
I found List and Delete functions in Azure SDK (Microsoft.Azure.Management.WebSites):
List: Msdn Documentation
Delete Msdn Documentation
I tested them and they work. The problem is with Create method _CreateFunctionWithHttpMessagesAsync (Msdn Documentation)
It is not clear which parameters should be passed for it to work. Currently I call it like this:
var response = await webClient.WebApps.CreateFunctionWithHttpMessagesAsync(ResourceGroupName, FunctionAppName, functionName, new FunctionEnvelope());
In about 10-20s it returns error: "Microsoft.Azure.Management.WebSites.Models.DefaultErrorResponseException : Operation returned an invalid status code 'InternalServerError'"
I think it is related to empty FunctionEnvelope. I tried passing various values, but none of them worked.
回答1:
AFAIK there is no SDK's available. But there are REST API's
which let you perform all the above operations.
List Functions
Delete Function
For updating and Deployment you can make use of the zip deployment
for Azure Functions.
Generate the FunctionApp.zip
using the msbuild command pointing to your csproj
->
/p:DeployOnBuild=true /p:DeployTarget=Package;CreatePackageOnPublish=true
The above will generate a zip file which can be used in the later part.
Now 2nd step is to obtain the Publish credentials
using this api, if you get the response it will in the below class format
public class GetPublishCredentials
{
public string Id { get; set; }
public string Name { get; set; }
public string Type { get; set; }
public string Location { get; set; }
public Properties Properties { get; set; }
}
public class Properties
{
public string Name { get; set; }
public string PublishingUserName { get; set; }
public string PublishingPassword { get; set; }
public object PublishingPasswordHash { get; set; }
public object PublishingPasswordHashSalt { get; set; }
public object Metadata { get; set; }
public bool IsDeleted { get; set; }
public string ScmUri { get; set; }
}
After obtaining the credentials, follow the below piece of code to deploy
or update
your Azure Functions
var base64Auth = Convert.ToBase64String(Encoding.Default.GetBytes
($"{functionCredentials.Properties.PublishingUserName}:{functionCredentials.Properties.PublishingPassword}"));
var stream = new MemoryStream(File.ReadAllBytes("zip file of azure function"));
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Add("Authorization", "Basic " + base64Auth);
var apiUrl = "https://" + parameters.FunctionAppName + ".scm.azurewebsites.net/api/zip/site/wwwroot";
var httpContent = new StreamContent(stream);
client.PutAsync(apiUrl, httpContent).Result;
}
Now your functionapp should be deployed.
来源:https://stackoverflow.com/questions/56794077/is-there-a-way-to-manage-azure-function-apps-using-azure-sdk