0

I'm trying to use an external code file to include some helper classes for several .ashx handlers. The example one is using ListToJSON, which just turns nested Lists (Theres stuff like List<List<whatevers>> that are being worked with) into JSON (which will get thrown into context.response)

The ListToJSON class works fine when it's in the same file. I'm trying to put it into a Helper.cs file which is included in the same project in VS2010, because these classes get used in several different handlers.

I was under the impression that "using Helper;" was what I needed to do, but I still get the error "The type or namespace Helper could not be found (are you missing a directive or assembly reference?" (and intellisense doesn't see it either)

I've also tried putting both code files into the same namespace. Same error.

It's not a DLL file, it's just a c# code file in the same project. Should I realy be compiling it as a DLL to do this? If so, how do I do that? (Once I do, I can do the right click->add reference, correct?)

I think I'm supposed to be using the App_Code folder, but I'm not really sure how to set that up in VS so that it's referenced properly.

My handler file (skeleton)

<%@ WebHandler Language="C#" Class="generateReport" %>

using System;
using System.Web;
using System.Configuration;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Diagnostics;
using Helper;
public class generateReport : IHttpHandler {

    public void ProcessRequest (HttpContext context) {
        ListToJSON Converter = new ListToJSON();
        //context.Response stuff goes here.
    }
}

My helper file, Helper.cs - this is in the same project directory and is included in the project:

using <stuff>;
namespace Helper
{
    public class ListToJSON
    {
        //class definition here
    }
}

UPDATE: So, I put Helper.cs into the /App_Code/ folder, and it seemed to be playing nicely. Intellisense was picking up things in Helper.cs after I did using Helper; When I tried it on IIS, it I got the following familiar error:

Compiler Error Message: CS0246: The type or namespace name 'Helper' could not be found (are you missing a using directive or an assembly reference?)
Line 19: using Helper;
Source File: <path>\info.ashx    Line: 19

I get no errors when running this through visual studio's IIS emulator. When I run it through IIS (localhost) I get the IIS internal server error described. App_Code folder was made through VS, I right clicked and chose Add ASP.NET Folder > App Code\

Edit: Added tag iis

sylverfyre
  • 3,049
  • 3
  • 17
  • 15
  • This is pretty weird. Try putting it in App_Code and see if that fixes it. You don't have to set any references up when everything is in the same project. – Oliver Jun 12 '12 at 16:29
  • I have used a quick fix by putting my helper class in an App_Code folder. Looking forward to seeing a real answer, though. – BumbleB2na Jun 12 '12 at 16:29
  • Yeah, I think the App_Code folder is actually the "Correct" way to do this (assuming you don't want to make a pre-compiled dll) – sylverfyre Jun 12 '12 at 16:35

2 Answers2

2

I assume that you are using WebSite and not WebApplication.

While adding new Class file in the Website, It should be in the App_Code Folder to avoid the Accessibility issues.

Edit - 1 => Please see below the Publish details.

enter image description here

Edit = 2 => Try adding the individual Assembly in the bin folder as stated below. This will confirm that the App_Code folder Dll are incorporated.

enter image description here

Pankaj
  • 9,749
  • 32
  • 139
  • 283
  • Yeah. It wasn't until I specifically started searching with App_Code that I found a proper solution. MSDN link explaining the folder structure: http://msdn.microsoft.com/en-us/library/t990ks23.aspx – sylverfyre Jun 12 '12 at 16:44
  • Okay. It worked through VS but not when I try to use it in IIS. What's going on? – sylverfyre Jun 12 '12 at 18:12
  • Rebuild the complete solution. Before publishing, select the Delete all files before Publish option and check again. – Pankaj Jun 12 '12 at 18:14
  • Nope. I don't see the option you describe, but even when I delete the old version manually, I still am seeing the same IIS error. Is it a setting in IIS I need to change? The /bin/ folder has the App_Code.dll and App_Code.compiled files in it. Shouldn't that make the Helper namespace accessible? – sylverfyre Jun 12 '12 at 18:20
  • Where is this Publish Web dialog found? I'm doing Build > Publish Web Site and it looks nothing like that. Either way, I published it to a clean output folder and it hasn't fixed the error. – sylverfyre Jun 12 '12 at 18:41
  • It's the same error as in the update. Compiler error CS0246: Type or namespace 'Helper' could not be found. `Using Helper;` is the line giving the error. – sylverfyre Jun 12 '12 at 18:52
  • Seems like the physical path is pointing toward another folder path. – Pankaj Jun 12 '12 at 18:54
  • What do you mean? I changed \info.ashx just to obfuscate the path. It's c:\Users\sylverfyre\livetest\serverside\info.ashx giving the error. The compiled App_Code.dll and App_Code.compiled are in c:\Users\sylverfyre\livetest\serverside\bin\ where I expect them to be. – sylverfyre Jun 12 '12 at 18:57
0

For IIS to read classes correctly from the App_Code folder, the App_Code folder (or the bin folder, for compiled assemblies) needs to be in the IIS root directory in order for classes within to be detected.

In my case, I was publishing to a subfolder of the IIS root directory, and so IIS couldn't see the classes in it.

sylverfyre
  • 3,049
  • 3
  • 17
  • 15
  • So, you were keeping the folder as Sub-Folder of an existing folder? – Pankaj Jun 18 '12 at 16:50
  • Pretty much. The website was in [iis root] /app/app_name/ and so visual studio put the App_Code folder in /app/app_name/App_Code - this resulted in the App_Code working fine within visual studio's iis emulator (since it puts site root at the project root), but when I told it to export to IIS it wouldn't work because App_Code was in the wrong place. It works correctly with App_Code folder manually moved to the IIS Root folder. the Bin folder has the same caveat - /bin/ MUST be in the IIS root directory to function. – sylverfyre Jun 27 '12 at 13:45