Error BC30002 - Type XXX is not defined

2019-02-22 02:52发布

问题:

OK, this begins to drive me crazy. I have an asp.net webapp. Pretty straightforward, most of the code in the .aspx.vb, and a few classes in 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

The error occurs about every time I modify the files in the App_Code folder. 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 ?

回答1:

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.

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



回答2:

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



回答3:

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.



回答4:

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

I bet it has something to do with the asp.net worker process not having the correct access rights on the server. 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. And don't forget to give it rights to all files and folders in your virtual directory!



回答5:

Replace your vbproj and vbproj.user file from your backup before if the references are equal