相同类型的对象的 InvalidCastException - 自定义控件加载

InvalidCastException for Object of the same type - Custom Control Load(相同类型的对象的 InvalidCastException - 自定义控件加载)

本文介绍了相同类型的对象的 InvalidCastException - 自定义控件加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常连线的错误,我的一个自定义控件似乎是创建两个编译文件,当我尝试使用 LoadControl() 动态加载它时失败,因为无法转换一个对另一个 - 即使它们完全相同.我写消息看看,都是一样的,只是改了编译好的dll.

I have a very wired error, one of my custom controls seems that is create two compiled files, and when I try to load it dynamically with LoadControl() is just fail because can not cast the one to the other - even if they are exactly the same. I write the message to see that all is the same, is only change the compiled dll.

System.Web.HttpUnhandledException (0x80004005):     
 Exception of type 'System.Web.HttpUnhandledException' was thrown. --->             
    System.InvalidCastException:
[A]ASP.Modules_OneProduct_MedioumImage cannot be cast to
[B]ASP.Modules_OneProduct_MedioumImage.         

   Type A originates from 'App_Web_kg4bazz1, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'
in the context 'Default'
    at location 'C:WindowsMicrosoft.NETFramework64v4.0.30319Temporary ASP.NET Files
oot80ed751310eb08d9App_Web_kg4bazz1.dll'.          

   Type B originates from 'App_Web_oneproduct_mediumimage.ascx.d1003923.4xoxco7b, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' 
in the context 'Default'    
    at location 'C:WindowsMicrosoft.NETFramework64v4.0.30319Temporary ASP.NET Files
oot80ed751310eb08d9App_Web_oneproduct_mediumimage.ascx.d1003923.4xoxco7b.dll'.

代码

这是我遵循 完全正确之后的代码MSDN上写的是什么:

foreach (int OneProductID in TheProductIdArrays)
{
    // here is the throw.
    ASP.Modules_OneProduct_MedioumImage OneProduct = 
        (ASP.Modules_OneProduct_MedioumImage)LoadControl(@"~/mod/OneProduct_MediumImage.ascx");

    // do some work with 
    //OneProduct
}

以前我在没有 ASP. 的情况下加载控件,但在出现此错误并寻找解决方案后,我严格遵循 MSDN 上的内容.不管我做什么,bug 仍然存在.

Previously I have Load the control without the ASP. but after this bug appears and looking for solution, I strictly follow what is on MSDN. The bug is still here no matter what I do.

我也尝试过这两种方法,一种是单独的,另一种是一起尝试(再次失败)

I have also try both of this methods, each one alone, and together (again fail)

<%@ Register src="~/mod/OneProduct_MediumImage.ascx" tagname="OneProduct_MediumImage" tagprefix="uc1" %>
<%@ Reference Control="~/mod/OneProduct_MediumImage.ascx" %>

配置

我的 web.config,我尝试了 maxBatchSize 20、100、1000,也尝试了 optimizeCompilations true 或 false,但错误再次出现.

Config

My web.config, I have try with maxBatchSize 20, 100, 1000, also with optimizeCompilations true or false, but the bug is appears again.

<compilation debug="false" defaultLanguage="C#" batch="true"
maxBatchSize="800" batchTimeout="10800" optimizeCompilations="false"
targetFramework="4.0">

现在有一些关于

