Azure连接尝试失败

Azure connection attempt failed

我正在使用以下代码进行连接。我可以连接到其他Azure资源。

但是对于一种资源,我收到以下错误:URL和密钥正确。

{"连接尝试失败,因为连接的一方在一段时间后未正确响应,或者建立的连接失败,因为连接的主机未能响应"}

代码如下

1
2
3
4
5
6
7
 _searchClient = new SearchServiceClient(searchServiceName, new
  SearchCredentials(apiKey));
    _httpClient.DefaultRequestHeaders.Add("api-key", apiKey);
    _searchServiceEndpoint = String.Format("https://{0}.{1}",
  searchServiceName, _searchClient.SearchDnsSuffix);

    bool result = RunAsync().GetAwaiter().GetResult();

有什么想法吗?提前吗?如何解决此问题?


  • 我将展示如何在C#中完成此操作
  • 您将需要一个appsettings.json
  • 您将在program.cs文件中需要此代码
  • 该文档中的示例中还有很多其他文件
    您可能需要使用,学习和编辑您的用例
  • 在使用c#和azure进行工作时,请始终首先了解解决方案结构文件的独特之处。这就是为什么我们在学习解决方案时从文档中构建示例。接下来,我们必须研究不同的代码块,这些代码块在执行时为解决方案整体提供一个功能。

    appsettings.json

    1
    2
    3
    4
    5
    6
    {
    "SearchServiceName":"[Put your search service name here]",
    "SearchIndexName":"hotels",
    "SearchServiceAdminApiKey":"[Put your primary or secondary Admin API key here]",
    "SearchServiceQueryApiKey":"[Put your primary or secondary Query API key here]"
    }

    Program.cs

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
       namespace AzureSearch.SDKHowTo
    {
        using System;
        using System.Linq;
        using System.Threading;
        using Microsoft.Azure.Search;
        using Microsoft.Azure.Search.Models;
        using Microsoft.Extensions.Configuration;
        using Microsoft.Spatial;
         // This sample shows how to delete, create, upload documents and query an index
        static void Main(string[] args)
        {
            IConfigurationBuilder builder = new ConfigurationBuilder().AddJsonFile("appsettings.json");
            IConfigurationRoot configuration = builder.Build();

            SearchServiceClient serviceClient = CreateSearchServiceClient(configuration);

            string indexName = configuration["SearchIndexName"];

            Console.WriteLine("{0}","Deleting index...\
    ");
            DeleteIndexIfExists(indexName, serviceClient);

            Console.WriteLine("{0}","Creating index...\
    ");
            CreateIndex(indexName, serviceClient);

            ISearchIndexClient indexClient = serviceClient.Indexes.GetClient(indexName);

            Console.WriteLine("{0}","Uploading documents...\
    ");
            UploadDocuments(indexClient);

            ISearchIndexClient indexClientForQueries = CreateSearchIndexClient(indexName, configuration);

            RunQueries(indexClientForQueries);

            Console.WriteLine("{0}","Complete.  Press any key to end application...\
    ");
            Console.ReadKey();
        }
     private static SearchServiceClient CreateSearchServiceClient(IConfigurationRoot configuration)
        {
            string searchServiceName = configuration["SearchServiceName"];
            string adminApiKey = configuration["SearchServiceAdminApiKey"];

            SearchServiceClient serviceClient = new SearchServiceClient(searchServiceName, new SearchCredentials(adminApiKey));
            return serviceClient;
        }

        private static SearchIndexClient CreateSearchIndexClient(string indexName, IConfigurationRoot configuration)
        {
            string searchServiceName = configuration["SearchServiceName"];
            string queryApiKey = configuration["SearchServiceQueryApiKey"];

            SearchIndexClient indexClient = new SearchIndexClient(searchServiceName, indexName, new SearchCredentials(queryApiKey));
            return indexClient;
        }

        private static void DeleteIndexIfExists(string indexName, SearchServiceClient serviceClient)
        {
            if (serviceClient.Indexes.Exists(indexName))
            {
                serviceClient.Indexes.Delete(indexName);
            }
        }

        private static void CreateIndex(string indexName, SearchServiceClient serviceClient)
        {
            var definition = new Index()
            {
                Name = indexName,
                Fields = FieldBuilder.BuildForType<Hotel>()
            };

            serviceClient.Indexes.Create(definition);
        }}

    要学习的天蓝色概念

    • 我们如何以及为什么创建天青客户
    • 我们为什么使用appsettings.json
    • 天蓝色搜索解决方案的一些示例文件结构是什么
    • 您想使用哪种编码方式来构建该解决方案
    • 你想使用天蓝色的sdk吗
    • 如何查找和创建API密钥

    要学习的C#概念

    • 什么是界面以及如何使用它
    • 如何将文件结构中的一个文件导入另一个文件
    • 主要功能如何运作
    • 如何在函数中调用变量
    • 如何使用函数调用函数
    • 如何编写服务器端代码与客户端代码
    • 如何将c#代码部署到天蓝色

    • 您正在使用什么版本的c#版本的asp.net,以及您将使用什么版本的

    • 什么是asp.net核心以及您将使用哪个版本

    如您所见,azure和c#具有较高的学习曲线。

    幸运的是,您有堆栈溢出和文档来研究上述所有问题以及更多其他内容:)

    关于您将如何进行故障排除...我该怎么做是研究文档示例中的每个代码块并在本地运行所有代码。然后,我一次测试每个代码块。您总是以代码块来测试数据流。因此,您可以通过创建测试变量来控制台记录代码块的结果,并将该变量打印到控制台。

    由于每个代码块代表一个功能或特性,因此每个测试将输出该功能或特性的通过或失败。因此,您可以设计功能,实施该设计并为新功能创建测试。