Postgresql and AoT

Posts   
 
    
ladamik
User
Posts: 4
Joined: 15-Feb-2024
# Posted on: 03-Feb-2025 08:53:52   

Hi, .NET 9 LLBL 5.11.3

I am trying to use AoT compilation in my lambda project. After setting <PublishAot>true<PublishAoT> in csproj and deploying app I get an error:

Unhandled exception. SD.LLBLGen.Pro.ORMSupportClasses.ORMGeneralOperationException: The property name 'NpgsqlDbType' is unknown on the parameter class in the assembly 'Npgsql, Version=9.0.2.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7' at SD.LLBLGen.Pro.ORMSupportClasses.DbProviderFactoryInfo.InitializeSpecificDbTypeCache() + 0x31e at SD.LLBLGen.Pro.DQE.PostgreSql.DynamicQueryEngine.Configure(PostgreSqlDQEConfiguration) + 0x153 at Program.<Main>$(String[] args) + 0xf1

The only way to resolve this issue is adding in csproj: <ItemGroup> <TrimmerRootAssembly Include="Npgsql" /> </ItemGroup>

but I don't want to set Npgsql library as root assembly because my application size is bigger when I do it. The problem probably occurs because, in class DbProviderFactoryInfo, in method InitializeSpecificDbTypeCache (line 243) reflection is used.

Code that I use to configure a database:

RuntimeConfiguration.ConfigureDQE<PostgreSqlDQEConfiguration>(c => c.SetTraceLevel(Debugger.IsAttached ? TraceLevel.Verbose : TraceLevel.Off) .AddDbProviderFactory(typeof(NpgsqlFactory)));

How can I resolve this problem without using TrimmerRootAssembly?

Walaa avatar
Walaa
Support Team
Posts: 14992
Joined: 21-Aug-2005
# Posted on: 04-Feb-2025 02:41:27   

Did you try Root descriptors to only root the dynamically accessed part ?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39825
Joined: 17-Aug-2003
# Posted on: 04-Feb-2025 08:32:02   

Additionally, it sounds like an Npgsql problem, maybe you should ask there? Besides we do dynamic compilation of lambdas at runtime, for projections. We have no code to make sure AOT works, so it might very well not work at all. We never tested our runtime on AOT nor will we do so, as our design requires being able to dynamically compile lambda's at runtime for certain situations.

Frans Bouma | Lead developer LLBLGen Pro
ladamik
User
Posts: 4
Joined: 15-Feb-2024
# Posted on: 04-Feb-2025 10:17:42   

I tried to use: [DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(NpgsqlDbType))] and also rd.xml, but it doesn't work. In my opinion, the problem occurs because in DbProviderFactoryInfo in InitializeSpecificDbTypeCache method some types are loaded from assembly, and it is probably not supported by AoT (library probably is trimmed, and then reflection doesn't work unless I set Npgsql library as root assembly). I had a similar problem with DI registration by reflection.

I had a problem with projections and enums but by adding code like: ProjectionRow a = new ProjectionRow(new object[] { 1, "2", 3.0m, DateTime.Now, Enum.Insert }); a.GetNullableEnumValue<Enum>(1); I can load metadata needed by AoT compilation. It is not the best solution, but it probably works. Generally, I tested my application, and it seems that queries work.

Ok, so I guess, I need to keep Npgsql as root assembly if another solution doesn't exist