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

核能气质少年 提交于 2019-12-22 01:33:16

问题


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>

回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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.




回答5:


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.




回答6:


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.




回答7:


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.

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.

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"




回答8:


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).

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.



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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!