gzak
gzak

Reputation: 4110

ashx handlers use older C# compiler

I've got an old solution which had been upgraded to C# 6 a while ago and we've been using it throughout much of the code. It has a wide variety of project types due to its age, everything from old-school asp.net all the way up to web api 2. However, for some reason ashx files in the traditional asp.net projects still target C# 5 and so we can't use some the newer language features there (note that the C# code is defined inline in the ashx file, there's no separate ashx.cs file). How do I upgrade the compiler for ashx files? Follow up question - how do I just make it inherit the compiler version used by the rest of the solution?

Upvotes: 0

Views: 1235

Answers (1)

John Wu
John Wu

Reputation: 52260

Firstly, know that code contained in markup (like .aspx or .ashx) is not compiled the same way as codebehind. Your codebehind is compiled into a DLL which gets deployed onto your bin folder. Meanwhile, unless you take the trouble to precompile the .ashx and .aspx files, they are compiled on a just-in-time basis. You can actually modify the markup directly on the web server and the changes will trigger a recompilation.

Secondly, bear in mind that .ashx compilation has changed dramatically. It used to be that its compilation was controlled by the <BuildProviders> element which would typically be configured with System.Web.Compilation.WebHandlerCompilationProvider. However, somewhere between 2.0 and 4.5, that was deprecated in favor of a different sort of configuration using <Compilers> element. The latter can be configured to specify lanaguge version, e.g.

<system.codedom>
 <compilers>
   <!-- zero or more compiler elements -->
   <compiler 
      language="c#;cs;csharp" 
      extension=".cs"
      type="Microsoft.CSharp.CSharpCodeProvider, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
      compilerOptions=""  
      warningLevel="1" />
 </compilers>
</system.codedom>

I imagine that your (very old) code base is not configured properly; it may be that it using the old style of configuration, or the version identifier is wrong.

My suggestion is to move the .ashx markup to a proper .cs file, and compile it along with all the other code during the build. That will avoid this problem completely. Otherwise you will need to pore over your machine's configs (these configs aren't necessarily in your web.config) and get them all aligned.

Upvotes: 4

Related Questions