简体   繁体   English

Azure 搜索:如何使用 C# 以编程方式创建和删除帐户

[英]Azure Search: How to create and delete an account programmatically using C#

I want to create a Cognitive Search Service account (with Basic configuration) programmatically using C# and delete it on demand.我想使用 C# 以编程方式创建一个Cognitive Search Service帐户(具有基本配置)并按需删除它。 Is there any documentation out there for doing this?是否有任何文档可以执行此操作?

I was able to find documentation for creating and deleting indexes, but I need to go one level higher and create and delete the account instead.我能够找到创建和删除索引的文档,但我需要更高一级的 go 并创建和删除该帐户。 I need to do this to cut costs.我需要这样做以降低成本。 For example, when I'm not actively dev testing, I need to delete the service account to avoid charges.例如,当我不积极进行开发测试时,我需要删除服务帐户以避免收费。

You can manage your Azure resources using ARM templates through the REST API. To get the ARM template for the Cognitive Search Service you can create a sample service and downloading the template from Export template under Settings on Azure portal.您可以通过 REST API 使用ARM 模板管理您的 Azure 资源。要获取认知搜索服务的 ARM 模板,您可以创建示例服务并从 88358778 门户网站上Settings下的Export template下载模板。

You could try the Azure CLI.您可以尝试 Azure CLI。 This has all endpoints for creating and deleting the search service.这具有用于创建和删除搜索服务的所有端点。

Reference: https://learn.microsoft.com/en-us/cli/azure/search/service?view=azure-cli-latest参考: https://learn.microsoft.com/en-us/cli/azure/search/service?view=azure-cli-latest

az login -u <username> -p <password>
#SKU List --> https://learn.microsoft.com/en-us/dotnet/api/microsoft.azure.management.search.models.skuname?view=azure-dotnet 
az search service create --name "<SEARCH SERVICE NAME>"  --resource-group "<RESOURCE GROUPNAME>" --sku "<SKU ENUM>" --subscription "YOUR SUBSCRIPTION ID"

Script can be coupled with a C# application and invoking it on demand.脚本可以与 C# 应用程序耦合并按需调用它。

Alternatively, I was checking what was the underlying endpoint that was invoked by the Azure CLI.或者,我正在检查 Azure CLI 调用的底层端点是什么。 You could refer my post for more information on the 'How To'.您可以参考我的帖子以获取有关“操作方法”的更多信息。

Request:要求: 在此处输入图像描述

Request Body:请求正文: 在此处输入图像描述

You can also try hitting this endpoint directly from your C# Application.您也可以尝试直接从您的 C# 应用程序访问此端点。

The same thing is applicable for the Delete as well.同样的事情也适用于删除。

az search service delete --name
                         --resource-group
                         [--subscription]
                         [--yes]

在此处输入图像描述

I haven't used Cognitive and C#, but you can find Azure Resource Management Package From here .我没用过Cognitive和 C#,但是你可以从这里找到 Azure Resource Management Package。

在此处输入图像描述

And you will find operations for Account.您会发现帐户的操作。

在此处输入图像描述

Maybe you can search it by keywords above in Github , and find this piece of code.或许你可以在Github中通过上面的关键词搜索,找到这段代码。

       //public static CognitiveServicesAccount CreateAndValidateAccountWithOnlyRequiredParameters(CognitiveServicesManagementClient cognitiveServicesMgmtClient, string rgName, string skuName, string accountType = Kind.Recommendations, string location = null)
        //{
        //    // Create account with only required params
        //    var accountName = TestUtilities.GenerateName("csa");
        //    var parameters = new CognitiveServicesAccountCreateParameters
        //    {
        //        Sku = new Microsoft.Azure.Management.CognitiveServices.Models.Sku { Name = skuName },
        //        Kind = accountType,
        //        Location = location ?? DefaultLocation,
        //        Properties = new object(),
        //    };
        //    var account = cognitiveServicesMgmtClient.CognitiveServicesAccounts.Create(rgName, accountName, parameters);
        //    VerifyAccountProperties(account, false, accountType, skuName, location ?? DefaultLocation);

        //    return account;
        //}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM