通过代码部署Azure函数(C#) [英] Deploy Azure function from code (c#)

查看:68
本文介绍了通过代码部署Azure函数(C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将代码为字符串(在C#中)的Azure函数(按计划执行)部署到给定的Azure函数应用中?

我将使用ARM模板来部署azure基金应用程序(及其所需的全部内容)

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.

+更多上下文:部署将通过应用程序服务进行,因此,最好不要具有NuGet以外的任何依赖项.例如.我不喜欢从C#调用Azure CLI的想法.

+ 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#.

推荐答案

正如Jesse Carter所说,我们可以使用 Kudu Zip Api 可以做到这一点.我为此做一个演示.它在我这边正常工作.以下是我的详细步骤:

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:

准备工作

注册一个AD应用程序并为应用分配角色,更多详细信息,请参考

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.准备一个身份验证文件,我们可以从github 文档.

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需要发布的文件

2.Zip the need to be published file

步骤:

1.创建一个C#控制台项目

1.Create a C# console project

2.参考 Microsoft.Azure.Management.ResourceManager.Fluent Microsoft.Azure.Management.AppService.Fluent ,更多详细信息,请参阅packages.config文件部分.

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.在Program.cs文件中添加以下代码

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.从本地进行测试

5.从Azure kudu工具检查发布的结果( https://yourazurefunctionanme.scm.azurewebsites.net/)

5.Check the published result from the Azure kudu tool(https://yourazurefunctionanme.scm.azurewebsites.net/)

packages.config

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#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