简体   繁体   English

错误 BC30002 - 类型 XXX 未定义

[英]Error BC30002 - Type XXX is not defined

OK, this begins to drive me crazy.好吧,这开始让我发疯。 I have an asp.net webapp.我有一个asp.net webapp。 Pretty straightforward, most of the code in the .aspx.vb, and a few classes in App_Code.非常简单,.aspx.vb 中的大部分代码,以及 App_Code 中的一些类。

The problem, which has begun to occur only today (even though most of the code was already written), is that once in a while, I have this error message :直到今天才开始出现的问题(即使大部分代码已经编写),是偶尔出现以下错误消息:

Error BC30002: Type 'XXX' is not defined错误 BC30002:未定义类型“XXX”

The error occurs about every time I modify the files in the App_Code folder.每次修改 App_Code 文件夹中的文件时都会发生错误。 EDIT : OK, this happens also if I don't touch anything for a while then refresh the page.编辑:好的,如果我暂时不触摸任何东西然后刷新页面,也会发生这种情况。 I'm still trying to figure out exactly how to trigger this error.我仍在试图弄清楚如何触发此错误。

I just have to wait a little bit without touching anything, then refresh the page and it works, but it's very annoying.我只需要稍等片刻,不要碰任何东西,然后刷新页面就可以了,但是很烦人。

So I searched a little bit, but nothing came up except imports missing.所以我搜索了一点,但除了缺少导入之外什么也没找到。 Any idea ?任何的想法 ?

This happened to me after I added a new project to an old solution.在我将新项目添加到旧解决方案后,这发生在我身上。 I lowered the Target framework to match that of the other 'older' projects and the error went away.我降低了 Target 框架以匹配其他“旧”项目的框架,错误消失了。

I think I found the problem.我想我发现了问题。

My code was like that :我的代码是这样的:

Imports CMS

Sub Whatever()
    Dim a as new Arbo.MyObject() ' Arbo is a namespace inside CMS
    Dim b as new Util.MyOtherObject() ' Util is a namespace inside Util
End Sub

I'm not sure why I wrote it like that, but it turns out the fact I was calling classes without either calling their whole namespace or importing their whole namespace was triggering the error.我不确定我为什么这样写,但事实证明,我在调用类时既没有调用它们的整个命名空间,也没有导入它们的整个命名空间,这触发了错误。

I rewrote it like this :我是这样改写的:

Imports CMS.Arbo
Imports CMS.Util 

Sub Whatever()
    Dim a as new MyObject()
    Dim b as new MyOtherObject()
End Sub

And now it works...现在它起作用了......

Sounds like a pre compile issue, particularly because you mention that you get the error and then wait and it disappears.听起来像是预编译问题,特别是因为您提到您收到错误,然后等待它消失。 ASP.NET may be still in the process of dynamically compiling your application or it has compiled the types into different assemblies. ASP.NET 可能仍处于动态编译应用程序的过程中,或者已将类型编译到不同的程序集中。

With dynamic compilation, you are not guaranteed to have different codebehind files compiled into the same assembly.使用动态编译,不能保证将不同的代码隐藏文件编译到同一个程序集中。 So the type you are referencing may not be able to be resolved within its precompiled assembly.因此,您引用的类型可能无法在其预编译程序集中解析。

Try using the "@Reference" directive to indicate to the runtime that your page and the file that contains your type should be compiled into the same assembly.尝试使用“@Reference”指令向运行时指示您的页面和包含您的类型的文件应该编译到同一个程序集中。

@ Reference - MSDN @参考 - MSDN

Check for a compiler warning (Output window of Visual Studio) "warning : The following assembly has dependencies on a version of the .NET Framework that is higher than the target and might not load correctly during runtime causing a failure".检查编译器警告(Visual Studio 的输出窗口)“警告:以下程序集依赖于高于目标的 .NET Framework 版本,并且可能在运行时无法正确加载导致失败”。 This happens when one of your dlls is compiled with a newer version of dotnet.当您的 dll 之一使用较新版本的 dotnet 编译时,就会发生这种情况。 If your current project is set to use a lower version of dotnet, the dependency chain prevents the dll (with the higher dotnet ver) from loading.如果您当前的项目设置为使用较低版本的 dotnet,则依赖链会阻止加载 dll(具有较高的 dotnet 版本)。 It gives a compile error in Visual Studio, but can still run in IIS.它在 Visual Studio 中出现编译错误,但仍然可以在 IIS 中运行。

如果引用相等,请先从备份中替换 vbproj 和 vbproj.user 文件

Sounds like it happens every time the website spins up (the app gets recycled every time you touch app_code and probably you have IIS configured to shut down the website after X minutes of inactivity).听起来每次网站启动时都会发生这种情况(每次您触摸 app_code 时该应用程序都会被回收,并且可能您已将 IIS 配置为在 X 分钟不活动后关闭网站)。

I bet it has something to do with the asp.net worker process not having the correct access rights on the server.我敢打赌这与 asp.net 工作进程在服务器上没有正确的访问权限有关。 So its trying to load an assembly and is being denied.所以它试图加载一个程序集并被拒绝。

Check this link and Table 19.3 for a list of all the folders the worker process account must have access to in order to function.检查此链接和表 19.3 以获取工作进程帐户必须有权访问才能运行的所有文件夹的列表。 And don't forget to give it rights to all files and folders in your virtual directory!并且不要忘记授予它对虚拟目录中所有文件和文件夹的权限!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM