在 ASP.NET 应用程序中使用扩展 - .NET 6
Using Extension in ASP.NET application - .NET 6
我正在尝试让 NEST 在 ASP.NET 应用程序中运行。我正在遵循指南 https://blexin.com/en/blog-en/how-to-integrate-elasticsearch-in-asp-net-core/#highlighter_541789。我正在尝试弄清楚如何在生成器上正确调用 AddElasticsearch
。我尝试了几种方法,但不断收到编译器投诉。
Extensions.cs
public static class Extensions
{
public static void AddElasticsearch(this IServiceCollection services, IConfiguration configuration)
{
var url = configuration["elasticsearch:url"];
var defaultIndex = configuration["elasticsearch:index"];
var settings = new ConnectionSettings(new Uri(url)).DefaultIndex(defaultIndex);
AddDefaultMappings(settings);
var client = new ElasticClient(settings);
services.AddSingleton(client);
CreateIndex<LegiscanModelBill>(client, defaultIndex);
}
private static void AddDefaultMappings(ConnectionSettings settings) => settings.DefaultMappingFor<LegiscanModelBill>(m => m);
// private static void CreateIndex<T>(IElasticClient client, string indexName)
// {
// var createIndexResponse = client.Indices.Create(indexName,
// index => index.Map<LegiscanModelBill>(x => x.AutoMap())
// );
// }
private static void CreateIndex<T>(ElasticClient client, string indexName) where T : class
{
client.Indices.Create($"{indexName}_{typeof(T).ToString}", i => i.Map<T>(m => m.AutoMap()));
}
}
Program.cs
var builder = WebApplication.CreateBuilder(args);
builder.Services
.AddElasticsearch(options => options.configuration)
.AddGraphQLServer()
.AddQueryType<Query>();
var app = builder.Build();
app.MapGraphQL();
app.Run();
compiler error
Cannot convert lambda expression to type 'IConfiguration' because it is not a delegate type
而不是 options => options.configuration
这只是一个没有意义的随机猜测,使用 builder.Configuration
.
老实说,我认为这种拥有 main
方法而不是真正拥有 main 方法的新方法隐藏了太多东西,以至于它并没有让它变得“更容易”,它实际上让它变得“更容易” 更难 理解实际发生的事情。因为都是隐藏的。无论如何,这就是你需要做的。
我正在尝试让 NEST 在 ASP.NET 应用程序中运行。我正在遵循指南 https://blexin.com/en/blog-en/how-to-integrate-elasticsearch-in-asp-net-core/#highlighter_541789。我正在尝试弄清楚如何在生成器上正确调用 AddElasticsearch
。我尝试了几种方法,但不断收到编译器投诉。
Extensions.cs
public static class Extensions
{
public static void AddElasticsearch(this IServiceCollection services, IConfiguration configuration)
{
var url = configuration["elasticsearch:url"];
var defaultIndex = configuration["elasticsearch:index"];
var settings = new ConnectionSettings(new Uri(url)).DefaultIndex(defaultIndex);
AddDefaultMappings(settings);
var client = new ElasticClient(settings);
services.AddSingleton(client);
CreateIndex<LegiscanModelBill>(client, defaultIndex);
}
private static void AddDefaultMappings(ConnectionSettings settings) => settings.DefaultMappingFor<LegiscanModelBill>(m => m);
// private static void CreateIndex<T>(IElasticClient client, string indexName)
// {
// var createIndexResponse = client.Indices.Create(indexName,
// index => index.Map<LegiscanModelBill>(x => x.AutoMap())
// );
// }
private static void CreateIndex<T>(ElasticClient client, string indexName) where T : class
{
client.Indices.Create($"{indexName}_{typeof(T).ToString}", i => i.Map<T>(m => m.AutoMap()));
}
}
Program.cs
var builder = WebApplication.CreateBuilder(args);
builder.Services
.AddElasticsearch(options => options.configuration)
.AddGraphQLServer()
.AddQueryType<Query>();
var app = builder.Build();
app.MapGraphQL();
app.Run();
compiler error
Cannot convert lambda expression to type 'IConfiguration' because it is not a delegate type
而不是 options => options.configuration
这只是一个没有意义的随机猜测,使用 builder.Configuration
.
老实说,我认为这种拥有 main
方法而不是真正拥有 main 方法的新方法隐藏了太多东西,以至于它并没有让它变得“更容易”,它实际上让它变得“更容易” 更难 理解实际发生的事情。因为都是隐藏的。无论如何,这就是你需要做的。