Visual Studio 2017/2019 Quick Tip: Can’t F5 Debug – Duplicate collection entry

I tried to debug one of my WebAPI projects in Visual Studio 2019 RC4 today and received an error message:

Cannot add duplicate collection entry of type ‘add’ with unique key attribute ‘name’ set to ‘aspNetCore

First I tried restarting Visual Studio but got the same error.  I researched a little and did find a StackOverflow post with some workarounds but they didn’t seem to fix the underlying issue.

https://stackoverflow.com/questions/40069806/cannot-add-duplicate-collection-entry-of-type-add-with-unique-key-attribute-n

By opening the associated error log after the error message I found the problem seems to be a duplicate http handler in the generated applicationHost.config file under the hidden “.vs” folder in my root solution folder.  I didn’t see any duplicate handlers in this file for my project so it must be duplicated somewhere else.

After further thought I remember reading a post at some point that talked about the applicationHost.config file becoming corrupt.  So I closed VS, opened a PowerShell console as Admin, navigated to the root solution folder and renamed the “.vs” folder.

> Rename-Item .vs old.vs

That did it!  Visual Studio regenerated the “.vs” folder and everything worked again.  I’m not sure if this was a 2019 RC specific issue or not but this solution will probably fix multiple similar issues in 2017/2019.

Hope this helps others.  If so or you have further question/info please leave a comment below.

Happy Debugging!

Leave a Reply