Reputation: 34038
I have this error:
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
The JSON file is present in the source code directory, however, it looks like it needs to find the file somewhere on the binary folder.
Before running the app with dotnet run, I executed dotnet build, assuming it would copy the JSON file to the output build folder but it doesn't.
The code is like this:
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();
}
}
}
Upvotes: 3
Views: 2364
Reputation: 1076
I've been looking for an answer to this question and most of the posts I've found reference the project.json file which has now been deprecated in favour of the .csproj
<Project>
<ItemGroup>
<None Include="appsettings.json" CopyToOutputDirectory="Always" />
</ItemGroup>
</Project>
Upvotes: 2
Reputation: 2496
You can add an ItemGroup
to your csproj
file, it would look something like this:
<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
Upvotes: 6