的细节
  • 错误是随机的,有的编译出现,有的没有.
  • 这个项目很大,页面上每分钟都有很多人要求看东西,但当里面没有人时也会出现.
  • 在 64 位 dot.net 4 上运行,集成
  • 作为网络花园运行,但也经过测试,单独使用一个池(并遇到同样的问题)
  • 整个项目的会话已关闭.
  • 这些页面是从 2007 年开始运行的,但这个问题是在上个月出现的,不幸的是我找不到开始的位置和方式,或者是什么触发了它,因为我迟到了几天才看到它.
  • 只出现一个自定义控件加载,调用繁重的那个.
  • 我已将代码更改了 4 次,进行了小改动或大改动,但仍然存在.
  • 我尝试了 optimizeCompilations true 和 false 以及同样的问题.
  • 我也尝试过停止网络,删除所有临时文件,重新打开,然后又出现了.
  • 当应用程序开始只锁定一个编译时,我尝试在 global.asax 上放置一个互斥锁,但这也失败了.
  • 从有效的那一刻起,一切都很好,但如果无效,则不会自动更正.
  • 我加载此自定义控件的代码存在并在代码的多个位置、不同页面上调用.
  • 其他具有类似负载的自定义控件没有任何问题.
  • 此自定义控件的 ViewState 已禁用.
  • 我也尝试重新定位一些代码,通过微优化更改完整的函数调用,不再失败.
  • 在开发计算机上工作正常.我将 batch="true" 放在 web.config 上,错误立即出现.
  • 没有其他类似的问题,比如我们无论如何都无法修复的错误.系统运行了好几天,池根本没有回收,内存稳定,还有更多的免费使用.该程序已运行多年,但我们几乎每天都在更新.
  • 在同一个核心代码下运行多个站点(例如 stackexchange)并且都存在相同的随机问题.
  • AutoEventWireup 为假
  • 它出现在我加载的其他自定义控件上.
  • Now some details about

    • The error is random, in some compile appears, in some other not.
    • The project is a big one, the pages are live with a lot of people in every minute that ask to see something, but also appears when there is no one inside.
    • Is run on 64bit dot.net 4, Intergrated
    • Run as web garden but also tested and one pool alone (and get the same issue)
    • The session is off on the full project.
    • The pages are run from 2007 but this issue is appears the last month, unfortunately I can not find where and how is started, or what is trigger it because I late some days to see it.
    • Appears only one one custom control loads, the one that have heavy call.
    • I have change 4 times the code making small changes, or big changes and still there.
    • I have try with optimizeCompilations true and false and the same issue.
    • I have try also by stopping the web, delete all temporary files, reopening, and there was again.
    • I have try to place a mutex on global.asax when the application starts to lock only one compile at the time, but this fails also.
    • From the moment that works, then all is good, but if not works is not auto corrected.
    • The code that I load this custom control is exist and called in more than one places on the code, on different pages.
    • Other custom controls, with similar load did not have any problems.
    • ViewState is disabled for this custom control.
    • I have also try relocate some code, change the full function call with micro optimizes, no again fail.
    • Is work fine on development computer. I place batch="true" on web.config and the bug appears right away.
    • There are no other issues like that, like a bug that we can not fix no matter what. The system is run for days, the pool is NOT recycle at all, the memory is stable, and there is more free to use. The program is run for years now, but we change is almost every day with updates.
    • Under the same core code runs more than one sites (something like stackexchange) and all have the same random problem.
    • The AutoEventWireup is false
    • Its appears and on other custom control that I load the same way.
    • 当出现此错误时,我现在做的解决方法是:我只是强制项目重新编译并稍作更改,错误就会消失,直到下一次更新.

      What I do now as workaround when this bug appears: I just force the project to recompile with a small change, and the error go away, until the next update.

      我有一个错误,试图解决最后几周的树,但找不到原因.我已经尝试了几乎所有我能做的事情,但都失败了,并且错误再次出现.所以我在这里发帖,也许有些人可以帮助我并找到解决办法.

      I have a bug that try to solve the last tree weeks with out find the reason. I have try almost anything I can thing of, but all fails, and the bug appears again. So I post here maybe some can help me out and find a way out of this.

      最后一句话:这个错误很疯狂,自定义控件是一样的,我在上面做任何事情我只动态加载它并繁荣,编译器出于某种只有他知道的原因有两次不同的时间 - 随机.

      Last word: This bug is crazy, the custom control is the same, I do anything on it I only load it dynamically and boom, the compiler is have it two different times for some reason that only he knows - randomly.

      我能够在开发人员机器上重现该错误.在那里我发现包含这个自定义控件的两个 dll 模块有不同.

      I been able to reproduce the bug on the developer machine. There I discover that the two dll modules that contains this custom control have a different.

      其中一个是 4 个自定义控件的捆绑包.另一个模块是单独的自定义控件.

      The one was a bundle of 4 custom controls together. The other module was the custom control alone.

      在尝试修复此错误的树数周后,我最终发现此错误是在编译器批量编译目录并将许多不同的自定义控件捆绑在同一个 dll 中时出现的.所以当我尝试单独加载它时会抛出这个异常.

      After tree weeks trying to fix this bug I end up that this bug is appears when the compiler make batch compile of a directory, and bundle many different custom controls, in the same dll. So when I try to load it alone is throw this exception.

      所以我将有问题的自定义控件单独移动到不同的目录中,似乎我现在避免使用它.

      So I move the problematic custom control in a different directory alone and seems that I avoid it for now.

      即使在我将一些文件移动到不同的目录之后也会再次出现.是随机的,找不到与触发它的原因的明确联系.

      Appears again, even after I move some files to a different directory. Is random and can not find a clear connection with what is triggers its.

      因为我们发现这里的主要问题是批量编译 (batch="true") 在同一个 dll 上编译许多自定义控件,这是对编译器说不做的一种方式那就是 maxBatchGeneratedFileSize范围.我使用它的值是 100,但问题又出现了,现在我将它降低到 40 并测试它.

      Because we have spot that the main issue here is the batch compile (batch="true") that compiles on the same dll many custom controls, one way to say to the compiler to NOT do that, is the maxBatchGeneratedFileSize parameter. I use it with a value of 100, and the issue appears again, now I have lower it to 40 and test it.

      maxBatchGeneratedFileSize="40"
      

      推荐答案

      当您打开批处理并且在目录级别有某种形式的循环引用时,可能会发生这种情况.

      This can happen when you have batching turned on and have some form of circular references at the directory level.

      请查看此答案以准确了解在这种情况下,我所说的循环引用"是什么意思,因为含义非常微妙.

      Please see this answer to see exactly what I mean by 'circular references' in this context, as the meaning is quite subtle.

      如果您设法打破循环(例如,通过将用户控件移动到别处),您将不会遇到此问题.

      If you manage to break the cycle (e.g. by moving a user control elsewhere), you will not hit this issue.

      我认为理论上,这只能由循环引起,但有时很难检测到.

      I would think that in theory, this can only be caused by a cycle, but sometimes they can be hard to detect.

      我会给你一个我认为可行并且很容易尝试的替代解决方案(尽管它有点小技巧).在给您带来问题的用户控件中,在指令中添加以下属性:

      I'll give you an alternative solution which I think will work and is very easy to try (even though it is a bit of a hack). In the user control(s) that is giving you problems, add the following attribute in the directive:

      <%@ Control Language="C#" [...] CompilerOptions="/define:dummy1" %>
      

      如果您在其他一些控件中看到此内容,则可以添加相同的内容,但使用 dummy2、dummy3 等...

      If you see this with some other controls, you can add the same thing but with dummy2, dummy3, etc...

      这将产生不批处理这一用户控件的效果,因为它具有与其他用户不同的编译需求.从技术上讲,您可以添加任何 C# 命令行作为 CompilerOptions,但虚拟 /define 是最简单且最无害的.

      This will have the effect of not batching this one user control, since it has different compilation needs from the others. Technically, you can add any piece of C# command line as the CompilerOptions, but a dummy /define is the simplest and most harmless.

      但与全局关闭批处理不同,性能影响很小,因为只有非常小的页面子集不会被批处理.

      But unlike turning off batching globally, the perf impact will be minimal, since only a very small subset of pages will not be batched.

      顺便说一句,不用说,您看到的是 ASP.NET 中的一个错误,而且这个错误可能已经存在 10 多年了!也许在某个时候它应该得到解决:)

      BTW, it goes without saying that what you're seeing is a bug in ASP.NET, and that bug has been there for probably 10+ years! Maybe at some point it should get addressed :)

      这篇关于相同类型的对象的 InvalidCastException - 自定义控件加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:相同类型的对象的 InvalidCastException - 自定义控件加载

基础教程推荐