c# - 如何在 Azure C# SDK 中使用 WebAppsOperationsExtensions 和 IWebAppsOperations
问题描述
这是我尝试使用的方法 WebAppsOperationsExtensions.BackupAsync(IWebAppsOperations, String, String, BackupRequestInner, CancellationToken)
但是,我不知道如何获取 IWebAppsOperations 对象,因为 Microsoft 实现不是公开的。
文档没有帮助:
解决方案
祝贺你的第一个帖子!
我已经使用 Azure 多年,但对 Azure SDK .NET 的工作并不多。因此,我很高兴深入挖掘您的问题并体验挑战。
我按照以下步骤为您提供答案:
第 1 步- 详细浏览文档
打开你的链接。我观察到的第一件事是WebAppsOperationsExtensions.BackupAsync是一种扩展方法。MSDN 标题将其表示为WebAppsOperationsExtensions.BackupAsync(IWebAppsOperations, String, String, BackupRequestInner, CancellationToken) 方法,但请密切注意文章中第一个参数代码示例中的“this”定义:
公共静态 System.Threading.Tasks.Task<Microsoft.Azure.Management.AppService.Fluent.Models.BackupItemInner> BackupAsync(此 Microsoft.Azure.Management.AppService.Fluent.IWebAppsOperations 操作,字符串 resourceGroupName,字符串名称,Microsoft.Azure。 Management.AppService.Fluent.Models.BackupRequestInner 请求,System.Threading.CancellationToken cancelToken = 默认);
所以这个方法是一种扩展方法。
第 2 步- 探索代码示例
滚动导航树菜单以查找分支中最顶部的文章:https ://docs.microsoft.com/en-us/dotnet/api/overview/azure/appservice?view=azure-dotnet
在用法上找到以下代码示例:
/* Include these "using" directives...
using Microsoft.Azure.Management.ResourceManager.Fluent.Core;
using Microsoft.Azure.Management.AppService.Fluent;
*/
IWebApp app1 = azure.WebApps
.Define("MyUniqueWebAddress")
.WithRegion(Region.USWest)
.WithNewResourceGroup("MyResourceGroup")
.WithNewWindowsPlan(PricingTier.StandardS1)
.Create();
第 3 步- 原型
打开 Visual Studio 2019 并尝试通过阅读有用的文章找到答案:https ://dotnetdevlife.wordpress.com/2020/01/16/quickstart-azure-management-libraries-for-net/comment-page-1/
总而言之,通过调用 BackupAsync 作为 azure.WebApps.Inner 的扩展方法,我能够将 IWebAppsOperations 传递到 WebAppsOperationsExtensions.BackupAsync。为此,我需要添加额外的 nuget 包(请参阅屏幕截图中的 using 语句以获取列表)。
注意:我没有为您提供 BackupAsync 的完整实现,因为这不是您的问题。
此致!