Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
174 views
in Technique[技术] by (71.8m points)

c# - Why is my use of a generic method in a code-behind class resulting in a dynamic compilation exception?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Edit 2: This looks like the solution to your problem: Strange Error - CS0012: The type x is defined in an assembly that is not referenced

But no real explanation why.


Edit:

Since your assembly is in the GAC, it should be finding it and not require Copy Local.

Make sure the version in the GAC is the version it's looking for. You can use Gacutil to check what's in the GAC. On my machine, gacutil.exe is in 'C:Program Files (x86)Microsoft SDKsWindowsv10.0AinNETFX 4.6.1 Tools', but it might be somewhere else on your machine. Once you find it, run this from the command line:

gacutil /l MyDataModelAssembly

The version and public key token output by gacutil must match what you see in the error message.


Your assembly DLL that defines MyDataModelClass has to be copied to the location that IIS is running the application from.

The Copy Local setting tells Visual Studio to copy the DLL to the compile output path (which is likely where IIS is running the application from). You will need Copy Local set to true for any assembly that is not in the GAC.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...