Tuesday, 5 December 2017

Rebuild all indexes in your database


Wednesday, 4 October 2017

Global error handling in filter attribute MVC Web api

When I inherited the first skeleton iteration of the api I'm working on every action was populated with largely repeated code whose function was to capture and log errors. I centralized all this into an attribute filter class called GlobalExceptionFilterAttribute, inheriting from ExceptionFilterAttribute, that looks a bit like this:

And then applied it to all actions with this line in WebApiConfig.cs:
config.Filters.Add(new GlobalExceptionFilterAttribute());

Then, throughout the app, we can throw different types of error pretty much anywhere and this will ultimately be captured by the filter attribute. So, for instance most of my service layer methods look like this:

Tuesday, 15 August 2017

Capturing raw data in Web Api controller

I needed to capture raw POSTed data in a Web API controller endpoint. This is how it's done:
using (var contentStream = await this.Request.Content.ReadAsStreamAsync())
                contentStream.Seek(0, SeekOrigin.Begin);
                using (var sr = new StreamReader(contentStream))
                    string rawData = sr.ReadToEnd();

Monday, 14 August 2017

Sourcetree error "remote: empty password" after updating to new version

I updated my Sourcetree installation today and could no longer push to my Bitbucket repositories. Seems like I'm not the only one who gets the error

remote: empty password
A workaround until they fix this bug is this - copy the first line of the Sourcetree error message (which is the Git command that failed), open a new terminal window (Shift-Alt-T on Windows), and paste it in, hit return. This will run the Push command and ask you for your password.

UPDATE: Here's a permanent fix - delete the file named passwd located in AppData\Local\Atlassian\SourceTree

Thursday, 20 July 2017

Crystal report ExportToDisk as PDF loses barcode font

I've been working on a .Net MVC Web API, part of which takes some data and produces a PDF using ExportToDisk provided by CrystalDecisions.CrystalReports.Engine.dll.

This proved to be relatively easy, as did the inclusion of a barcode font in the report, along with some vb code in a forumula field, that cleverly takes a supplied number and renders it as a barcode.

A problem arose when exporting a report containing this barcode to PDF - the barcode was not visible in the PDF. This seems to be a common problem with what feels like a rather hacky fix - adding a registry key. This is how it's done:

Open up registry editor (regedit) and navigate to HKEY_CURRENT_USER\Software\SAP BusinessObjects\Crystal Reports for .NET Framework 4.0\Crystal Reports\Export. Add a new key called PDF, and a new value to it of Type 'DWORD' and name 'ForceLargerFonts' and value '1'. It should look like this when you've done:

Friday, 2 June 2017

Writing ProductVersion to a text file during installation

Today I was tasked with creating a text file during the running of an MSI installer, and writing the app's ProductVersion to it, so that our remote admin tools could read at a glance what versions our users were running.

First of all I needed to create an Installer Class in the app, and then override the Install method like so, capturing an input parameter and writing it to a text file:

 public override void Install(IDictionary savedState)

            string version = Context.Parameters["Version"];

            string path2 = "C:\\MyFile.txt";

                FileStream fs1 = new FileStream(path2, FileMode.OpenOrCreate, FileAccess.Write);
                StreamWriter writer1 = new StreamWriter(fs1);
                writer1.Write("Version:" + "\r\n" +
            catch (Exception)
               //do something here if you need to

Then, in the Setup project,  I added Custom Action. Right-click on the project, go to View > Custom Actions. Right-click on the install folder, and select Add Custom Action. Browse to the Application folder, select the primary output, and click 'Add Output'. Then, select the thing you've just added, and in the properties window, do the following: