json-api-dotnet/JsonApiDotNetCore

Visual Studio 2022 17.3.0 breaks JsonAPIDotNet 5.0.1

telosity opened this issue · 7 comments

We are JsonApiDotNetCore library (5.0.1). in developing a rich REST API. All is working well.

However, today I updated my VS 2022 to 17.3.0, and now when I try to run I get the error JsonApiDotNetCore.Errors.InvalidConfigurationException with the message "Multiple controllers found for resource type ...

Running the same source on a non-updated VS2022 works just fine.

Not a code change between running on different machines, other than the VS2022 version,
and the compiled application runs perfectly.

at JsonApiDotNetCore.Middleware.JsonApiRoutingConvention.Apply(ApplicationModel application)
at Microsoft.AspNetCore.Mvc.ApplicationModels.ApplicationModelConventions.ApplyConventions(ApplicationModel applicationModel, IEnumerable1 conventions) at Microsoft.AspNetCore.Mvc.ApplicationModels.ApplicationModelFactory.CreateApplicationModel(IEnumerable1 controllerTypes)
at Microsoft.AspNetCore.Mvc.ApplicationModels.ControllerActionDescriptorProvider.GetDescriptors()
at Microsoft.AspNetCore.Mvc.ApplicationModels.ControllerActionDescriptorProvider.OnProvidersExecuting(ActionDescriptorProviderContext context)
at Microsoft.AspNetCore.Mvc.Infrastructure.DefaultActionDescriptorCollectionProvider.UpdateCollection()
at Microsoft.AspNetCore.Mvc.Infrastructure.DefaultActionDescriptorCollectionProvider.Initialize()
at Microsoft.AspNetCore.Mvc.Routing.ActionEndpointDataSourceBase.<>c__DisplayClass11_0.b__0()
at Microsoft.Extensions.Primitives.ChangeToken.OnChange(Func`1 changeTokenProducer, Action changeTokenConsumer)
at Microsoft.AspNetCore.Mvc.Routing.ActionEndpointDataSourceBase.Subscribe()
at Microsoft.AspNetCore.Mvc.Routing.ControllerActionEndpointDataSource..ctor(ControllerActionEndpointDataSourceIdProvider dataSourceIdProvider, IActionDescriptorCollectionProvider actions, ActionEndpointFactory endpointFactory, OrderedEndpointsSequenceProvider orderSequence)
at Microsoft.AspNetCore.Builder.ControllerEndpointRouteBuilderExtensions.GetOrCreateDataSource(IEndpointRouteBuilder endpoints)
at Microsoft.AspNetCore.Builder.ControllerEndpointRouteBuilderExtensions.MapControllers(IEndpointRouteBuilder endpoints)
at Program.

$(String[] args) in C:\Users\gerry.hull\source\repos\dragonfly-api\Dragonfly.Api\Program.cs:line 50

I'm having trouble reproducing this using v17.3, but it seems related to source generators. Do you see duplicate classes in Visual Studio?

image

Something else you can try: add the following to your .csproj, rebuild and inspect the contents of .\obj\Debug\net6.0\generated\JsonApiDotNetCore.SourceGenerators\JsonApiDotNetCore.SourceGenerators.ControllerSourceGenerator\

<PropertyGroup>
	<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
</PropertyGroup>

Also the usual cleanup steps may help, such as removing all bin/obj folders, git clean -xdf, deleting the .vs folder etc.

If all else fails, can you try creating a minimal repro project?

In case it helps to unblock you and you don't want to downgrade VS: Try removing [Resource] from your resource classes and manually add controller classes as described at Earlier versions in https://www.jsonapi.net/usage/extensibility/controllers.html#earlier-versions.

Many other projects are suffering from the same problem, which apparently is caused by a bug in recent versions of the .NET 6 SDK. There's a long thread at dotnet/wpf#6792, listing several workarounds.

Thanks much for the quick and detailed reply. I may have to downgrade we shall see.

Our issue is resolved. We had partial classes for each controller as we needed to attribute them for security reasons. In addition, we had the [Resource] attribute. Removing that solved the problem. Thank you!