Deploy Azure function from code (c#)(从代码部署 Azure 函数 (c#))
问题描述
如何使用代码作为字符串(在 c# 中)将 azure 函数(按计划执行)部署到给定的 azure 函数应用程序?
我将使用 ARM 模板来部署 azure fund 应用程序(+ 所需的一切)
5.查看 Azure kudu 工具发布的结果(
packages.config
<?xml version="1.0" encoding="utf-8"?><包><package id="Microsoft.Azure.Management.AppService.Fluent" version="1.1.3" targetFramework="net452"/><package id="Microsoft.Azure.Management.Batch.Fluent" version="1.1.3" targetFramework="net452"/><package id="Microsoft.Azure.Management.Cdn.Fluent" version="1.1.3" targetFramework="net452"/><package id="Microsoft.Azure.Management.Compute.Fluent" version="1.1.3" targetFramework="net452"/><package id="Microsoft.Azure.Management.ContainerRegistry.Fluent" version="1.1.3" targetFramework="net452"/><package id="Microsoft.Azure.Management.Dns.Fluent" version="1.1.3" targetFramework="net452"/><package id="Microsoft.Azure.Management.DocumentDB.Fluent" version="1.1.3" targetFramework="net452"/><package id="Microsoft.Azure.Management.Fluent" version="1.1.3" targetFramework="net452"/><package id="Microsoft.Azure.Management.Graph.RBAC.Fluent" version="1.1.3" targetFramework="net452"/><package id="Microsoft.Azure.Management.KeyVault.Fluent" version="1.1.3" targetFramework="net452"/><package id="Microsoft.Azure.Management.Network.Fluent" version="1.1.3" targetFramework="net452"/><package id="Microsoft.Azure.Management.Redis.Fluent" version="1.1.3" targetFramework="net452"/><package id="Microsoft.Azure.Management.ResourceManager.Fluent" version="1.1.3" targetFramework="net452"/><package id="Microsoft.Azure.Management.ServiceBus.Fluent" version="1.1.3" targetFramework="net452"/><package id="Microsoft.Azure.Management.Sql.Fluent" version="1.1.3" targetFramework="net452"/><package id="Microsoft.Azure.Management.Storage.Fluent" version="1.1.3" targetFramework="net452"/><包 id="Microsoft.Azure.Management.TrafficManager.Fluent" 版本="1.1.3" targetFramework="net452"/><包 id="Microsoft.IdentityModel.Clients.ActiveDirectory" 版本="2.28.3" targetFramework="net452"/><package id="Microsoft.Rest.ClientRuntime" version="2.3.8" targetFramework="net452"/><package id="Microsoft.Rest.ClientRuntime.Azure" version="3.3.8" targetFramework="net452"/><package id="Microsoft.Rest.ClientRuntime.Azure.Authentication" version="2.3.0" targetFramework="net452"/><package id="Newtonsoft.Json" version="9.0.1" targetFramework="net452"/></包>
How to deploy azure function (executed on schedule) with code as string (in c#) to given azure functions app?
I am going to use ARM template to deploy azure fund app (+ all it needs) https://github.com/Azure/azure-quickstart-templates/tree/master/101-function-app-create-dynamic, which can be deployed via code;
But I don't see ways to deploy function to function app via code.
+ A little more context: deployment is going to happen from an app service, so it's preferred not to have any dependencies beyond NuGet. E.g. I don't like the idea to call azure cli from c#.
As Jesse Carter mentioned that we could use Kudu Zip Api to do that. I do a demo for that. It works correctly on my side. The following is my detail steps:
Preparation:
Registry an AD application and assign role to applcation, more details please refer to Azure official tutorials. After that we can get tenantId, appId, secretKey from the Azure Portal.
1.Prepare an authentication file, we could get more information from github document.
subscription=########-####-####-####-############
client=########-####-####-####-############
tenant=########-####-####-####-############
managementURI=https://management.core.windows.net/
baseURL=https://management.azure.com/
authURL=https://login.windows.net/
graphURL=https://graph.windows.net/
2.Zip the need to be published file
Steps:
1.Create a C# console project
2.Reference the Microsoft.Azure.Management.ResourceManager.Fluent and Microsoft.Azure.Management.AppService.Fluent, more detail info please refer to the packages.config file section.
3.Add the following code in the Program.cs file
var credentials = SdkContext.AzureCredentialsFactory.FromFile(@"authentication file path");
var azure = Azure
.Configure()
.WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
.Authenticate(credentials)
.WithDefaultSubscription();
var webFunctionAppName = "azure function name";
var webFunctionApp = azure.AppServices.FunctionApps.List().Where(x => x.Name.Equals(webFunctionAppName))?.First();
var ftpUsername = azure.AppServices.FunctionApps.GetById(webFunctionApp.Id).GetPublishingProfile().FtpUsername;
var username = ftpUsername.Split('\').ToList()[1];
var password = azure.AppServices.FunctionApps.GetById(webFunctionApp.Id).GetPublishingProfile().FtpPassword;
var base64Auth = Convert.ToBase64String(Encoding.Default.GetBytes($"{username}:{password}"));
var file = File.ReadAllBytes(@"zip file path");
MemoryStream stream = new MemoryStream(file);
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Add("Authorization", "Basic " + base64Auth);
var baseUrl = new Uri($"https://{webFunctionAppName}.scm.azurewebsites.net/");
var requestURl = baseUrl+ "api/zip/site/wwwroot";
var httpContent = new StreamContent(stream);
var response = client.PutAsync(requestURl, httpContent).Result;
}
4.Test from the local
5.Check the published result from the Azure kudu tool(https://yourazurefunctionanme.scm.azurewebsites.net/)
packages.config
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.Azure.Management.AppService.Fluent" version="1.1.3" targetFramework="net452" />
<package id="Microsoft.Azure.Management.Batch.Fluent" version="1.1.3" targetFramework="net452" />
<package id="Microsoft.Azure.Management.Cdn.Fluent" version="1.1.3" targetFramework="net452" />
<package id="Microsoft.Azure.Management.Compute.Fluent" version="1.1.3" targetFramework="net452" />
<package id="Microsoft.Azure.Management.ContainerRegistry.Fluent" version="1.1.3" targetFramework="net452" />
<package id="Microsoft.Azure.Management.Dns.Fluent" version="1.1.3" targetFramework="net452" />
<package id="Microsoft.Azure.Management.DocumentDB.Fluent" version="1.1.3" targetFramework="net452" />
<package id="Microsoft.Azure.Management.Fluent" version="1.1.3" targetFramework="net452" />
<package id="Microsoft.Azure.Management.Graph.RBAC.Fluent" version="1.1.3" targetFramework="net452" />
<package id="Microsoft.Azure.Management.KeyVault.Fluent" version="1.1.3" targetFramework="net452" />
<package id="Microsoft.Azure.Management.Network.Fluent" version="1.1.3" targetFramework="net452" />
<package id="Microsoft.Azure.Management.Redis.Fluent" version="1.1.3" targetFramework="net452" />
<package id="Microsoft.Azure.Management.ResourceManager.Fluent" version="1.1.3" targetFramework="net452" />
<package id="Microsoft.Azure.Management.ServiceBus.Fluent" version="1.1.3" targetFramework="net452" />
<package id="Microsoft.Azure.Management.Sql.Fluent" version="1.1.3" targetFramework="net452" />
<package id="Microsoft.Azure.Management.Storage.Fluent" version="1.1.3" targetFramework="net452" />
<package id="Microsoft.Azure.Management.TrafficManager.Fluent" version="1.1.3" targetFramework="net452" />
<package id="Microsoft.IdentityModel.Clients.ActiveDirectory" version="2.28.3" targetFramework="net452" />
<package id="Microsoft.Rest.ClientRuntime" version="2.3.8" targetFramework="net452" />
<package id="Microsoft.Rest.ClientRuntime.Azure" version="3.3.8" targetFramework="net452" />
<package id="Microsoft.Rest.ClientRuntime.Azure.Authentication" version="2.3.0" targetFramework="net452" />
<package id="Newtonsoft.Json" version="9.0.1" targetFramework="net452" />
</packages>
这篇关于从代码部署 Azure 函数 (c#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:从代码部署 Azure 函数 (c#)


- MoreLinq maxBy vs LINQ max + where 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- 输入按键事件处理程序 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01