aspnetcore icon indicating copy to clipboard operation
aspnetcore copied to clipboard

When using Hot Reload endpoints get duplicated

Open vsfeedback opened this issue 1 year ago • 8 comments

This issue has been moved from a ticket on Developer Community.


[severity:It bothers me. A fix would be nice] [regression] [worked-in:17.3.3] I'm working on an API project using .NET 6, and when using hot reload, after a certain number of times, the method (and thus the enpoind) gets duplicated. I'm not quite sure if it's a .NET 6 problem or a VS problem.

To replicate (could be a local problem also):

  • Create an edpoint and edit it several times, then hot reload, after multiple times reloading the code the app will get an Microsoft.AspNetCore.Routing.Matching.AmbiguousMatchException: The request matched multiple endpoints.

If I start reloading several times after I get the error, the endpoint will get duplicated the number of times I have hot reloaded my code.


Original Comments

Feedback Bot on 8/30/2022, 07:35 PM:

(private comment, text removed)


Original Solutions

(no solutions)

vsfeedback avatar Nov 02 '22 21:11 vsfeedback

I'm also experiencing this issue, which I thought was being caused by the SpaStaticFiles middleware. However, I disabled that and I'm still seeing an issue with endpoints being duplicated with hot reload enabled via dotnet watch.

After I run the app, I can save a change in my API Controller and hot reload works fine. Then, the 2nd time I save a change, hot reload works, but then my requests fail to that endpoint until I restart the application. Other endpoints work fine.

This issue seems to either be related to or a duplicate of https://github.com/dotnet/aspnetcore/issues/41177

Here is the error I'm seeing coming back from my API requests:

Microsoft.AspNetCore.Routing.Matching.AmbiguousMatchException: The request matched multiple endpoints. Matches: 

MyEndpoint.HandleAsync (MyProject)
MyEndpoint.HandleAsync (MyProject)
   at Microsoft.AspNetCore.Routing.Matching.DefaultEndpointSelector.ReportAmbiguity(CandidateState[] candidateState)
   at Microsoft.AspNetCore.Routing.Matching.DefaultEndpointSelector.ProcessFinalCandidates(HttpContext httpContext, CandidateState[] candidateState)
   at Microsoft.AspNetCore.Routing.Matching.DefaultEndpointSelector.Select(HttpContext httpContext, CandidateState[] candidateState)
   at Microsoft.AspNetCore.Routing.Matching.DfaMatcher.MatchAsync(HttpContext httpContext)
   at Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.Invoke(HttpContext httpContext)
   at Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.HttpsPolicy.HttpsRedirectionMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)
...

HEADERS
=======
...

I am using Attribute routing and here's my middleware pipeline:

    public void Configure(IApplicationBuilder app) =>
        app
            .IfDevelopment(Environment, a =>
                a
                .UseDeveloperExceptionPage()
                .UseCors("CORS")
                .UseSwagger()
                .UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "MyProject v1")))
            .IfNotDevelopment(Environment, a =>
                a.UseExceptionHandler("/error"))
            .UseHttpsRedirection()
            .UseStaticFiles()
            .UseRouting()
            .UseAuthentication()
            .UseAuthorization()
            .UseEndpoints(endpoints =>
            {
                _ = endpoints.MapControllers();
            });

Here is my dotnet --info:

> dotnet --info
.NET SDK (reflecting any global.json):
 Version:   6.0.402
 Commit:    6862418796

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.19043
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\6.0.402\

global.json file:
  C:\dev\projects\my\project\global.json

Host:
  Version:      6.0.10
  Architecture: x64
  Commit:       5a400c212a

.NET SDKs installed:
  3.0.100 [C:\Program Files\dotnet\sdk]
  3.1.424 [C:\Program Files\dotnet\sdk]
  5.0.408 [C:\Program Files\dotnet\sdk]
  6.0.100 [C:\Program Files\dotnet\sdk]
  6.0.201 [C:\Program Files\dotnet\sdk]
  6.0.302 [C:\Program Files\dotnet\sdk]
  6.0.305 [C:\Program Files\dotnet\sdk]
  6.0.402 [C:\Program Files\dotnet\sdk]

seangwright avatar Nov 03 '22 17:11 seangwright

Took a stab at trying to reproduce this. It appears that only certain types of edits cause the duplicate endpoints to be produced. Specifically, changing the return type as mentioned in https://github.com/dotnet/aspnetcore/issues/41177 is a reliable repro. Another one is modifying the parameter types in a method.

@tmat Is there anything special that happens when applying deltas for these types of scenarios that could be a clue here?

captainsafia avatar Nov 15 '22 06:11 captainsafia

My guess is that there are two reasons for this:

  1. https://github.com/dotnet/runtime/issues/75154
  2. Caches in ASP.NET need to be invalidated.

@davidwengier FYI

tmat avatar Nov 15 '22 19:11 tmat

Caches in ASP.NET need to be invalidated.

Which caches are you referring to here?

captainsafia avatar Nov 15 '22 23:11 captainsafia

Any caches of endpoints that memoize MethodInfos

tmat avatar Nov 16 '22 00:11 tmat

Thanks for contacting us. We're moving this issue to the .NET 8 Planning milestone for future evaluation / consideration. Because it's not immediately obvious that this is a bug in our framework, we would like to keep this around to collect more feedback, which can later help us determine the impact of it. We will re-evaluate this issue, during our next planning meeting(s). If we later determine, that the issue has no community involvement, or it's very rare and low-impact issue, we will close it - so that the team can focus on more important and high impact issues. To learn more about what to expect next and how this issue will be handled you can read more about our triage process here.

ghost avatar Nov 23 '22 18:11 ghost

Because it's not immediately obvious that this is a bug in our framework.

Ha. Well, it certainly isn't a bug in my code because it worked one day and then when I updated the .NET SDK it stopped working 🤷‍♂️.

Also, if anyone looks at #41177 you can see this problem has been around since at least April 2022.

seangwright avatar Nov 23 '22 19:11 seangwright

Because it's not immediately obvious that this is a bug in our framework.

Ha. Well, it certainly isn't a bug in my code because it worked one day and then when I updated the .NET SDK it stopped working 🤷‍♂️.

Also, if anyone looks at #41177 you can see this problem has been around since at least April 2022.

Ooof! Ignore the bot response in this case. We placed it in the milestone because I need more time to investigate what is happening here. There's a couple of hunches to track down and I'm aware of the referenced issue...

captainsafia avatar Nov 23 '22 19:11 captainsafia

Absolutely the same problem. It used to work but suddenly I discovered that endpoints get duplicated when I change literally anything inside actions. Visual Studio 2022 17.7.3. I suspect it's related to the spree of recent updates.

nikriaz avatar Sep 05 '23 17:09 nikriaz

To learn more about what this message means, what to expect next, and how this issue will be handled you can read our Triage Process document. We're moving this issue to the .NET 9 Planning milestone for future evaluation / consideration. Because it's not immediately obvious what is causing this behavior, we would like to keep this around to collect more feedback, which can later help us determine how to handle this. We will re-evaluate this issue, during our next planning meeting(s). If we later determine, that the issue has no community involvement, or it's very rare and low-impact issue, we will close it - so that the team can focus on more important and high impact work.

ghost avatar Oct 06 '23 21:10 ghost