InvalidCastException的相同类型的对象 - 自定义控制负载 [英] InvalidCastException for Object of the same type - Custom Control Load

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

问题描述

我有一个很有线的错误,我的自定义控件的一个似乎是创建两个编译的文件,当我尝试使用 LoadControl动态加载()只是失败,因为无法施展的一方向另一方 - 即使他们是完全一样的。我写的消息看到,所有都是一样的,只是改变编译DLL。

  System.Web.HttpUnhandledException(0x80004005的):
 类型'System.Web.HttpUnhandledException'引发的异常。 --->
    System.InvalidCastException:
[A] ASP.Modules_OneProduct_MedioumImage不能转换为
[B] ASP.Modules_OneProduct_MedioumImage。   键入从App_Web_kg4bazz1,版本= 0.0.0.0,文化=中立,公钥=空'A起源
在上下文中默认
    在位置C:\\ WINDOWS \\ Microsoft.NET \\ Framework64 \\ v4.0.30319 \\临时ASP.NET文件\\ ROOT \\ 80ed7513 \\ 10eb08d9 \\ App_Web_kg4bazz1.dll。   B型从起源'App_Web_oneproduct_mediumimage.ascx.d1003923.4xoxco7b,版本= 0.0.0.0,文化=中立,公钥=空'
在上下文中默认
    在位置C:\\ WINDOWS \\ Microsoft.NET \\ Framework64 \\ v4.0.30319 \\临时ASP.NET文件\\ ROOT \\ 80ed7513 \\ 10eb08d9 \\ App_Web_oneproduct_mediumimage.ascx.d1003923.4xoxco7b.dll。

的code

这是code,因为它是现在在我有跟随的什么是写在MSDN

 的foreach(INT OneProductID在TheProductIdArrays)
{
    //这里是抛出。
    ASP.Modules_OneProduct_MedioumImage OneProduct =
        (ASP.Modules_OneProduct_MedioumImage)LoadControl(@〜/ MOD / OneProduct_MediumImage.ascx);    //做一些工作,
    // OneProduct
}

previously我有没有加载在 ASP控制。但毕竟这错误出现,并寻求解决方案,我严格遵循什么MSDN上。该缺陷是仍然在这里,无论我做什么。

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

 <%@注册SRC =〜/ MOD / OneProduct_MediumImage.ascx变量名=OneProduct_MediumImage标签preFIX =UC1%GT;
<%@参考控制=〜/ MOD / OneProduct_MediumImage.ascx%GT;

配置

我的web.config中,我尝试用 maxBatchSize 20,100,1000,还与 optimizeCompilations true或假的,但这个错误是会再次出现。

 <编译调试=假defaultLanguage =C#批=真
maxBatchSize =800batchTimeout =10800optimizeCompilations =假
targetFramework =4.0>

现在的一些细节


  • 的误差是随机的,在显得有些编译,在其他一些没有。

  • 该项目是一个大的,页面是活的有很多人在每分钟的要求看的东西,还当里面有没有人出现。

  • 是64位dot.net运行4,Intergrated

  • 运行作为Web园也考验,独自一池(并获得同样的问题)

  • 会议为关整个项目。

  • 的页是从2007年运行,但这个问题似乎上个月,可惜我找不到在哪里以及如何启动,或者是什么触发它,因为我迟到了几天看看吧。

  • 只显示一个一个自定义的控制负荷,有沉重的通话之一。

  • 我修改了code 4次作出小的修改,或大的变化,仍然在那里。

  • 我有 optimizeCompilations 试试真假同样的问题。

  • 我也曾经尝试通过停止网页,删除所有临时文件,重新打开,并再次出现了。

  • 我也尝试把一个互斥体上的global.asax在应用程序启动的时间来锁定只有一个编译,但失败也。

  • 从时运作的瞬间,那么一切都很好,但如果没有作品不自动校正。

  • 的code,我加载此自定义的控制是存在的,并呼吁在上code以上的地方,在不同的页面。

  • 其他自定义控件,具有类似的负载没有任何问题。

  • ViewState是此自定义控制禁用。

  • 我也尝试迁往一些code,改变全功能调用微优的,没有再次失败。

  • <击>是开发计算机上做工精细。我把 =批次真实在web.config文件和错误出现的时候了。

  • 有没有其他的问题,如,像我们无法修复不管是什么错误。该系统运行天,池是不是在所有循环中,存储器是稳定的,并且有更多的自由使用。在程序运行了多年,但是我们改变的是,几乎每天都更新。

  • 在相同的核心code同时运行多个网站(像stackexchange),并都具有相同的随机问题。

  • 的AutoEventWireup是假

  • 其出现和其他自定义的控制,我加载相同的方式。

我现在做的是解决办法时,会出现这个错误。我只是强制项目,一个小的变化重新编译,错误消失,直到下一次更新

我有尝试解决最后一棵树周出去寻找原因的错误。我有尝试几乎什么我可以的事情,但都失败,错误再次出现。所以我张贴在这里也许有些可以帮我找到出路了这一点。

最后一个字:此错误是疯了,自定义控件是一样的,我做任何事情,我只动态加载和繁荣,编译器有它两个不同的时间,只有他知道一些原因 - 随机<。 / p>

更新1

我已经能够重现开发机器上的错误。在那里,我发现包含该自定义控件的两个DLL模块有不同的。

该之一是4自定义控件捆绑在一起。另一个模块是单独的自定义控件。

解决方法

树周试图解决这个错误我结束了,这是错误出现时,编译器创建一个目录中的批处理编译和捆绑许多不同的自定义控制,在同一个DLL后。所以,当我尝试单独加载它是引发此异常。

所以我谨独在不同的目录中有问题的自定义控件,似乎我避免它现在。

更新2

再次出现,即使我一些文件移动到不同的目录。是随机的,不能找到什么触发其清晰的连接。

更新3

由于我们有现货,这里的主要问题是批处理编译(批=真正的),它在相同的dll许多自定义控件编译,单程说到编译器那样做,是<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.configuration.compilationsection.maxbatchgeneratedfilesize.aspx\"><$c$c>maxBatchGeneratedFileSize参数。我用它值100,这个问题再次出现,现在我有40个低并测试它。

  maxBatchGeneratedFileSize =40


解决方案

当你配料打开这可能发生,有某种形式在目录级循环引用。

请参阅此<一个href=\"http://stackoverflow.com/questions/14784369/error-cs0433-in-large-$p$pcompiled-asp-net-website-project\">answer看正是我在这样的背景下循环引用指的是,意义是相当微妙的。

如果你成功地打破周期(例如,通过在其他地方移动的用户控件),你就不会遇到这个问题。

更新1

我认为从理论上讲,这只能通过一个循环造成的,但有时也可以是防不胜防。

我给你一个替代解决方案,我认为会工作,是很容易去尝试(即使它是一个黑客位的)。在这是给你的问题​​在​​用户控制(S),在指令添加以下属性:

 &LT;%@控制语言=C#[...] CompilerOptions =/定义:dummy1%&GT;

如果你看到这与其他一些控件,可以添加同样的事情,但dummy2,dummy3,等等...

此将没有配料这一个用户控制的效果,因为其具有不同的编译与其他需要。从技术上讲,你可以添加任何一块C#命令行的 CompilerOptions ,而是一个虚拟的的/定义是最简单,最无害的。

但是,与全球范围内关闭配料中,PERF影响将是最小的,因为只有一个页面非常小的子集将不进行批处理。

顺便说一句,这不用说,你看到的是ASP.NET中的错误,并且错误已经存在了大概10年以上!也许在某些时候它应该得到解决:)

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:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root\80ed7513\10eb08d9\App_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:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root\80ed7513\10eb08d9\App_Web_oneproduct_mediumimage.ascx.d1003923.4xoxco7b.dll'.

The code

This is the code as it is right now after I have follow exactly what is written on 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
}

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" %>

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">

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.

Update 1

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.

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

Workaround

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.

Update 2

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.

Update 3

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.

Update 1

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" %>

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

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.

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的相同类型的对象 - 自定义控制负载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