Skip to content

Projects taking a FrameworkReference to ASP.NET do not get web-project targets settingsΒ #36122

Open
@ericstj

Description

@ericstj

Describe the bug

When a project has an ASP.NET framework reference directly:

    <FrameworkReference Include="Microsoft.AspNetCore.App" />

Or through some project reference to another project (web or as above) it will get a reference to source generators, but not get the web SDK targets. This won't happen for non-aspnet framework references since in those cases the project would take a Configuration package reference which brings in the targets.

These are the settings that are missing:

<PropertyGroup Condition="'$(PublishTrimmed)' == 'true' Or '$(PublishAot)' == 'true'">
<!-- Enable the built-in source generators. -->
<EnableRequestDelegateGenerator Condition="'$(EnableRequestDelegateGenerator)' == ''">true</EnableRequestDelegateGenerator>
<EnableConfigurationBindingGenerator Condition="'$(EnableConfigurationBindingGenerator)' == ''">true</EnableConfigurationBindingGenerator>
</PropertyGroup>
<PropertyGroup>
<!-- Set the namespaces emitted by the RequestDelegateGenerator for interception when applicable. -->
<InterceptorsPreviewNamespaces Condition="'$(EnableRequestDelegateGenerator)' == 'true'">$(InterceptorsPreviewNamespaces);Microsoft.AspNetCore.Http.Generated</InterceptorsPreviewNamespaces>
<!-- Set the namespaces emitted by the ConfigurationBindingGenerator for interception when applicable. -->
<InterceptorsPreviewNamespaces Condition="'$(EnableConfigurationBindingGenerator)' == 'true'">$(InterceptorsPreviewNamespaces);Microsoft.Extensions.Configuration.Binder.SourceGeneration</InterceptorsPreviewNamespaces>
</PropertyGroup>

We had discussed that the publish AOT defaults might be OK, but missing InterceptorsPreviewNamespaces results in a compile time error when the user has opted into the generator.

I this this porting eShopOnContainers and had to set the property myself.

CC @captainsafia @eerhardt @tarekgh @eiriktsarpalis

To Reproduce

  1. create a new non-web project
  2. Add <FrameworkReference Include="Microsoft.AspNetCore.App" />
  3. Add <EnableConfigurationBindingGenerator>true</EnableConfigurationBindingGenerator>
  4. Use configuration:
using Microsoft.Extensions.Configuration;

var c = new ConfigurationBuilder()
    .AddInMemoryCollection(new Dictionary<string,string?>()
    {
        ["Value"] = "42",
    })
    .Build();
C value = new();
c.Bind(value);

class C { public int Value { get; set; } }

configNotWeb.zip

Exceptions (if any)

C:\scratch\configNotWeb\Microsoft.Extensions.Configuration.Binder.SourceGeneration\Microsoft.Extensions.Configuration.Binder.SourceGeneration.ConfigurationBindingGenerator\BindingExtensions.g.cs(34,10): error CS9137: The 'interceptors' experimental feature is not enabled in this namespace. Add '<InterceptorsPreviewNamespaces>$(InterceptorsPreviewNamespaces);Microsoft.Extensions.Configuration.Binder.SourceGeneration</InterceptorsPreviewNamespaces>' to your project. [C:\scratch\configNotWeb\configNotWeb.csproj]

Further technical details

  • Include the output of dotnet --info
C:\scratch\configNotWeb>dotnet --info
.NET SDK:
 Version:   8.0.100-rc.2.23502.2
 Commit:    0abacfc2b6

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.22621
 OS Platform: Windows
 RID:         win-x64
 Base Path:   C:\Program Files\dotnet\sdk\8.0.100-rc.2.23502.2\

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions