Combatting GAC Reference Errors

PowerTools 2004-05-09 05:09:51
Building a WebControl can pretty straight forward, especially when you put all of your classes into a single assembly. However, many times it makes more sense to separate out the classes which provide common functionality, into another 'shared' assembly, that can be re-used for multiple projects. The problem is, when an ASPX page is compiled which includes this custom control, the compiler will not look in the Global Assembly Cache (GAC) for the shared assembly, even though your project has a reference to it. When you try to view your page in a browser, you'll end up getting an error that looks something like this -

Server Error in '/WebApplication1' Application.
________________________________________
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS0234: The type or namespace name 'Shared' does not exist in the class or namespace 'Infragistics.WebUI' (are you missing an assembly reference?)

Source Error:


Line 12: <body MS_POSITIONING="GridLayout">
Line 13: <form id="Form1" method="post" runat="server">
Line 14: <iglbar:UltraWebListbar id="UltraWebListbar1" style="Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 8px" runat="server" BorderStyle="Ridge" BorderWidth="4px" Width="124px" Height="200px" BarWidth="100%">
Line 15: <Groups>
Line 16: <iglbar:Group TextAlign="Left" Text="Group"></iglbar:Group>

Source File: c:\inetpub\wwwroot\WebApplication1\WebForm1.aspx Line: 14

Notice that your control's assembly was found just fine, but your shared assembly, which your control has a dependency on, was not. Looking at the Detailed Compiler Output on the error page, you'll notice that your reference was not added to the compiler string which was used to dynamically compile this ASPX page. You can quickly get around the compilation problem by setting the "Copy Local" property to 'true' for the resource in your project. This will copy the assembly into your web application's bin directory, where the runtime will always look to find a resource. However, as a result of copying the assembly local, you lose all of the advantages of putting your assembly into the GAC. So the question remains, how do you tell .NET to search the GAC for your referenced assembly, when compiling an ASPX page?

There are actually a couple of different ways to do this. The first way, is to add an Assembly Directive to your ASPX page. The directive will specify the full name of the Assembly to look for, including the Version, Culture, and PublicKeyToken. Here is an example of what your tag should look like.

<%@ Assembly Name="Infragistics.WebUI.Shared.v2, Version=2.0.20041.10, Culture=neutral, PublicKeyToken=7dd5c3163f2cd0cb" %>

The drawback of adding the directive to the page becomes clearly evident when you have multiple pages in your application. You will need to include this Assembly Directive on each page that has a control which requires a reference to your shared assembly. This becomes a major maintenance problem when the version number of your referenced assembly inevitably changes. Luckily, there is another place you can use, which will be effective for your entire web application, not just a single page.

To get your entire project to search the GAC for this assembly, you can modify the Compilation section of your web.config file. You will need to add an Assembly tag, which again specifies the full name of your assembly. Here is an example of what the Compilation section would look like before -

<compilation
defaultLanguage="c#"
debug="true"
/>

And after -

<compilation defaultLanguage="c#" debug="true">
<assemblies>
<add assembly="Infragistics.WebUI.Shared.v2, Version=2.0.20041.10, Culture=neutral, PublicKeyToken=7dd5c3163f2cd0cb" />
</assemblies>
</compilation>

Now the compiler knows that it should look for a Strong Named assembly, and has all of the information it needs to retrieve that assembly from the GAC. If your resource assembly's version number should ever change, you only need to adjust the version number in one place - the web.config file.


...全文
87 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

3,423

社区成员

发帖
与我相关
我的任务
社区描述
其他开发语言 其他开发语言
社区管理员
  • 其他开发语言社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