简体   繁体   中英

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

You could try the 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

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.

Alternatively, I was checking what was the underlying endpoint that was invoked by the 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.

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 .

在此处输入图像描述

And you will find operations for Account.

在此处输入图像描述

Maybe you can search it by keywords above in Github , and find this piece of code.

       //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;
        //}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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