Tuesday, May 5, 2015

Controlling console output from a windows task

Windows tasks continue to be very handy especially for automating batch operations that do not require constant human attention. But even such operations may require occasional human interference to troubleshoot problems.

Most of the Windows tasks execute command line programs that do not have graphical user interface but produce output in form of log files or console messages. While monitoring of log files can be relatively easy automated via different kinds of log collection services, the console output that is mostly meant for human eyes and could provide helpful information is usually lost as by default Windows task does not save console output anywhere.

There is however a relatively easy way to save console output produced by a Windows task in a text file. Windows command line environment provides with a function to redirect standard program output to stdout or stderr to a file using redirection commands '>' or '>>'. The first one redirects that output to a file and replaces the file if it exists with a new version and the second one appends the output to a file if it exists.

In order to use output redirection a Windows task action should be configured to execute a CMD shell instead of an actual cl program. So instead of

>myprogram.exe > output.txt

it should be configured as

>CMD /C "myprogram.exe" > output.txt

In the first example of direct calling a program Windows task will ignore output redirection and the entire output will not be saved. In the second example the redirection will be executed not by a Windows task but by a CMD shell instead and the output will be saved in a file.




Friday, September 19, 2014

404 Error in IIS for a Url with a plus + in the Path

Default Internet Information Server (IIS) behavior since IIS 7 rejects requests to Urls with a plus (+) sing in the path like:

www.somesite.com/one+two or
www.somesite.com/path/subpath/three+four

The behavior is considered a security feature and controlled by a setting called "Double Escaping Filtering". To override the default rejecting behavior the setting needs to be changed either via a web.config configuration:

or an IIS Management user interface:

Thursday, August 28, 2014

External Social Login/Authentication with Microsoft ASP.NET Identity Does Not Work

Well, it's painful as hell, when you spend many days developing custom implementation of ASP.NET Identity with a main goal to enable social/external authentication (as in Facebook, Google, Twitter, etc.) and at the end everything works but the social authentication itself.

The main reason of it not working is that inside ExternalLogin (or similarly named) action method of an Account controller the external login info object is always null:

Google search returns a lot of discussions on Stack Overflow and it takes a lot of time to go through them but eventually you'll find out (and will be very disappointed) that if you did everything right with your Identity implementation and did not make any stupid mishaps than it's totally not your fault and there is almost nothing in answers on Stack Overflow for you to figure out the problem.

Apparently it's an OWIN bug that is still not fixed (I tested with both Identity 2.0.x and 2.1 and it's still there and happening) as it was figured by smart people (https://stackoverflow.com/questions/20737578/asp-net-sessionid-owin-cookies-do-not-send-to-browser) and without going into obscure details there is a simple fix for it.

In a Login (or named similarly) action method (GET version) of your Account controller add the very first line of code as below:

The most disappointing part of this problem is that it will not necessarily surface while you are testing. First everything might work just fine and than suddenly out of thin air without any obvious reason it will stop working completely. If it's the case (you had it working and than it stopped) than this is your solution: just add that dummy code line as above and it will do the magic.

Let's see how long it will take Microsoft to fix this bug. Happy coding and thanks all the smart people for sharing their findings.

Thursday, July 24, 2014

Upgrade TeamCity to enable support for Visual Studio 2013

Symptoms


If you are using TeamCity in your development process and had upgraded from Visual Studio 2010 to VS 2013 in your development environment you may experience unexpected build errors on TeamCity if its version is lower than 8.1.x similar to the following:

error MSB4019: The imported project "C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v11.0\WebApplications\Microsoft.WebApplication.targets" was not found. Confirm that the path in the declaration is correct, and that the file exists on disk.

The Story


The situation is that developers' work stations had been upgraded to Windows 8.x along with Visual Studio 2013 but a TeamCity build server still happily runs under Windows 2008 with .NET 4 installed and everything is just fine. Most likely the projects TeamCity builds target .NET 4.0 framework because they don't utilize newer features from .NET 4.5.x.

Next the decision is to switch your projects target framework to .NET 4.5 in order to take advantage of some new features, for example MVC 5, or ASP.NET Identity framework, etc. The projects have been successfully modified on developers' workstations and compiles and runs there but suddenly TeamCity is not happy because it cannot figure out how to properly build the project with the existing set of MSBuild tools. Apparently a build server software upgrade is required.

The Problem


In the error message above the key problem is that MSBuild is trying to import build targets from the wrong location "C:\...\MSBuild\...\v11.0\...". The exact problem is "v11.0" part. It comes from an environment variable that TeamCity sets when invokes MSBuild. The TeamCity sets it based on MSBuild tool version selected in its project definition. So technically it should be just enough to modify the MSBuild version to 2013 to fix the problem but TeamCity before 8.1.0 simply does not allow any other option to select from. Upgrade is required.

The Solution: Proper Build Server Set-up


Ideally a build server should resemble a production environment and should run the same versions of the build tools as developers have so obvious choice here is to install Windows 2012 with .NET 4.5 already on it and add Visual Studio 2013 build tools for compilation support. Then move TeamCity from the old build server to the new one and everything should be just fine. But there is a catch.

On the old build server TeamCity build steps have been configured to run MS Build v4.0 on .NET 4.0 Framework and MS Build would not be able to find required build targets because they are now in a different location corresponding to Visual Studio 2013. Installing Visual Studio 2013 in addition to the MS Build Tools 2013 would not help resolve the problem (and it's absolutely not required) since the problem is not with the .NET environment at all: the problem is that TeamCity does not know about Build Tools version 12.0.

To fix the problem TeamCity must be upgraded to the latest version but not below 8.1 because support for Visual Studio 2013 was added in version 8.1.0 and then all the build steps in all the projects should be modified to use a proper MSBuild version accordingly. Screen shots below illustrate the build step settings before and after the change:


After the change:



Thursday, July 3, 2014

PDF4NET: Setting Transparent Background for a Barcode

PDF4NET is my favourite PDF manipulation library for .NET. It is very versatile and has many useful and unique features but unfortunately lacks good documentation. So often in order to achieve something it requires some digging and researching to do.

Here is a small trick that allows setting a transparent background for a bar code inside a PDF document.

PDF4NET renders bar codes inside PDF as images. Because of that there is a background colour of a bar code area. By default it's a White colour. If a bar code are is not overlapping with any other coloured content in a PDF document it does not matter. But if a bar code is drawn on top of another coloured area it will look like a white rectangle with a bar code inside it which may not be your exact intention.

PDFBarCode class has a BackColor property of PDFColor type that allows setting a background colour for a bar code area but there is no way to define neither RGB or CMYK colour that is transparent because neither colour model support transparency in PDF. But assigning explicitly BackColor a null value will remove any colour from the background and make a bar code area transparent so only the bar code data will be visible.