You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Microsoft.AspNetCore.Hosting.ListenOptionsHttpsExtensions without reflection ? #31561
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.
Reacted by nietras and Stanislav Denisov
Reacted by Nicholas Ventimiglia and mrzaxaryan
Reacted by Andrii Kurdiumov and Nicholas Ventimiglia
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
Thanks for the feedback. I am investigating further on my side whether the requested change would solve my issue, but I have a new MacBook Air ARM and building aspnetcore on that one seems not easy/possible. Will have to switch to my MacBook Pro but that will be for next weekend.
Here some more info after doing a lot of testing this weekend; I have the feeling it is almost there, just still looking for how to include my custom Kestrel build in the NativeAOT flow.
First, let me give you some more background about the feature I am looking for: the idea is to make some simple web api's, developed in C#, making use of Kestrel, but natively compiled for size, speed and C++ alike code protection.
Based on some examples I found, I came to the following program:
```C#
using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Hosting.Server;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
namespace tinykestrel
{
class Program
{
static async Task Main(string[] args)
{
NullLoggerFactory nullLoggerFactory = new NullLoggerFactory();
SocketTransportOptions socketTransportOptions = new SocketTransportOptions();
SocketTransportFactory socketTransportFactory = new SocketTransportFactory(
new OptionsWrapper<SocketTransportOptions>(socketTransportOptions), nullLoggerFactory);
KestrelServerOptions kestrelServerOptions = new KestrelServerOptions();
/* This ApplicationServices configuration is required to make .NET5 and .NET6 work,
but it prohibits NativeAOT without reflection */
/*
kestrelServerOptions.ApplicationServices = new ServiceCollection()
.AddLogging()
.BuildServiceProvider();
*/
kestrelServerOptions.ListenLocalhost(5001, listenOptions =>
{
X509Certificate2 serverCertificate = new X509Certificate2("server.pfx", "xxx");
listenOptions.UseHttps(serverCertificate);
});
using (KestrelServer kestrelServer =
new KestrelServer(new OptionsWrapper<KestrelServerOptions>(kestrelServerOptions),
socketTransportFactory,
nullLoggerFactory
)
)
{
await kestrelServer.StartAsync(new HttpApplication(), CancellationToken.None);
Console.ReadLine();
}
}
}
public class HttpApplication : IHttpApplication<IFeatureCollection>
{
public IFeatureCollection CreateContext(IFeatureCollection contextFeatures)
{
return contextFeatures;
}
public void DisposeContext(IFeatureCollection context, Exception exception)
{
}
public async Task ProcessRequestAsync(IFeatureCollection features)
{
IHttpRequestFeature request = (IHttpRequestFeature)features[typeof(IHttpRequestFeature)];
IHttpResponseFeature response = (IHttpResponseFeature)features[typeof(IHttpResponseFeature)];
IHttpResponseBodyFeature responseBody = (IHttpResponseBodyFeature)features[typeof(IHttpResponseBodyFeature)];
//IHttpConnectionFeature connection = (IHttpConnectionFeature)features[typeof(IHttpConnectionFeature)];
string requestBodyString;
Dictionary<string, object> jsonObjects = null;
using (StreamReader streamReader = new StreamReader(request.Body))
{
requestBodyString = await streamReader.ReadToEndAsync();
if (requestBodyString != string.Empty)
{
jsonObjects = await JsonSerializer.DeserializeAsync<Dictionary<string, object>>(new MemoryStream(Encoding.UTF8.GetBytes(requestBodyString)));
jsonObjects.Add("extra 1", "hey, I just changed your json");
}
}
if (jsonObjects == null)
{
jsonObjects = new Dictionary<string, object>();
jsonObjects.Add("line 1", "please send me some json and I will send it back to you");
jsonObjects.Add("line 2", "you can use me if you like");
jsonObjects.Add("line 3", "do no use subobjects in the json, I am a simple web api");
}
string responseBodyString = JsonSerializer.Serialize(jsonObjects);
byte[] responseBodyBytes = Encoding.UTF8.GetBytes(responseBodyString);
response.Headers.ContentLength = responseBodyBytes.Length;
response.Headers.Add("Content-Type", "application/json");
await responseBody.Stream.WriteAsync(responseBodyBytes, 0, responseBodyBytes.Length);
}
}
}
```
Without the https configuration lines:
```C#
kestrelServerOptions.ListenLocalhost(5001, listenOptions =>
{
X509Certificate2 serverCertificate = new X509Certificate2("server.pfx", "xxx");
listenOptions.UseHttps(serverCertificate);
});
```
I can natively compile and run this on my Mac. However, when I add the UseHttps lines above without:
```C#
kestrelServerOptions.ApplicationServices = new ServiceCollection()
.AddLogging()
.BuildServiceProvider();
```
Then I indeed get as you replied a Null reference error on the lines mentioned in the Microsoft.AspNetCore.Hosting.ListenOptionsHttpsExtensions.
I managed to compile the Kestrel project with the requested change implemented locally on my Mac and then in debug mode the above program runs fine again, also with the UseHttps and without the kestrelServerOptions.ApplicationServices configured.
The only thing I am missing at the moment is how to include my custom built Microsoft.AspNetCore.Server.Kestrel.Core.dll and Microsoft.AspNetCore.Server.Kestrel.dll in the NativeAOT compilation on Mac. I am pretty sure if I get there, the above will work and opens a nice solution to develop simple natively compiled web api's with Kestrel in C#. So it would be nice if you could include the 2 question marks to avoid the Null reference error.
On 11 Apr 2021, at 17:29, David Fowler ***@***.***> wrote:
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;
}
}
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub <#31561 (comment)>, or unsubscribe <https://github.com/notifications/unsubscribe-auth/AKO6WGESLP4MJTLRZ432EGTTIG56VANCNFSM42O4F65Q>.
On 11 Apr 2021, at 17:32, David Fowler ***@***.***> wrote:
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.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub <#31561 (comment)>, or unsubscribe <https://github.com/notifications/unsubscribe-auth/AKO6WGGOYAZLK6HCXQRO263TIG6I7ANCNFSM42O4F65Q>.
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,
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.
@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 ?
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.
@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.
Activity
davidfowl commentedon Apr 6, 2021
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
BrennanConroy commentedon Apr 7, 2021
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?
LLT21 commentedon Apr 8, 2021
davidfowl commentedon Apr 9, 2021
I don't think it will.
LLT21 commentedon Apr 11, 2021
davidfowl commentedon Apr 11, 2021
Instead of doing this:
Implement a custom IServiceProvider that does this:
LLT21 commentedon Apr 11, 2021
davidfowl commentedon Apr 11, 2021
Yes, you're basically implementing your own
IServiceProviderand it can do whatever you want. Like manually create objects.LLT21 commentedon Apr 11, 2021
davidfowl commentedon Apr 11, 2021
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,
36 remaining items
roji commentedon Jun 3, 2021
@LLT21 thanks for this super interesting work.
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.
LLT21 commentedon Jun 3, 2021
@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 ?
MichalStrehovsky commentedon Jun 4, 2021
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.
LLT21 commentedon Jun 4, 2021
@MichalStrehovsky The flow has started, so I am happy it goes this fast. Thanks for your feedback.
LLT21 commentedon Jun 4, 2021
@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
roji commentedon Jun 4, 2021
@LLT21 great. If you still run into Npgsql issues, we can try to iterate quickly to get to a point where it works.
MichalStrehovsky commentedon Jun 5, 2021
Yup, these all look good! Looking forward to see the results!
roji commentedon Jun 6, 2021
@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).
LLT21 commentedon Jun 8, 2021
@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.
roji commentedon Jun 8, 2021
Yeah, we're working on it :) I'll ping you once we have something which should work.
davidfowl commentedon Oct 20, 2022
We have no plans to make no reflection mode work.