Catch exceptions in ASP.NET resulting from Generic Handler (ashx) file

浪子不回头ぞ 提交于 2019-11-29 12:50:27
MacGyver

If you have a folder called "App_Code", you can go through headaches and migranes. Or simply replace "App_Code" with some random name like "code" or "data" and put your classes in there to fix this. In my case, my project GUID was a Web Application and C# type.

Best Answer:

App_Code folder issues

Note:

I am running the site using IIS with a static port, not using Visual Studio 2012 with a dynamic port. I have specific server configuration on the web server, so I want the site to behave the same way our released site works. So that's why I run it like that.

Answer to my question:

This occurred because we had duplicate versions of the App_Data classes in the bin folder. The "Compile" version of the *.cs, and the website version of the *.cs. The “isProvider” error happens when more than one class with the same name exists. When we deploy the ASP.NET application, we deploy ONLY the compiled version of the *.cs files. The development environment, we keep them set to Properties > Build Action > None.

Not sure of an easy way around that. It might have to do with having things in the temporary and/or obj folder, but not sure exactly. Will take a little more troubleshooting to discover why. In the mean time, here is a list of things to resolve the issue. Not sure if some or all of these were necessary, but it's working now.

Steps to fix:

  • change all App_Data *.cs files to Properties > Build Action > Compile
  • Fix exceptions they throw to get that build working again - Clean Solution > Build Solution ... one exception was not caught with those set to Properties > Build Action > None, so that's why this was necessary. Essentially, Visual Studio 2012 does not throw an exception and says "Build Successful".
  • then change App_Data *.cs files back to Properties > Build Action > None
  • remove dlls out of \obj\Debug
  • remove dlls out of \bin
  • remove temporary ASP.NET files - C:\WINDOWS\Microsoft.NET\Framework\{.NET version}\Temporary ASP.NET Files\
  • Build Solution
  • restart IIS web site

Now things work great!

EDIT:

Actual problem was that I was referencing an external project in Visual Studio, but there was no dll file for it because the project type was Console Application, not Class Library. The real problem was that Visual Studio says it compiles the code fine (in App_Code) when you have a using statement to that other project, but says Built Successfully, even when the Build Action > Compile option is set for all *.cs files in App_Code. When you run the web application, it has to be set back to Build Action > None for it to work. However, since the *.cs files in App_Code didn't actually compile with the new code (if that's how ASP.NET in IIS treats these App_Code *.cs files), it throws the 500 Internal Server error because there's no good CIL code during runtime. To solve it, I just changed the project type to Class Library, and it still has no errors, but now it can reference the dll to the other project.

The isProvider error always occurs when there are duplicate classes found. So I'm not sure why that occurs, but that essentially goes away once you fix the part above.

You can use the Application_Error event in Global.asax file to catch that error.

protected void Application_Error(Object sender, EventArgs e)
{
    HttpContext ctx = HttpContext.Current;
    Exception exception = ctx.Server.GetLastError();
    int httpCode = ((HttpException)exception).GetHttpCode(); // check if it is 500
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!