检测到覆盖类型 System.Web.IHttpHandler 的现有映射的尝试

An attempt to override an existing mapping was detected for type System.Web.IHttpHandler

当我将 asp.net mvc 应用程序复制到测试服务器的 IIS 文件夹时,出现以下错误。

在本地它工作得很好:

检测到对名称为“”的类型 System.Web.IHttpHandler 的现有映射的覆盖尝试,当前映射到类型 Microsoft.Reporting.WebForms.HttpHandler,类型为 Microsoft.Reporting.WebForms.HttpHandler。

UnityConfig.cs代码是这样的:

namespace xxx.Relacionamiento.Web.App_Start
{
    /// <summary>
    /// Specifies the Unity configuration for the main container.
    /// </summary>
    public class UnityConfig
    {
        #region Unity Container
        private static Lazy<IUnityContainer> container = new Lazy<IUnityContainer>(() =>
        {
            var container = new UnityContainer();
            RegisterTypes(container);
            return container;
        });

        /// <summary>
        /// Gets the configured Unity container.
        /// </summary>
        public static IUnityContainer GetConfiguredContainer()
        {
            return container.Value;
        }
        #endregion

        /// <summary>Registers the type mappings with the Unity container.</summary>
        /// <param name="container">The unity container to configure.</param>
        /// <remarks>There is no need to register concrete types such as controllers or API controllers (unless you want to 
        /// change the defaults), as Unity allows resolving a concrete type even if it was not previously registered.</remarks>
        public static void RegisterTypes(IUnityContainer container)
        {
            // NOTE: To load from web.config uncomment the line below. Make sure to add a Microsoft.Practices.Unity.Configuration to the using statements.
            container.RegisterType<IUnitOfWork, EntityFrameworkUnitOfWork>(new PerRequestLifetimeManager());
            container.RegisterType<DbContext, ApplicationDbContext>(new HierarchicalLifetimeManager());
            container.RegisterType<UserManager<ApplicationUser>>(new HierarchicalLifetimeManager());
            container.RegisterType<IUserStore<ApplicationUser>, UserStore<ApplicationUser>>(new HierarchicalLifetimeManager());
            container.RegisterType<AccountController>(new InjectionConstructor(
                //new ResolvedParameter<ApplicationUserManager>("userManager"),
                //new ResolvedParameter<ApplicationSignInManager>("signInManager"),
                new ResolvedParameter<DatosExternosService>("DatosExternos"),
                new ResolvedParameter<UserRolesServices>("UserRolesServices"),
                new ResolvedParameter<AspNetUserService>("AspNetUserService"),
                new ResolvedParameter<AsesoresService>("AsesoreService"),
                new ResolvedParameter<AsociadosService>("AsociadoService")
                ));
            container.RegisterTypes(
                AllClasses.FromLoadedAssemblies(), WithMappings.FromMatchingInterface, WithName.Default
               );
        }
    }
}

web.config

<system.web>
    <!--<globalization uiCulture="es" culture="ES" />-->
    <globalization uiCulture="auto" culture="auto" />
    <!--<httpRuntime maxRequestLength="1048576" />-->
    <httpHandlers>
      <add path="Reserved.ReportVidewerWebControl.axd" verb="*" type="Microsoft.Reporting.WebForms.HttpHandler, Microsoft.ReportViewer.WebForms, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" validate="false" />
      <add path="Reserved.ReportViewerWebControl.axd" verb="*" type="Microsoft.Reporting.WebForms.HttpHandler, Microsoft.ReportViewer.WebForms, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" validate="false" />
      <add path="Reserved.ReportViewerWebControl.axd" verb="*" type="Microsoft.Reporting.WebForms.HttpHandler, Microsoft.ReportViewer.WebForms, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" validate="false" />
   </httpHandlers>
    <customErrors mode="Off"></customErrors>
    <authentication mode="None" />
    <compilation debug="true" targetFramework="4.5.1">
      <assemblies>
        <add assembly="Microsoft.ReportViewer.WebForms, Version=12.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" />
        <add assembly="Microsoft.ReportViewer.Common, Version=12.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" />
        <add assembly="Microsoft.Build.Framework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" />
      </assemblies>

我已经用谷歌搜索了这个错误,但没有得到任何结果。

您可能需要清除任何继承的 HttpHandlers

<httpHandlers>
  <clear/>
  <add path="...

由于 RegisterTypes 方法抛出 DuplicateTypeMappingException,您可能已经有一个注册映射,该映射也符合 RegisterTypes 上定义的约定之一 - 称为 'registration by convention'.

RegisterTypes 方法 returns IUnityContainerRegistrationConvention 相关,如下所示:

public static IUnityContainer RegisterTypes(
    this IUnityContainer container,
    RegistrationConvention convention,
    bool overwriteExistingMappings = false)

然后,这解释了可选 overwriteExistingMappings 参数的工作原理:

Use this parameter to control the behavior of the RegisterTypes method if the parameters to this method cause an existing mapping in the container to be overwritten. The existing mapping may have been created by a previous call to to register a type or types or during the current call to the RegisterTypes method. If this parameter is set to false, the method throws an DuplicateTypeMappingException exception if it detects an attempt to overwrite an existing mapping. If the parameter is set to true, the method overwrites the existing mapping with a new mapping based on the other parameters to the method. The default value for this parameter is false.

由于在 web.config 和 overwriteExistingMappings 中设置了预定义的 httpHandlers 默认情况下设置为 false,它检测到试图覆盖现有 httpHandlers 映射和投掷 DuplicateTypeMappingException 符合预期。

根据上面的解释,您可以尝试从以下代码部分添加 overwriteExistingMappings 参数设置为 true

container.RegisterTypes(AllClasses.FromLoadedAssemblies(), WithMappings.FromMatchingInterface, WithName.Default);

给这个:

container.RegisterTypes(AllClasses.FromLoadedAssemblies(), WithMappings.FromMatchingInterface, WithName.Default, overwriteExistingMappings: true);

注意:这只会防止 RegisterTypes 在尝试覆盖现有映射时抛出异常。如果您不确定 Microsoft.Reporting.WebForms.HttpHandler 配置没有被覆盖,请使用 DI 方式在 RegisterTypes 方法中注册 ReportViewer 映射。

参考:

Registration by Convention(MSDN)

其他参考资料:

Unity Container: Dependency Injection with Unity(MSDN)

Convention based registrations with Unity