Commandline Equivalent for DLL

Sep 9, 2011 at 1:14 AM


I was wondering what the command would be using the dll referenced in a project for the following commandline:

"C:\Program Files (x86)\Microsoft\Microsoft Ajax Minifier\ajaxmin.exe" -JS -clobber ../Scripts/jquery-1.6.2.min.js ../Scripts/jquery-ui-1.8.16.min.js -o ../Scripts/Production/mvpApp.all.min.js


Sep 9, 2011 at 1:47 AM

Well, the first thing to consider is that the DLL does absolutely no file access. It was implemented that way on purpose so we can quickly, easily, and (above all) safely apply the AllowPartiallyTrustedCallers security attribute to the assembly. That attribute is needed if you have, for instance, an ASP.NET application and you want to minify some JavaScript or CSS. Microsoft has some pretty strict requirements on publishing assemblies with that security attribute, mostly involving lots of architecture, security, and code reviews with resident security experts and such. All the DLL does is work on in-memory strings.

So, if you have an application and you want to completely duplicate the command line above, your application will first need to read the two input files into a string, pass that string to the DLL using just the default code settings, then write the returned minified string to the output file. Just to be clear, if you are using the DLL, your application is completely responsible for reading and writing to the file system.

Reading the input files is pretty straight-forward -- just use standard C#  System.IO classes like StreamReader and StringBuilder. Writing JavaScript and CSS files may be a little more tricky, depending on the output encoding you want and whether or not your JavaScript/CSS contains any special characters that might need escaping. If your code has no special characters, or if you want to write your file to UTF-8 or UTF-16, then you don't need to do anything special and can just use standard C# output classes like StreamWriter. But if you have special characters and/or you want to write your output in ASCII or some other encoding format, the DLL provides a couple C# "encoder fallback" classes that can be used to safely write JavaScript or CSS in any encoding: JSEncoderFallback for JavaScript or CssEncoderFallback for CSS.

I'll write a larger article on how to use the fallback encoders soon, but the simple explanation is that you will need to create the appropriate Encoding object (System.Text namespace) with one of the two encoder fallbacks, and use that encoding when you write your file. For instance, let's say you want to output this in the ASCII encoding. Create the ASCII encoding object like this:

var encoding = (Encoding)Encoding.ASCII.Clone();
encoding.EncoderFallback =
new JSEncoderFallback();

And when you create your output stream, specify this encoding object in the constructor. For example, given the output path in the outputPath variable, and the minified code returned by AjaxMin in the minifiedCode variable:

using(var outputStream = new StreamWriter(outputPath, false, encoding))

That should always give you the proper character escaping (if needed) in your JavaScript. Exactly the same process for CSS, except you'd use the CssEncoderFallback class.

Hope that helps!