X509Certificate object c# performance and memory issues alternative – fixed

February 17th, 2016 2 comments

The Problem

For my PatchCleaner product I needed the ability to read the digital certificate off a file, like can be seen on the following tab on the file properties window


I want to read the digital certificate and obtain the contents of the “Subject” Field that is a string formated similarly to an Active Directory OU path.

CN=Microsoft Corporation, OU=MOPR, O=Microsoft Corporation, L=Redmond, S=Washington, C=US

The Issue

I originally used the X509Certificate and X509ertificate2 objects like so:

 var cert1 = new X509Certificate(_file);
 var cert2 = X509Certificate2.CreateFromSignedFile(_file);

Both of these worked but I noticed that when accessing the certificate on a large file (200+ Mb):

  1. It was very slow
  2. It seemed to use alot of memory

In fact on closer investigation it appears the X509Certificate reads the entire object into memory! I had a file of 800Mb that I opened with X509Certificate2.CreateFromSignedFile(_file), and the memory usage on my application increased by the same 800Mb before releasing once I had read the value!! Not cool!!

The Solution


This image shows a test application that attempts to read a 294mb patch file in 3 different ways, with a 1 sec sleep between calls:

  1. T1 – read the digital certificate by the X509 Certificate
  2. T2 – read the certificate via WinCrypt all content
  3. T3 – my soltuion via WinCrypt with just PK7 content

You can clearly see two large memory spikes for test 1 and 2, but test 3 doesn’t even register.

Below is a full c# code sample.

I based my solution on the following stack overflow article

Get timestamp from Authenticode Signed files in .NET


The only extra class you need to import is:

  • System.Secuirty


This is a copy paste from the stack overflow article:


This is my static class that implements two method calls.

  1. GetDigitalCertificateSlow() -This is the version behind T2 above
  2. GetDigitalCertificate() – this is the one you want

These methods were based on the IsTimestamp code in the StackOverflow article, but I have manage to tweak slightly to get superior performance.

Categories: Uncategorized Tags:

Running WCF Service as both HTTP and HTTPS

February 16th, 2016 No comments

When trying to configure a WCF service to run as both a HTTP and HTTPS endpoint there are some differences to consider:

  1. HTTP uses SOAP 1.1
  2. HTTPS uses SOAP 1.2

This can cause issues with how your XML packets need to be generated with different SOAP Headers.

SSL Config

On the IIS Manager under the website there is a tile titled “SSL Settings”.
To run both HTTP and HTTPS ensure “Require SSL” is unticked

Web Config

If you want to allow people to generate a .wsdl file do not forget to set this in the web config in the service behaviours

<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />

Categories: Uncategorized Tags:

citrix receiver disable add account popup on startup

February 16th, 2016 3 comments

The problem

I recently installed CitrixReceiver 4.4 for windows (on 2 windows 10 pc’s) and now on start up I am always being asked to “Add Account”

The Solution

The solution that worked for me was from the Citrix website

How to Suppress the Add Account Window in Citrix Receiver for Windows

After reading this I found the solution that worked on my x64 bit windows 10, (so I assume will work on 32 bit) was to run this registry entry

  1. Create a text file
  2. Copy/Past the text into it
  3. Save
  4. change the file extension to .reg
  5. Double click and follow the prompts
Windows Registry Editor Version 5.00


The next time you start, no more Add Account window.

Categories: Uncategorized Tags:

SQL Server Script user with SID

September 18th, 2015 No comments

The Problem

When you move sql logins around servers the logins are created with an SID that uniquely identifies the login.

Assume you setup two logins manually, one on your production server and one on your dev server.

When you restore your prod database to development your Login will be “disconnected” from the restored database and the SID of the “User” in the restored production database does not match the SID of the Login on your dev box.

The Solution

Create your login specifying the SID.
The easiest way is to let SQL set and SID and then read it off the DB and use it in your new login creation script
This script should do it

Use master

