C
C#2y ago
gerard

❔ 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..?
2 Replies
gerard
gerardOP2y ago
Ah, for now I found a work-around for my initial problem. I couldn't use Activate.CreateInstance on my type. So I've added the following:
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]
on the assembly attribute that registers the control. ThinKenobi
Accord
Accord2y ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.

Did you find this page helpful?