Skip to content

Microsoft.AspNetCore.Hosting.ListenOptionsHttpsExtensions without reflection ? #31561

Closed as not planned

Description

@LLT21

I have tried to compile my .NET 5 bare http server project with Native AOT without reflection. This is possible for a normal http connection, but not for a https connection because in Microsoft.AspNetCore.Hosting.ListenOptionsHttpsExtensions the line of code:

var loggerFactory = listenOptions.KestrelServerOptions?.ApplicationServices.GetRequiredService<ILoggerFactory>() ?? NullLoggerFactory.Instance;

that appears twice in this class requires reflection. Could it be changed to:

var loggerFactory = listenOptions.KestrelServerOptions?.ApplicationServices?.GetRequiredService<ILoggerFactory>() ?? NullLoggerFactory.Instance;

so that it has the ? after ApplicationServices. In this way I believe the reflection can be avoided.

Activity

  1. davidfowl commented on Apr 6, 2021

    @davidfowl
    Member

    This must be the tip of the iceberg. We can change small things if people send pull requests but without a way to stop future regressions with tests it'll keep breaking in the future

  2. BrennanConroy commented on Apr 7, 2021

    @BrennanConroy
    Member

    Like David said, until we have AOT testing we wont be able to make any significant improvements for AOT environments.

    If you would like to send a PR we can accept that, otherwise we wont be fixing this issue specifically right now.

    Also, what was the actual error you were getting? A nullref?

  3. added
    help wantedUp for grabs. We would accept a PR to help resolve this issue
    on Apr 7, 2021
  4. added this to the Backlog milestone on Apr 7, 2021
  5. LLT21 commented on Apr 8, 2021

    @LLT21
    Author
  6. davidfowl commented on Apr 9, 2021

    @davidfowl
    Member

    I don't think it will.

  7. LLT21 commented on Apr 11, 2021

    @LLT21
    Author
  8. davidfowl commented on Apr 11, 2021

    @davidfowl
    Member

    Instead of doing this:

    kestrelServerOptions.ApplicationServices = new ServiceCollection()
                                                             .AddLogging()
                                                             .BuildServiceProvider();
    

    Implement a custom IServiceProvider that does this:

    private class ServiceProvider : IServiceProvider
            {
                public object GetService(Type serviceType)
                {
                    if (serviceType == typeof(ILoggerFactory))
                    {
                        return NullLoggerFactory.Instance;
                    }
    
                    return null;
                }
            }
  9. LLT21 commented on Apr 11, 2021

    @LLT21
    Author
  10. davidfowl commented on Apr 11, 2021

    @davidfowl
    Member

    Will this avoid then using the dependency injection ? Because that seems to be the problem to natively compile without reflection.

    Yes, you're basically implementing your own IServiceProvider and it can do whatever you want. Like manually create objects.

  11. LLT21 commented on Apr 11, 2021

    @LLT21
    Author
  12. davidfowl commented on Apr 11, 2021

    @davidfowl
    Member

    You can use the HttpContext directly in your application as well. You might want to use something like https://github.com/benaadams/Ben.Http which wraps these Kestrel APIs and lets you create the server directly.

    cc @benaadams you might need to test https with your server,

  13. 36 remaining items

  14. roji commented on Jun 3, 2021

    @roji
    Member

    @LLT21 thanks for this super interesting work.

    with ODBC as an interface to PostgreSQL

    Am curious... Have you given Npgsql a try instead of ODBC? There's definitely some reflection and runtime codegen in current released versions which could be problematic, but I've worked on removing those for 6.0 (see npgsql/npgsql#3300); Npgsql 6.0.0-preview4 already includes these changes.

  15. LLT21 commented on Jun 3, 2021

    @LLT21
    Author

    @roji Thanks for the feedback. I was not aware of the work in progress on Npgsql regarding this topic. Otherwise it could have saved me some sleepless odbc nights. Now that the implementation is there, I would like to first let the odbc test run and once these results are available, then compare with Npgsql. If performance is similar or better with Npgsql, I would certiainly favor the latter as it is much easier to implement. However, at the moment I am a bit confused how to proceed with the TechEmpower Benchmarks. According to this link TechEmpower/FrameworkBenchmarks#6614, a maintainer has to approve the running of workflows for first-time contributors (which I am). I cannot find a list of maintainers or how to get in contact. Do you have any idea ?

  16. MichalStrehovsky commented on Jun 4, 2021

    @MichalStrehovsky
    Member

    The TechEmpower people are maintainers. I don't have a separate channel to them - not sure if anyone on this issue has. They look at these things but sometimes it takes weeks to add a new thing (clicking through other PR history). Updates get merged faster.

  17. LLT21 commented on Jun 4, 2021

    @LLT21
    Author

    @MichalStrehovsky The flow has started, so I am happy it goes this fast. Thanks for your feedback.

  18. LLT21 commented on Jun 4, 2021

    @LLT21
    Author

    @MichalStrehovsky One more question Michal: could you have a quick look at the project settings in https://github.com/LLT21/FrameworkBenchmarks/blob/appmpowerv1/frameworks/CSharp/appmpower/src/appMpower.csproj whether these are all optimal for fastest execution ?
    @roji Next week I will try whether I can convert and compile with disabled reflection and the version 6 npgsql

  19. roji commented on Jun 4, 2021

    @roji
    Member

    Next week I will try whether I can convert and compile with disabled reflection and the version 6 npgsql

    @LLT21 great. If you still run into Npgsql issues, we can try to iterate quickly to get to a point where it works.

  20. MichalStrehovsky commented on Jun 5, 2021

    @MichalStrehovsky
    Member

    whether these are all optimal for fastest execution ?

    Yup, these all look good! Looking forward to see the results!

  21. roji commented on Jun 6, 2021

    @roji
    Member

    @LLT21 if and when you get around to looking at Npgsql, check out npgsql/npgsql#3813 (comment). We've also added some tasks to npgsql/npgsql#3300, and there's a pretty good idea of what needs to be done (thanks to @NinoFloris).

  22. LLT21 commented on Jun 8, 2021

    @LLT21
    Author

    @roji I added today a repository https://github.com/LLT21/appmpower-npgsql with therein a class RawDbNpgsql. In the HttpApplication class there are conditional compilation statements for Npgsql or Odbc. In the RawDbNpgsql class I have also foreseen some Console.WriteLine statements to see up to where the native compilation goes. At the moment it ends before the creation of the new NpgsqlConnection, but I noticed in your repository you still did some recent changes I have not integrated yet. Feel free to test with the https://github.com/LLT21/appmpower-npgsql repository if you want.

  23. roji commented on Jun 8, 2021

    @roji
    Member

    Yeah, we're working on it :) I'll ping you once we have something which should work.

  24. davidfowl commented on Oct 20, 2022

    @davidfowl
    Member

    We have no plans to make no reflection mode work.

  25. ghost locked as resolved and limited conversation to collaborators on Nov 19, 2022
  26. added
    area-networkingIncludes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions
    and removed on Jun 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    NativeAOTarea-networkingIncludes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractionsfeature-kestrel

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions