コード例 #1
0
        public async Task HandleErrors()
        {
            await using SearchResources resources = await SearchResources.CreateWithHotelsIndexAsync(this);

            Environment.SetEnvironmentVariable("SEARCH_ENDPOINT", resources.Endpoint.ToString());
            Environment.SetEnvironmentVariable("SEARCH_API_KEY", resources.PrimaryApiKey);

            #region Snippet:Azure_Search_Tests_Samples_HandleErrors
            Uri endpoint = new Uri(Environment.GetEnvironmentVariable("SEARCH_ENDPOINT"));
            SearchApiKeyCredential credential = new SearchApiKeyCredential(
                Environment.GetEnvironmentVariable("SEARCH_API_KEY"));

            // Create an invalid SearchIndexClientClient
            string            fakeIndexName = "doesnotexist";
            SearchIndexClient index         = new SearchIndexClient(endpoint, fakeIndexName, credential);
            /*@@*/ index = InstrumentClient(new SearchIndexClient(endpoint, fakeIndexName, credential, GetSearchClientOptions()));
            try
            {
                //@@ index.GetCount();
                /*@@*/ await index.GetCountAsync();
            }
            catch (RequestFailedException ex) when(ex.Status == 404)
            {
                Console.WriteLine("Index wasn't found.");
            }
            #endregion Snippet:Azure_Search_Tests_Samples_HandleErrors
        }
コード例 #2
0
        public async Task GetCountAsync()
        {
            await using SearchResources resources = await SearchResources.CreateWithHotelsIndexAsync(this);

            Environment.SetEnvironmentVariable("SEARCH_ENDPOINT", resources.Endpoint.ToString());
            Environment.SetEnvironmentVariable("SEARCH_API_KEY", resources.PrimaryApiKey);
            Environment.SetEnvironmentVariable("SEARCH_INDEX", resources.IndexName);

            #region Snippet:Azure_Search_Tests_Samples_GetCountAsync
            // Create a SearchIndexClient
            Uri endpoint = new Uri(Environment.GetEnvironmentVariable("SEARCH_ENDPOINT"));
            SearchApiKeyCredential credential = new SearchApiKeyCredential(
                Environment.GetEnvironmentVariable("SEARCH_API_KEY"));
            string            indexName = Environment.GetEnvironmentVariable("SEARCH_INDEX");
            SearchIndexClient index     = new SearchIndexClient(endpoint, indexName, credential);
            /*@@*/ index = InstrumentClient(new SearchIndexClient(endpoint, indexName, credential, GetSearchClientOptions()));

            // Get and report the number of documents in the index
            Response <long> count = await index.GetCountAsync();

            Console.WriteLine($"Search index {indexName} has {count.Value} documents.");
            #endregion Snippet:Azure_Search_Tests_Samples_GetCountAsync
        }