Cannot add duplicate collection entry of type 'add' with unique key attribute 'name' set to 'aspNetCore
Asked Answered
C

11

42

I have recently published my ASP.NET Core application to my host. I am hitting a HTTP Error 500.19.

IIS 8.5 says the issue is:-

"Cannot add duplicate collection entry of type 'add' with unique key attribute 'name' set to 'aspNetCore'"

It also highlights this key add line in my system.webServer config:-

<handlers>
  <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule"      
</handlers>

I'm not really sure what to do on this. It looks as though there is a duplicate instance of this, so I have tried renaming this but it still asks to add this again?

Here is my web.config:-

<?xml version="1.0" encoding="utf-8"?>
<configuration>

  <!--
  Configure your application settings in appsettings.json. Learn more at http://go.microsoft.com/fwlink/?LinkId=786380
  -->

  <system.webServer>
  <handlers>
    <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
  </handlers>
  <aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false"/>
  </system.webServer>

<system.net>
<defaultProxy useDefaultCredentials="true" >
</defaultProxy>
</system.net>
</configuration>
Cida answered 16/10, 2016 at 11:38 Comment(5)
Can you post the full error log, may be you'll more detail running it in console. What did u set as Url bindings in ISSMalorie
Have you tried adding <remove name="aspNetCore" /> before the add?Dowdell
what if you call it aspNetCoreHandler ?Malorie
Getting the same thing. By any chance did you figured out the solution for this?Stillborn
I tried not adding an application under the website, and changing the appPool of the website itself, and now I'm getting the same error I was getting when I put <remove name="aspNetCore" /> before the add, so that might helpWingspan
R
35

The answer above didn't work for me, however DavidG's comment did solve my problem, so going to post as an answer in case it helps someone else.

For me, I was not running it as a sub-application, and a project that had been working for me no issue for over a year suddenly stopped working with this issue. Still not sure what changed. When I commented out or removed the <add name="aspNetCore".../> the error persisted, and then that line got automatically re-added.

To solve the problem, I added <remove name="aspNetCore" /> to the config file, right above the <add name="aspNetCore"... /> entry, and things started working again.

Reduction answered 28/2, 2018 at 7:1 Comment(1)
That's a good example of Voodoo programming. It works, but the hell why.Vanscoy
D
17

To continue running on IIS EXPRESS, go on root folder where the .sln file stays.

go to delete file from .vs\config\applicationhost.config or save it in a temporary place if you have something there.

Close/Re Open VS Studio, run again, will work.

If you need to add something back from save applicationhost.config, just compare those two, but I don't see what you could have there.

Disc answered 13/9, 2018 at 14:43 Comment(7)
This should be the correct answer. I had an duplicate entry in this config, after deleting the file it created a new file without the duplicate and it worked. Thanks @DiscClive
It worked. But also i deleted .vs\{projectName}\config\applicationhost.config fileHeft
This worked for me. I also found that the .vs folder was locked by Rolsyn (found using Process Explorer | Find | Find Handle or DLL). Had to kill this first to remove the folder.Gomulka
I also think this should be the right solution. At least In my case IIS, Express stopped working without apparently I had changed nothing. Cleaning the previous applicationhost.config solved the problemLollar
@Disc I am really interested in knowing how did you find out that this is the file that is causing the issue.Fixative
From learn.microsoft.com/en-us/iis/get-started/… is the file located in different places (IIS, IIS express) that define how your website bindings/virtual folders are defined.Disc
To tell you the truth, I no longer use IIS or IIS express with .net core, is just to bad and interfering ports when I start 3 or 4 websites, I just run kestrel and that is it with netcore CMD where I can see exactly what port is used or what the issue is.Disc
M
10

None of the suggested solutions worked for me unfortunately. By some miracle I learned that my applicationhost.config file had been modified in an unfortunate matter, making that "Cannot add duplicate collection entry" error appear when I navigated to a specific page in my .NET Core website application.

Under the <sites> tag in applicationhost.config, I had the following:

<site name="MyWebsite" id="2">
    <application path="/" applicationPool="Clr4IntegratedAppPool">
        <virtualDirectory path="/" physicalPath="C:\Users\___\solutionname\MyWebsite" />
    </application>
    <application path="/SomePage" applicationPool="Clr4IntegratedAppPool">
        <virtualDirectory path="/" physicalPath="C:\Users\___\solutionname\MyWebsite" />
    </application>
    <bindings>
        <binding protocol="https" bindingInformation="*:12345:localhost" />
    </bindings>
</site>

The 500.19 HTTP error was shown to me when I navigated to the page "/SomePage". As you can see, for some reason there was a separate <application> tag for this specific page. I have no idea why.

I removed that entire <application> tag for the "/SomePage" path, and everything started working again.