-- Step 1 - Create Login
IF  EXISTS (SELECT * FROM sys.server_principals WHERE name = N'usr_website')
DROP LOGIN [usr_website]

CREATE LOGIN [usr_website] WITH PASSWORD = 'password:-)'

-- Step 2 - Get SID
Declare @name nvarchar(254)

set @name = 'usr_website'

FROM [msdb].[sys].[syslogins]
where name = @name or @name is null

-- Step 3 - Create Login with SID
IF  EXISTS (SELECT * FROM sys.server_principals WHERE name = N'usr_website')
DROP LOGIN [usr_website]

CREATE LOGIN [usr_website] WITH PASSWORD = 'password:-)', SID = 0x346837D1EC9C484C91A3B46D7EC7EABF

Categories: SQL Server, T-SQL Tags:

T-SQL Drop temp table if it exists

September 9th, 2015 No comments

The Problem

I often work in SQL Server with temp tables and they can be a pain when developing as you may have code like

Create Table #tblTemp( Id int , Value nvarchar(32))

Insert Into #tblTemp
select Id, Value from SomeOtherTableAndLotsOfJoins

--Drop Table #tblTemp

You comment out the drop as you want to check the results and do some testing but next time you want to run the script you have to remember to call the “Drop Table”

The Solution

Put this at the top of your script

if exists(SELECT * FROM tempdb.dbo.sysobjects WHERE ID = OBJECT_ID(N'tempdb..#tblTemp'))
	drop table #tblTemp;

Categories: T-SQL Tags:

LANTorrent seed you torrents from NAS without corruption

June 18th, 2015 No comments

The Scenario

I have been a long time user of uTorrent. I run it on my laptop that has limited storage, so if you want to keep seeding you have to relocate your downloaded files to an external drive. I use a wifi connected NAS type storage.

The problem

If uTorrent starts whilst you are offline, your entire seeding collection will be corrupted and it can take many hours, even days to recover.

The solution




LANTorrent is a wrapper for you existing torrent application that adds a layer of protection to stop you corrupting your torrent setup, and recovering when it does.


Automatic startup 

You can set it so LANTorrent will start your preferred Torrent application when it starts. Combine that with setting LANTorrent to starts when windows starts, you can now have LANTorrent control the starting of your app.

Startup Protection

If you start your torrent application when off the network it will corrupt your configuration.

If you have LANTorrent set to start your torrent app, LANTorrent will look for your network drive. If the network drive is not accessible it assumes that you are disconnected and will not start your torrent application. This will stop you from accidentally destroying your seeding library.


LANTorrent can backup your torrent configuration, in the following ways:

  1. On startup
  2. On Close
  3. At predefined intervals

You can choose how many backups you wish to retain.


If you happen to corrupt your configuration, just choose a backup and click restore and you are back up and running.


LANTorrent monitors the torrent application and will close itself (and perform an on close backup if set), when it detects that the torrent application has closed.


Runs on Windows and requires .Net 4.5

Supported Torrent Clients

  1. uTorrent
  2. Vuze
  3. BitTorrent
  4. BitLord
  5. Deluge
  6. qBittorent
  7. tixati


Categories: Free Software Tags:

Clean the Windows Installer Folder

June 1st, 2015 3 comments


The c:\Windows\Installer folder contains windows .msi installers and .msp patch files that are used when installing and updating software on your windows computer.

Over time as more and more programs are patched the size of this directory gets very large and some of the files are actually orphaned and no longer required on your system.

The problem is you cannot just delete the entire directory as these files are used when upgrading or uninstalling your software. What you need to do is only delete those files that are not in use any more


Patch Cleaner

This is a simple piece of software that will clean your windows installer folder in one easy click.



If you are unsure of the program just use the move function to move the detected orphaned patches off onto an external drive. If the program has done something wrong you can just copy them back.

This software is 100% free. >> Download Here <<

Categories: Free Software Tags:

How to make a self extracting archive that runs your setup.exe with 7zip -sfx switch

