我正在使用以下代码进行连接。我可以连接到其他Azure资源。
但是对于一种资源,我收到以下错误:URL和密钥正确。
{“连接尝试失败,因为连接的用户在一段时间后未正确响应,或者建立的连接失败,因为连接的主机无法响应”“
代码如下
_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();
有什么想法吗?提前如何解决此问题?
答案 0 :(得分:2)
使用c#和azure进行工作时,请始终首先了解解决方案结构文件的独特之处。这就是为什么我们在学习解决方案时从文档中构建示例。接下来,我们必须研究不同的代码块,这些代码块在执行时为解决方案整体上提供一个功能。
appsettings.json
{
"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
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...\n");
DeleteIndexIfExists(indexName, serviceClient);
Console.WriteLine("{0}", "Creating index...\n");
CreateIndex(indexName, serviceClient);
ISearchIndexClient indexClient = serviceClient.Indexes.GetClient(indexName);
Console.WriteLine("{0}", "Uploading documents...\n");
UploadDocuments(indexClient);
ISearchIndexClient indexClientForQueries = CreateSearchIndexClient(indexName, configuration);
RunQueries(indexClientForQueries);
Console.WriteLine("{0}", "Complete. Press any key to end application...\n");
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);
}}
要学习的天蓝色概念
要学习的C#概念
如何将c#代码部署到蔚蓝
您正在使用什么版本的c#?asp.net是什么?您将使用什么版本
如您所见,azure和c#具有较高的学习曲线。
幸运的是,您有堆栈溢出和文档来研究上述所有问题以及更多内容:)
关于您将如何进行故障排除...我该做什么是研究文档示例中的每个代码块并在本地运行所有代码。然后,我一次测试每个代码块。您总是以代码块来测试数据流。因此,您可以通过创建测试变量将控制台代码块的结果记录到日志中,并将该变量打印到控制台。
由于每个代码块代表一个功能或特性,因此每个测试将输出该功能或特性的通过或失败。因此,您可以设计功能,实施该设计并为新功能创建测试。