Mic answered 5/5, 2018 at 16:2 Comment(3)
Thanks, this also fixed it for me. The steps I had taken to cause this were that I wanted my application to open under debug (F5) on a certain page, which I added in the Project Settings, but this then caused the entry as described above in applicationHost.config which broke my appLongicorn
@System24Tech exact same issue here; I changed the "Project URL" in the server section instead of the "Specific Page" in the Start Action section. Doh! Blindly clicking away not thinking about what I'm doing...Cecilycecity
perhaps add a note that this can be found at {projectpath}/.vs/{project}/config/applicationhost.configSilhouette
B
6

I faced this issue with vs 2017 on a project that was working fine without changing the web.config. Looking at this posts I realized that it might be an IIS express issue and I solved simply deleting .vs folder and restarting vs.

Babarababassu answered 2/6, 2018 at 7:17 Comment(0)
Z
3

I had the same problem and in my case commenting the line

<add name="aspNetCore"...

solved the issue and brought up the question "why is it working without AspNetCoreModule". The problem in my case was that I was adding the site as a sub-application in defaultwebsite and it was located in the wwwrootfolder. I think the config was automatically picked up by the defaultwebsite and applied for all sub-application sites.

This link helped

So the solution was to move it as a separate site on another port.

Zelda answered 20/11, 2017 at 12:26 Comment(1)
Solves my problem. Thanks!Orran
P
0

In my case, the issue was caused by putting a path in the Debug Tab of my web project so that the app would open at a particular page. This causes two silent additions to the file .vs\config\applicationhost.config, similar to the one observed by eightx2.

In :

<add name="api AppPool" managedRuntimeVersion="" />

In :

<application path="/blah" applicationPool="api AppPool">
<virtualDirectory path="/" physicalPath="your-path\src\your-proj" />
</application>

where a similar entry already exists. This is the root of the problem.

The error message, unfortunately, is completely misleading.

Solution is to rename applicationhost.config, restart VS, and let it rebuild the file. This is why Ricardo's solution of deleting the entire .vs folder also works.

Pallua answered 11/8, 2018 at 16:46 Comment(0)
R
0

That error is because there is a root file in ASP.NET Core that is called ".vs\config\applicationhost.config" Initially it has 67 keys. You can see it for yourself here, in the Configuration Editor. Where to find the ApplicationHost.config

This file called ".vs\config\applicationhost.config" has the default settings carried by the Web.config to be able to work, and one of them is that handler. You can also see it here. Key aspNetCore

The problem is that that file has that handler and what you publish is going to inherit that handler.

You have two solutions, comment on the line of your published web.config or delete that handler from the ".vs\config\applicationhost.config"

Raila answered 13/9, 2018 at 15:55 Comment(0)
R
0

I just had this one, it turns out I had changed the App Url in Debug settings for the website Properties, in order to load a specific page (wrong but happened).

enter image description here

In IIS it automatically created a new application under the test domain called About (in this case).

Removing the rogue IIS application under the domain solves the issue as it doesn't attempt to reload the same web.config when navigating to the page.

Renatarenate answered 26/9, 2019 at 15:0 Comment(0)
C
0

I commented the below statement

<verbs allowUnlisted="true">
And its works
Citystate answered 24/9, 2021 at 8:29 Comment(0)
M
0

If you're running your site in IIS, check if your Application Pool has a "duplicate" Application attached.

I had this same problem debugging my site in IIS. While troubleshooting I found that the Application Pool for my site showed 2 applications connected to it. I checked each of my sites to make sure I hadn't accidentally assigned one of them to the same app pool but they were all correct. So I deleted my site in IIS and checked the app pool which then showed 0 applications. I recreated the site in IIS, attached it to the application pool which then showed only the 1 application. Restarted the site and it worked correctly.

Not sure how that phantom application got attached to my app pool but that's what was causing the duplicate aspNetCore key in my case.

Marta answered 11/8, 2022 at 19:51 Comment(0)
S
0

I have a parent web application with multiple child applications.

This is what worked for me:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <location path="." inheritInChildApplications="false">
    <system.webServer>
      <handlers>
        <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
      </handlers>
      <aspNetCore processPath="dotnet" arguments="<pathToDll>" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="InProcess" />
    </system.webServer>
  </location>
</configuration>

Basically, wrap the system.webServer element of the parent website with the location element and set inheritInChildApplications: <location path="." inheritInChildApplications="false">.

Then the child websites can stay the same and you do not need to add <remove name="aspNetCore" /> to each child website's web.config.

Note that this is in the project file:

<aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="InProcess" />

And it gets changed to this during publish:

<aspNetCore processPath="dotnet" arguments="<pathToDll>" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="InProcess" />
Stockist answered 20/10, 2022 at 17:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.