May 14th, 2015 9 comments

The scenario

When you use the default Microsoft Visual Studio Installer (yes, it is back as an extension for VS 2013 – Visual studio Installer Projects Extension), the output are two files:

  1. installer.msi – the .msi installation file
  2. setup.exe – which is the bootstrap that does your pre-requisite checks

So you want to provide a download for your product from your website as a single file.

Some possible options include:

  1. Zip the two files – the user has to download a zip, extract the zip and run the setup.exe
  2. Just provide the msi file, but this by passes the pre-requisite checks which is not ideal; or
  3. Create a self extracting archive that runs, extracts the software and then calls the setup.exe

The self extracting archive option seems the best.

The problem

I wanted to use 7Zip to create the self extracting archive but I could not seem to find a simple guide that explained exactly how to do it.

I have finally worked it out and below are some detailed instructions.

How to make a self extracting archive that launches your installer after extraction

It is actually quite simple to do if you follow these easy steps.

Step 1 – Setup your installation folder

To make this easy create a folder c:\Install. This is where we will copy all the required files.

Step 2 – 7Zip your installers

  1. Go to the folder that has your msi and your setup.exe
  2. Select both the .msi and the setup.exe
  3. Right-Click and choose 7Zip –> “Add to Archive”
  4. Name your archive “Installer.7z” (or a name of your choice)
  5. Click Ok
  6. You should now have “Installer.7z”.
  7. Copy this .7z file to your c:\Install directory

Step 3 – Get the 7z-Extra sfx extension module

You need to download the 7z-Extra.

  1. Follow this link to go to 7zip download.
  2. You need to download the 9.20 version (as @ May-2015) as the beta does not contain the correct files.
  3. A direct download link.
  4. Extract the 7zip extra files
  5. Copy the file “7zS.sfx” to c:\Install

Step 4 – Setup your config.txt

I would recommend using NotePad++ to edit this text file as you will need to encode in UTF-8, the following instructions are using notepad++.

  1. Using windows explorer go to c:\Install
  2. right-click and choose “New Text File” and name it config.txt
  3. right-click and choose “Edit with NotePad++
  4. Click the “Encoding Menu” and choose “Encode in UTF-8”
  5. Enter something like this:
    Title="SOFTWARE v1.0.0.0"
    BeginPrompt="Do you want to install SOFTWARE v1.0.0.0?"

Edit this replacing [SOFTWARE v1.0.0.0] with your product name.
Notes on the parameters and options for the setup file are here


You should now have a folder “c:\Install” with the following 3 files:

  1. Installer.7z
  2. 7zS.sfx
  3. config.txt

Step 5 – Create the archive

These instructions I found on the web but nowhere did it explain any of the 4 steps above.

  1. Open a cmd window, Window + R –> cmd –> press enter
  2. In the command window type the following
    cd \
    cd Install
    copy /b 7zS.sfx + config.txt + Installer.7z MyInstaller.exe
  3. Look in c:\Install and you will now see you have a MyInstaller.exe
  4. You are finished

    Run the installer

    Double click on MyInstaller.exe and it will prompt with your message. Click OK and the setup.exe will run.

    Everything is easy… once you know how. 🙂

    P.S. Note on Automation

    Now that you have this working in your c:\Install directory I would create an “Install.bat” file and put the copy script in it.

    copy /b 7zS.sfx + config.txt + Installer.7z MyInstaller.exe

    Now you can just edit and run the Install.bat every time you need to rebuild a new version of you deployment package.

    P.P.S. super user

    If you got here from the super user article, perhaps you would like to vote my answer up 🙂

Categories: Scripts Tags:

Terrible Performance of SQL Server Linked Server query over WAN

April 14th, 2015 2 comments

The problem

I have a linked server connection between 2 SQL servers approx half the world away. One server resides in Sydney, Australia and the other in London, UK.

I was getting terrible performance.

To simulate this for testing purposes, on my home internet connection, I created an Azure database in Northern Europe and my local SQL server is in Sydney.

