我遇到了这个错误:
Unhandled Exception: System.IO.FileNotFoundException: The configuration file 'appsettings.json' was not found and is not optional. The physical path is '/ONEDRIVE/OneDrive - SE/LuloTV/Azure Search/DemoSearchIndexer/bin/Debug/netcoreapp2.2/appsettings.json'.
at Microsoft.Extensions.Configuration.FileConfigurationProvider.Load(Boolean reload)
at Microsoft.Extensions.Configuration.ConfigurationRoot..ctor(IList`1 providers)
at Microsoft.Extensions.Configuration.ConfigurationBuilder.Build()
at DemoSearchIndexer.Program.Main(String[] args) in /ONEDRIVE/OneDrive - SE/LuloTV/Azure Search/DemoSearchIndexer/Program.cs:line 16
JSON文件位于源代码目录中,但似乎需要在二进制文件夹中找到该文件。
在使用dotnet run运行应用程序之前,我执行了dotnet build,希望它会将JSON文件复制到输出构建文件夹中,但实际上并没有这样做。
代码如下:
using System;
using System.Linq;
using System.Threading;
using Microsoft.Azure.Search;
using Microsoft.Azure.Search.Models;
using Microsoft.Extensions.Configuration;
using Microsoft.Spatial;
namespace DemoSearchIndexer
{
class Program
{
static void Main(string[] args)
{
IConfigurationBuilder builder = new ConfigurationBuilder().AddJsonFile("appsettings.json");
IConfigurationRoot configuration = builder.Build();
SearchServiceClient serviceClient = CreateSearchServiceClient(configuration);
string indexName = configuration["SearchIndexName"];
ISearchIndexClient indexClientForQueries = CreateSearchIndexClient(indexName, configuration);
RunQueries(indexClientForQueries);
Console.WriteLine("{0}", "Complete. Press any key to end application...\n");
Console.ReadKey();
}
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 SearchServiceClient CreateSearchServiceClient(IConfigurationRoot configuration)
{
string searchServiceName = configuration["SearchServiceName"];
string adminApiKey = configuration["SearchServiceAdminApiKey"];
SearchServiceClient serviceClient = new SearchServiceClient(searchServiceName, new SearchCredentials(adminApiKey));
return serviceClient;
}
private static void RunQueries(ISearchIndexClient indexClient)
{
SearchParameters parameters;
DocumentSearchResult<Role> results;
Console.WriteLine("Search the entire index for the term 'Administrador' and return only the RoleName field:\n");
parameters = new SearchParameters()
{
Select = new[] { "RoleName" }
};
results = indexClient.Documents.Search<Role>("Administrator", parameters);
WriteDocuments(results);
Console.Write("Apply a filter to the index to find roles with a rolename:Usuario, ");
Console.WriteLine("and return the hotelId and description:\n");
parameters = new SearchParameters()
{
Filter = "RoleName:'Partner')",
Select = new[] { "Id", "RoleName" }
};
results = indexClient.Documents.Search<Role>("*", parameters);
WriteDocuments(results);
Console.Write("Search the entire index, order by a specific field (Id) ");
Console.Write("in descending order, take the top two results, and show only rolename and id");
parameters = new SearchParameters()
{
OrderBy = new[] { "Id desc" },
Select = new[] { "Id", "RoleName" },
Top = 2
};
results = indexClient.Documents.Search<Role>("*", parameters);
WriteDocuments(results);
Console.WriteLine("Search the role names for the term 'Superadministrator':\n");
parameters = new SearchParameters()
{
SearchFields = new[] { "RoleName" }
};
results = indexClient.Documents.Search<Role>("Superadministrator", parameters);
WriteDocuments(results);
}
private static void WriteDocuments(DocumentSearchResult<Role> searchResults)
{
foreach (SearchResult<Role> result in searchResults.Results)
{
Console.WriteLine(result.Document);
}
Console.WriteLine();
}
}
}