简体   繁体   中英

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:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\Temporary ASP.NET Files\\MyApplication\\bdb24adf\\eb592999\\App_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

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 SDKs\\Windows\\v10.0A\\bin\\NETFX 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 .

The type 'MyDataModelClass' is defined in an assembly that is not referenced. You must add a reference to assembly 'MyAssembly, Version=40.0.0.30, Culture=neutral,

Do you actually have a Reference in your project to whatever assembly (DLL) that that class is defined in? The error thinks you don't.

Fix it by adding the DLL here and setting it to Copy Local:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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