我遇到此错误:
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文件复制到输出build文件夹中,但是没有。
代码如下:
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();
}
}
}
答案 0 :(得分:3)
您可以将ItemGroup
添加到csproj
文件中,看起来像这样:
<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
答案 1 :(得分:1)
我一直在寻找这个问题的答案,而我发现的大多数帖子都引用了project.json文件,该文件现已不推荐使用.csproj
<Project>
<ItemGroup>
<None Include="appsettings.json" CopyToOutputDirectory="Always" />
</ItemGroup>
</Project>