gerard
gerard
CC#
Created by gerard on 3/17/2023 in #help
❔ Source Generator does not work in .NET CLI
When I compile my project in Rider, everything works fine:
2>------- Finished building project: WebFormsCore.NativeAOT.Example. Succeeded: True. Errors: 0. Warnings: 0
2>------- Finished building project: WebFormsCore.NativeAOT.Example. Succeeded: True. Errors: 0. Warnings: 0
but when I try to build it with the CLI (dotnet build or dotnet publish), I get the following:
Build FAILED.

C:\Sources\WebForms\examples\WebFormsCore.NativeAOT.Example\Program.cs(12,10): error CS1061: 'ServiceCollection' does n
ot contain a definition for 'AddAssemblyControls' and no accessible extension method 'AddAssemblyControls' accepting a
first argument of type 'ServiceCollection' could be found (are you missing a using directive or an assembly reference?)
[C:\Sources\WebForms\examples\WebFormsCore.NativeAOT.Example\WebFormsCore.NativeAOT.Example.csproj]
Build FAILED.

C:\Sources\WebForms\examples\WebFormsCore.NativeAOT.Example\Program.cs(12,10): error CS1061: 'ServiceCollection' does n
ot contain a definition for 'AddAssemblyControls' and no accessible extension method 'AddAssemblyControls' accepting a
first argument of type 'ServiceCollection' could be found (are you missing a using directive or an assembly reference?)
[C:\Sources\WebForms\examples\WebFormsCore.NativeAOT.Example\WebFormsCore.NativeAOT.Example.csproj]
In my .csproj I included the source generator with a project reference:
<ProjectReference Include="..\..\src\WebFormsCore.SourceGenerator\WebFormsCore.SourceGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
<ProjectReference Include="..\..\src\WebFormsCore.SourceGenerator\WebFormsCore.SourceGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
The source generator generates the following:
namespace WebFormsCore.NativeAOT.Example
{
internal static class ControlsServiceProviderExtensions
{
public static void AddAssemblyControls(this global::Microsoft.Extensions.DependencyInjection.IServiceCollection services)
{
global::Microsoft.Extensions.DependencyInjection.ServiceCollectionServiceExtensions.AddTransient<WebFormsCore.NativeAOT.Example.Test, WebFormsCore.NativeAOT.Example.CompiledViews.Test_aspx>(services);
global::Microsoft.Extensions.DependencyInjection.ServiceCollectionServiceExtensions.AddTransient<WebFormsCore.NativeAOT.Example.CompiledViews.Test_aspx>(services);
}
}
}
namespace WebFormsCore.NativeAOT.Example
{
internal static class ControlsServiceProviderExtensions
{
public static void AddAssemblyControls(this global::Microsoft.Extensions.DependencyInjection.IServiceCollection services)
{
global::Microsoft.Extensions.DependencyInjection.ServiceCollectionServiceExtensions.AddTransient<WebFormsCore.NativeAOT.Example.Test, WebFormsCore.NativeAOT.Example.CompiledViews.Test_aspx>(services);
global::Microsoft.Extensions.DependencyInjection.ServiceCollectionServiceExtensions.AddTransient<WebFormsCore.NativeAOT.Example.CompiledViews.Test_aspx>(services);
}
}
}
and in code I do the following:
services.AddAssemblyControls();
services.AddAssemblyControls();
I tried both net8.0 (preview 2) and net7.0, with and with-out <PublishAot>true</PublishAot>. What am I missing? Should I try to remove the preview SDK and see if it works..?
3 replies