I used the DimDate table as created in this code project example. http://www.codeproject.com/Articles/647950/Create-and-Populate-Date-Dimension-for-Data-Wareho
(This is a 36 column table)

I ran this against my azure database and populated it with 45,656 records, (days between 1-Jan-1974 to 31-Dec-2099, just adjust the populate script)

On my local sql server, I have a Linked Server connection called LSEUROPE set to use SQLNCLI – SQL Native Client, configured to connecting to the Azure database in Europe.

On the local sql server:

1. Download and Install Wireshark on the local server, https://www.wireshark.org/download.html
2. Download and Install Compass wildpackets free from, http://download.cnet.com/Compass-Free/3000-2085_4-75447541.html. This is used to visualise the throughput.

Wireshark was enabled and set to monitor only the remote server, (capture filter – “host” where is the IP address of the remote server).

2 tests were executed:

Test 1 – SSMS Direct

1. Open SSMS and connect directly to the REMOTE server.
2. Start Wireshark Capture
3. execute “select * from dbo.DimDate”
4. once finished stop wireshark
5. Save the wireshark session as a .pcap file, (compass only opens .pcap files)

Test 2 – Linked Server

1. Open SSMS and connect to the LOCAL server.
2. Start Wireshark Capture
3. execute “select * from LSEUROPE.TestDB.dbo.DimDate”, (Note: OpenQuery in this instance makes no difference as all we are testing is the throughput of transferring data from the other side of the world.)
4. once finished stop wireshark
5. Save the wireshark session as a .pcap file.

Fig 1. SSMS Direct Test – 35 sec

Fig 2. Linked Server Test – 6m 29 sec

As you can see, throughput on the Linked server query is limited to 0.22 Mbits/s, whilst the remote SSMS query happily averages around 3Mbits/s (again, this is just on my standard ADSL2+ home internet connection, but I have replicated this exact same pattern at work on a 20MBps dedicated WAN link).

There is some bottle neck that is slowing performance of the data being sent via a linked server connection!!

A solution of sorts

After much playing and testing of the various Linked server connections I have managed to get the following results over a linked server connection.

Fig 3

Fig 4

Figure 4 was obtained by creating a ODBC System DSN of type ODBC Driver 11 for SQL Server. This provided the fastest performance, bringing down the same 45,656 rows in 27 seconds.

Figure 3 was obtained by creating the ODBC System DSN using the SQL Native Client. It is now approx 46 seconds which is much better than 6 minutes, but still not the 27 seconds of the ODBC driver.

After multiple runs I was getting similar performance regardless of the Client used in the DSN


In both cases Fig 3 and Fig 4, the linked server was created using the “Microsoft OLE DB Provider for ODBC Drivers” and configured the Linked server to use the System DSN.


Now the real question, why does this seem to work?

I am going to test using this as I am sure there will be synax type issues of changing the linked server connection, (I think ODBC doesn’t handle TEXT/NTEXT fields)

Microsoft, what is going on here?

I am going to be trying to find out what the reason is for the poor performance of the “Microsoft OLEDB provider of SQL Server”, and if there is something that can be done to improve the performance.

Categories: SQL Server Tags:

MVC Razor View escape character

February 13th, 2015 No comments

The Problem

You want to use your model or some code to append a variable inline with a string.
Assume you want to create a URL with a dynamic element like:


where -dev will be an appsetting accessed in razor from @Settings.Default.URLSuffix

When you add the @Settings value in like so;


it treats the @Settings.Default.URLSuffix as a text string, outputting “http://appserver@Settings.Default.URLSuffix.domain.com” not “http://appserver-dev.domain.com” as you were hoping. What is the escape character?

The Solution

@@ is the escape sequence.. so that should be easy..


The problem now is it treats the .domain.com as part of the code and is not happy because a string object does not have .domain.com methods.


Add a semi-colon to end the code and that is the problem solved.

Categories: MVC, Uncategorized Tags: