After making some changes to a legacy ASP.Net Web Forms app I'm working on, I started receiving the following exception whenever I tried to visit a page of the application.
C:WindowsMicrosoft.NETFrameworkv4.0.30319Temporary ASP.NET FilesMyApplicationdb24adfeb592999App_Web_mypage.aspx.cdcab7d2.dc1wid-d.0.cs(148): error CS0012: The type 'MyDataModelClass' is defined in an assembly that is not referenced. You must add a reference to assembly 'MyDataModelAssembly, Version=40.0.0.30, Culture=neutral, PublicKeyToken=7ca3fb5049101832'
After hours of troubleshooting, I finally narrowed down the problem to a method I had added to a base page class from which all pages in the application derive. Call it MyBasePageClass
.
protected T CreatePlaceholder<T>(T item) where T : MyDataModelClass, new()
{
return new T { TemporaryIdentifier = item.TemporaryIdentifier };
}
When I comment out that method, the exception goes away and everything works as I would expect. If I add an assembly reference in the web.config file, this exception also goes away. Oddly, when I change the reference to Copy Local = true, this also makes the exception go away.
This is not the first time that MyDataModelClass has been incorporated into the application. It is used in scores of places in almost every code behind file. But for some reason, using it as a type constraint is more than .Net can handle.
I can only imagine that It has something to do with the nature of generics, but I have no idea what that might be. I cannot be the first to have tried this, but I can't find any information about it on SO or anywhere else. Could it have something to do with how IIS is configured?
Update:
MyDataModelAssembly is indeed installed in the GAC. Here is the output of gacutil /l:
The Global Assembly Cache contains the following assemblies:
MyDataModelAssembly, Version=40.0.0.30, Culture=neutral, PublicKeyToken=7ca3fb5049101832, processorArchitecture=MSIL
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…