Add Assembly (in Web.Config) required for assembly in GAC - asp.net

Please see the code below (Image Hander):
<%# WebHandler Language="VB" Class="com.Genie.PresentationLayer.Web.ImageHandler1" %>
Imports com.Genie.BusinessLogicLayer
Namespace com.Genie.PresentationLayer.Web
Public Class ImageHandler1
Inherits ImageHandler
Private p1 as Person
Public Sub New()
End Sub
end class
end namespace
com.Genie.BusinessLogicLayer was added to the GAC today. The application produces a runtime error when it gets to the Image Handler: 'Type Person is not defined'. Adding an assembly to the web.config as follows resolves the problem:
<assemblies>
<add assembly="BusinessLogicLayer, Version=1.0.0.0, Culture=neutral, PublicKeyToken="669e0ddf0bb1aa2a"/>
</assemblies>
Why do I have to amend the web.config? I thought 'Add Assembly' was for website projects that do not use MSBuild (and therefore do not have 'References'). This is a web application project.

The references you add to a project are used when the project is compiled, but an ASPX page or ASHX handler is compiled when it is first requested. At this time the references are lost, so you'll need to provide them yourself.
The alternative would be precompiling your ASPX pages, causing the error to be caught at compiletime as opposed to runtime, or moving the code to codebehind so your project's references will be used.

Related

VS2015 - VB/ASP.NET Build fake error: "Type 'XYZ' is not defined" in UserControl code-behind

Ok guys, this one is not easy...
For the story, I got a old 3.5 ASP.NET/VB.NET website project (ASPX) build on VS2012 that I've upgraded to 4.5 to run well on VS2015.
I'm also trying to remove all the compilations errors that it got, even if the website was working under them (VB.NET is weird...)
During this operation, I've got some unchanged codes that worked well before but they don't let me run anymore.
So, here is the build problem:
I've got a UserControl (private/UserControls/Fiche/OngletAccueil.ascx), used in a page (private/fiche.aspx), that need some values from the MasterPage (private/MasterPage.master).
They all use the same namespace Vita, here are some excerpts to understand:
The MasterPage:
Namespace Vita
Partial Public Class private_MasterPage
Inherits System.Web.UI.MasterPage
Public _debug As Integer = HttpContext.Current.Request.Params("debug")
Public ReadOnly Property ModeDebug As Integer
Get
Return _debug
End Get
End Property
...
The UserControl:
Namespace Vita
Public Class private_UserControls_Fiche_OngletAccueil
Inherits System.Web.UI.UserControl
Public ReadOnly Property modeDebug As Integer
Get
Return DirectCast(Me.Page.Master, private_MasterPage).ModeDebug
End Get
End Property
...
The ASPX page:
<%# Page Async="true" Title="" Language="VB" MasterPageFile="~/private/MasterPage.master" AutoEventWireup="false" CodeFile="fiche.aspx.vb" Inherits="Vita.public_fiche" %>
<%# Register TagPrefix="Tools" TagName="OngletAccueil" Src="UserControls/Fiche/OngletAccueil.ascx" %>
...
With that, I got a compilation error about the MasterPage class type in the UserControl:
Type 'private_MasterPage' is not defined.on \private\UserControls\Fiche\OngletAccueil.ascx.vb
I don't even understand why it don't run the IISexpress when it was ok on previous framework...
I show you the code to make you understand that the code is not really the problem: it seems that the problem come from intelliSense or from the compilation temp files, like if the MasterPage class can't be seen (scope access) by the UserControl because they are not in the same temp assembly.
Before, I've got the same problem with a userControl used into another userControl (which files are saved in exactly the same folder...), but I was able to trick the compilator with a web.config setting, it seems to work:
<pages>
<controls>
<add tagPrefix="Tools" src="~/private/UserControls/Fiche/PosteUser.ascx" tagName="PosteUser" />
<add tagPrefix="Tools" src="~/private/UserControls/Fiche/NotesDeFrais.ascx" tagName="NotesDeFrais" />
<add tagPrefix="Tools" src="~/private/UserControls/Fiche/Evals_CollaborateursInternes.ascx" tagName="EvalsCollaborateursInternes" />
</controls>
</pages>
But here, the MasterPage is not a control, it's a page, I can't use the same trick...
Thx for help guys, I've already spent a lot of time on it and I did analyzed a bunch of Q&A & MSDN docs before post it here, on this you're my last hope :'(
Here some last & useful infomation on the web.config:
<supportedRuntime version="v4.0"/>
...
<compilation debug="true" batch="false" strict="false" explicit="true" targetFramework="4.5">
...
<pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID">
...
Ok guys, I got it, by changing PC...
I think the problem come from compilating the project on different versions of VS.
I did only one compilation of my project on 2012 and then the compilation on 2015 made this unbreakable error...
So even if you purge some temp folders like AppData\Local\Temp\Temporary ASP.NET Files, AppData\Local\Temp\VWDWebCache or .vs\project\v14\.suo, it's not enough.
After some more debug on others things that appeared, I can now compile with 0 errors and publish also after bit more debug. Now I'm on some other problems on runtime, it's still not fully working like before but it's getting closer ;)
UPDATE
What I did before worked for the compilation but it didn't solved the runtime (the bug came back on runtime as InvalidCastException).
To solve the runtime, I just checked the Build/Publish option "use fixed naming and single page assemblies" and it solved my case :)
Unckeck also the build (and publish) option "Allow this precompiled site to be updatable"
Here some useful links: https://msdn.microsoft.com/en-us/library/hh475319(v=vs.110).aspx
https://msdn.microsoft.com/en-us/library/aa479044.aspx
http://forums.asp.net/t/960707.aspx

Register custom class in assembly with web project

I'm using web project and web forms.
I create Custom Class
Namespace CustomWebControls
Public Class NoValidationDDL
Inherits DropDownList
End Class
End Namespace
and i try to register this class in assembly web.config page section
<controls>
<add tagPrefix="customDDL" namespace="CustomWebControls" assembly="ae" />
</controls>
and try to use in ascx
<customDDL:NoValidationDDL ID ="id" runat="server"></customDDL:NoValidationDDL>
How can i do this in web project?
This should work (assuming the assembly name for your project is ae) - make sure that you build the project after creating/modifying your custom control. Unless updated 'ae.dll' is present in your bin directory, intellisense etc will not work.
Instead of modify web.config, you may also try register control at page(aspx) or control(ascx) level by adding register directive after page directive:
<%# Register Assembly="ae" Namespace="CustomWebControls" TagPrefix="customDDL" %>

Using <%# Assembly %> reference without CopyLocal

I am trying to write a standalone ASPX page (no codebehind) for reasons I won't get into. So I want to have some functions declared in the page. But those functions need to reference assemblies that aren't generally referenced by ASPX pages. This means that I need an <%# Assembly %> reference, as detailed here:
http://msdn.microsoft.com/en-us/library/d864zc1k.aspx
And it all works fine if I have my reference to the assembly set to CopyLocal = true. However, it is in my GAC, so I don't want to set it to copy local. But if I don't have the DLL in my /bin folder, the page does not compile due to a bad reference.
What do I have to do to get the <%# Assembly %> declaration to work against an assembly in the GAC?
Typically assemblies that are in the GAC are referenced in the web.config. Is your assembly referenced in the system.web -> compliation -> assemblies section? If not, adding it should resolve your issue.

ASP.NET "Reference required to assembly" error

I have an ASP.NET page A that uses a data layer assembly DAL. I reference the DAL assembly with a
<%# Assembly Name="NGC.DAL" %> tag since there is no project to add the reference to. The assembly itself I copied to the pages' bin folder.
The assembly DAL is a project that references System and System.Data.
Now when A calls a method in DAL, the compiler gives the error
BC30652: Reference required to assembly 'System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=969db8053d3322ac, Retargetable=Yes' containing the type 'System.Data.IDbConnection'. Add one to your project.
I tried referencing the assembly System.Data with a <%#Import Namespace="System.Data" %> tag and I tried copying it to the bin folder and referencing it with a
<%# Assembly Name="System.Data" %> tag, both in A directly. Nothing worked.
Why would a project A that uses project B which uses project C require a reference to B and C rather than just B and why does the way above not work?
This MSDN thread covers the problem but shows no solution that works.
EDIT: As Robert Wagner suggested I added the reference to the web.config.
Now I get another error message. BC30560: 'CommandBehavior' is ambiguous in the namespace 'System.Data'. which is odd because the one in the web.config is the only one I reference inside the page.
Maybe it's worth mentioning that the System.Data referenced and used in the DAL assembly is from the compact framework. But since it's the only one I use, where does the ambiguity come from?
Most likely you have inherited from a System.Data class in your DAL dll and are using that class in your website. It requires a reference to be able to resolve the references those classes otherwise the compiler would not know what the class contract is.
You should not need to copy in System.Data as it is in the GAC.
A better way would be to add the reference in your web.config file:
<system.web>
<compilation debug="true">
<assemblies>
<add assembly="System.Data, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=B77A5C561934E089" />
</assemblies>
</compilation>
</system.web>
When you say "there is no project to add references to" are you using Visual Studio? If so, why not use the Web Application or Web Site projects?

LoadControl on control with assembly reference

I'm trying to use System.Web.UI.TemplateControl.LoadControl to build a templated web part for a SharePoint application, and I'm having trouble with a control that references an external assembly.
I have a user control with the first few lines like this:
<%# Control Language="C#" ClassName="MyControl" %>
<%# Assembly Name="AjaxControlToolkit" %>
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
The web part derives from a base webpart that implements the templating and some other utilities which is in an assembly called "MyWebParts" (for example), while the derived web part class is in "MyProject". MyProject has a reference to "MyWebParts" (obviously), and to "AjaxControlToolkit", but "MyWebParts" does not have a reference to "AjaxControlToolkit". At runtime, when the templated web part tries to load the user control (a .ascx file in the "controltemplates" SharePoint root folder), it calls the base methods it inherited across assemblies, and it throws an HttpParseException, indicating that it couldn't load a required assembly. The EventViewer confirms that the assembly in question is AjaxControlToolkit.
I tried adding the AjaxControlToolkit assembly to the web.config for sharepoint, and it's sitting in the GAC, but to no avail.
Anyone know how I can do this without having a reference to every assembly in existence in my "MyWebParts" assembly?
Add a reference to your assembly in the web.config assemblies section, this will force sharepoint to load that assembly into memory, like so:
<assemblies>
<add assembly="AjaxControlToolkit, Version=1.0.10920.16768, Culture=neutral, PublicKeyToken=28f01b0e84b6d53e"/>
</assemblies>
P.S. this is for ajax 1.0, correct the assembly version if you are using the AJAX 3.5 version.

Resources