Code cannot find reference to assembly in GAC

Code cannot find reference to assembly in GAC

September 29th, 2006 // 7:09 pm @ Amar

I came across this error today which made me quite curious on why it is happening. This occured to me in a SharePoint WebPart, but it is a very general .NET Framework error and as such can occur in any type of .NET application.


CS0011: Referenced class someClass’ has base class or interface
someNamespace.someBase’ defined in an assembly that is not referenced. You must add
a reference to assembly ’someNameSpace’.


To explain the scenario. You have an assembly in the GAC with some classes. You have some code which you are running which reference the GAC dll. For my case, I was inheriting my webpart from one of the baseclasses defined in the GAC assembly. If I put the assembly in the /bin directory as well as the GAC it worked beautifully, but if I deleted the assembly from my /bin directory, I got the above error. This sort of nullified the entire purpose of having a GAC.


What was happening was that the code was trying to reference the someNamespace.someBase Class but as there was no link in the code to provide it with a hint that the required assembly is in the GAC, it was complaining. As soon as the dll was in the /bin directory it was able to reference it and all was ok. To solve the problem, there are two ways to do it.


1. If you have a ASP.NET application, you can add a @Register Assembly derivative in your page.
2. You can add the reference in your web.config to hint the application to look for the assembly in the GAC by adding the following lines in your web.config file..
<system.web>
<compilation batch=”false” debug=”true”>
<assemblies>
<add assembly=”someAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=s0m3publ1ck3y” />
</assemblies>
</compilation>
</system.web></code>


The solution was found, the day was won. But it made me wonder if this is a design defect with .NET. If placed in the bin directory, the application was very happy to reference the dll. If an assembly is in the GAC, why do we have to explicitely tell out application to look for the reference in the GAC. Shouldn’t it be doing that automatically in the first place.


Category : SharePoint & dotNet

2 Comments → “Code cannot find reference to assembly in GAC”


  1. http://

    3 years ago

    Hi,
    I m developing a Pocket PC Application which uses Sql Server Mobile. I am facing the same problem. In Pocket PC Application, there is no web.config file. I can see that the assembly which my application is looking for is in “c:\windows\assembly” folder. I want to ask you how to set reference to the assembly which is installed in GAC.

    Regards,
    Milind


  2. http://

    2 years ago

    It seems to me, and so far no one is picking up on this, (I’ve been scouring the web for a while now) I think the GAC is meant for a production enviornment rather than a developer’s. I assume the GAC is to save time when updating a .dll. If you have 5 sites that all use the ajaxtoolkit.dll in production, you can just update 1 file instead of 5. But in development you have to go through a separate step to use a custom GAC file. If you look at the references window, none of those files paths point to the GAC


Leave a Reply

Latest Posts

Testimonials

"Amar clearly has a thorough knowledge of Sharepoint, this knowledge, coupled with his professionalism and dedication made him a tremendous asset to the project. He was the key contributor to the later stages of the project delivery effort (the really difficult bit, long hours and hard work that puts huge demands on people), and I can state categorically that without his focussed dedication and hard work we would not have been able to meet the timescales imposed upon the project."

Tim Ellis , Sharepoint Project Manager , Royal Bank of Scotland

Subscribe Now