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:

Thursday, 18 May 2017

Visual Studio 2017 freezes / crashes when I open the toolbox

I recently installed VS 2017 on a new Win 10 machine, and I had a problem when trying to open the toolbox window - the whole IDE would just freeze and never recover.

This is a problem with the Xamarin extension that is yet to be fixed, apparently it is trying to warn us something about the Android SDK (that it might not be installed?), but instead it brings the whole thing to a halt. The quick fix is to disable the Xamarin extension - but I guess installing the Android SDK may fix it too.

Friday, 10 March 2017

Visual Studio 2017 install stuck on Applying Microsoft.VisualStudio.Debugger.JustInTime

This happened to me and I tried lots of things to overcome the problem - eventually I found I had to this:

  • Switch off AVG antivirus protection
  • Switch off  'Real-time protection' on your Windows Defender settings
  • Restart
After restart my install started where it left off and didn't get stuck again!

Tuesday, 28 February 2017

Winforms - showing touch keyboard on Win 10

If you're using a textbox in a winforms app on Windows 10 in tablet mode the touch keyboard only seems to show if your textbox is a password box. Weird huh? There's a few proposed solutions for this to do with tabtip.exe or osk.exe but neither of these worked for me. The most effective solution was to:

Insert a reference to UIAutomation.dll
    In the form-load-handler of the application's main window, insert the following code:
      var asForm = System.Windows.Automation.AutomationElement.FromHandle(this.Handle);

      Friday, 13 January 2017

      cIgnore SSL HTTPS errors GetAsync Httplient .Net winforms

      I'm working on a Windows app at the moment and part of the job involves comms with a Rest api over https. During the development phase the SSL certs for the server aren't valid, and this causes GetAsync for the HttpClient to return a Taskstatus.Faulted, and the whole thing breaks. To overcome this you just need this line of code:

      ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;