简体   繁体   English

在 ASP.NET 应用程序中使用扩展 - .NET 6

[英]Using Extension in ASP.NET application - .NET 6

I'm trying to get NEST working in an ASP.NET application.我试图让 NEST 在 ASP.NET 应用程序中工作。 I'm following the guide https://blexin.com/en/blog-en/how-to-integrate-elasticsearch-in-asp-net-core/#highlighter_541789 .我正在遵循指南https://blexin.com/en/blog-en/how-to-integrate-elasticsearch-in-asp-net-core/#highlighter_541789 I'm trying to figure out how to call AddElasticsearch correctly on the builder.我试图弄清楚如何在构建器上正确调用AddElasticsearch I've tried a few ways but keep getting compiler complaints.我尝试了几种方法,但不断收到编译器投诉。

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 

Instead of options => options.configuration which is just a random guess with no meaning, use builder.Configuration .而不是options => options.configuration这只是一个没有意义的随机猜测,使用builder.Configuration

To be honest, I think that this new way of having a main method and not actually having a main method is hiding so much that it doesn't make it "easier", it actually makes it harder to understand what is actually happening.老实说,我认为这种拥有main方法而不实际拥有 main 方法的新方法隐藏了太多,以至于它并没有使它“更容易”,实际上它更难理解实际发生的事情。 Because it's all hidden.因为这一切都是隐藏的。 Anyway, that is what you need to do.无论如何,这就是你需要做的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM