[Resolved]Cannot Consume/Access/Use Asp .Net Classes in App_Code / Bin folder and WebService ‘Compilation Error’

Problem

We can create Asp .Net classes or bussiness objects inside App_Code or using compiled library .dll file in Bin folder.
Alternatively classes may be write inside WebService. If nothing is wrong then we can access our web app classes when it is launched via Visual Studio.
Unfortunately, after we deploy it to IIS and try to access deployed classes or WebService an error “Compilation error .. Type xxx is not defined ..” occurs.

Solution

Let’s make a New Website Asp .Net Empty Web Application to make problem and solution clear.

  1. Create a new Website with location as File System
  2. Add new item -> WebForm -> Default.aspx. Write one label control inside form tag:
    <form id="form1" runat="server">
        <div>
            <asp:Label ID="msg" runat="server"></asp:Label>
        </div>
        </form>
    
  3. Add New Item -> Class -> Hello.vb -> Place it to App_code folder. Hello class have one function only to return ‘Hello World’:
    Imports Microsoft.VisualBasic
    
    Public Class Hello
        Public Sub New()
    
        End Sub
        Public Function SayHello() As String
            Return "Hello World"
        End Function
    End Class
    
  4. At Code behind Default.aspx write inside Page_load event:
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim hlo As New Hello()
        msg.Text = hlo.SayHello()
    End Sub
    

    So when page is load the msg label control text will be set to ‘Hello World’

  5. Right click Default.aspx from solution explorer and then view in browser. The page contains Hello World text without error. So, when this website launched via Visual Studio no error occured.

Let’s try using users point of view via IIS.
FYI, I have IIS virtual directory ‘aspapp’ and this web app location is in folder ‘AspNetCode’ under ‘aspapp’ virtual dir.

So if I access the web app with url: http://localhost/aspapp/aspnetcode/Default.aspx then it should be ok but I was wrong. A ‘Compilation error. Type ‘Hello is not defined’ occur

The solution is quite simple, make this ‘AspNetCode’ web app folder as a virtual directory.

  1. Open IIS and create new virtual directory and names alias as ‘AspNetCode’
  2. Browse to ‘AspNetCode’ folder and then make sure you choose ‘write’ and ‘execute’ checkbox, finish

Try again with url: http://localhost/AspNetCode/Default.aspx (without aspapp part because aspnetcode is virtual dir now) then it is ok now. 🙂

Alternative 1, you can right click website app at solution explorer and then click copy website.

  1. Click Connect
  2. Choose Local IIS or FTP or Remote and then click create new web application icon name it ‘AspNetCode’
  3. Block all source code on left pane and copy them to the right pane

Alternative 2, Create a new website with web location of HTTP

  1. Create a new website application
  2. Choose Web Location to HTTP and write http://localhost/AspNetCode/ on its textbox, do not choose file system
  3. Write all source code from start

Alternative 3, Create a new project and choose asp .net empty website. Write necessary source codes and publish the project to IIS to make sure we create/upload to a virtual directory

Bin folder is created when we reference library (.dll) file. This .dll file is pre-compiled class. Problem and solution are same as above.

WebService

Same problem with same solution.

  1. Create a new website choose Web Location to HTTP and fill textbox with http://localhost/UsingWebService
  2. Add new item -> WebForm -> Default.aspx. Add one msg label same as above Default.aspx
  3. Add new item -> Web Service -> Hello.asmx. Leave code as it is
  4. Right click website app at solution explorer, click add web reference, click Web Services in this Solution, choose Hello.
  5. Write Web Reference Name to HelloWs
  6. Edit code behind file of Default.aspx:
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    	Dim hlo As New HelloWs.Hello()
    	msg.Text = hlo.HelloWorld()
    End Sub
    

    Same as above, this website intend to write ‘Hello world’ at Page load.

  7. Run this website with right click Default.aspx file, click view in browser or through IIS with url: http://localhost/UsingWebService/Default.aspx. It should be no error

To know more about Asp .Net Webservices just click this url: Quickly Mastering: Create And Use Asp .Net Web Service From Basic, Database Access To Ajax-Enabled

Summary

Solution to the problem Compilation Error when accesing app_code / bin folder or WebService is making a virtual directory or website refer to your website application root folder. The classes under app_code / bin folder must on root of virtual directory.

Regards,
Agung Gugiaji

4 responses to “[Resolved]Cannot Consume/Access/Use Asp .Net Classes in App_Code / Bin folder and WebService ‘Compilation Error’

  1. I have an app_code folder on the root, and my pages are under /myAppfolder, which has to be an application by design because of custom app recycle. So I need to be able to launch “http://myweb/myAppfolder/myapp.aspx”, but I can’t because on live IIS the page can’t see the app_code code (while I can test it correctly using “browse this page” from VS). I can’t create a new “myFolder” virtual directory (not app), otherwise the app custom recycle will not take place… Do you have a solution for this particular case too ? All this mess was not happening with IIS 6…

    • Hi,

      You place the app_code folder on root and you pages are under /myAppfolder. Where is ‘root’ that you mean ? app_code folder should be under /myAppfolder too.

      Assuming you place the app_code folder in correct place then what is custom app recycle ? Are you write your own code in app_code or it is already compiled library i.e *.dll ?

      Regards

  2. Hello many thanks for your response. The root in my case is the / of the website. So I have /app_code and I have like 9 application folders who are using that code, under IIS6, without any problem.

    After migrating the same website under IIS7 it’s not working any more… and I would like to avoid having to phisically copy one app_code folder under every appfolder (if I need to update one app_code page, I would need to copy it again under all folders)…

    For “custom app recycle” I mean that every appfolder has a different and customized recycle logic so I can’t have them just as “virtual directories”… and yes, I tried both to put my aspx files in /app_code -and also- removing /app_code, compiling them to dll and putting them on a “/bin”. But the problem is always the same: I can’t access shared library from different appfolders unless I create a “app/code” folder inside every appfolder.

    Is there a wayout ?

  3. Hello I found a solution on a post by Luis Dragotto (thanks!) and decided to post here for anyone: I needed to create a “symbolic link” (like a “windows shortcut”, but it’s different) from the filesystem for every folder I needed access to app_code. For example run a cmd prompt, then type mklink /D “App_Code” “C:\inetpub\wwwroot\www.mysite.com\myfolder”. That solved my problem! Thanks anyway for your support!

Leave a comment