Tuesday 20 September 2016

Could not establish trust relationship for the SSL/TLS secure channel with authority XXXX

Just putting this here as it's come up and I couldn't remember the steps.

If you're spinning up a WCF service for testing in Visual Studio and you get the message "Could not establish trust relationship for the SSL/TLS secure channel with authority 'localhost:XXXXX'" then there is a simple 2 minute fix.

Take the URL of the svc file you are trying to access (e.g. https://localhost:XXXXX/MyService.svc) and past it into IE.

IE will prompt you saying the certificate is invalid. Accept this to carry on.

When your auto-generated service page comes up the address bar will be red. Click on the right end of the address bar (depending on the version of IE it may show a little red shield or say "Certificate Error". Click on View Certificates, then Install Certificate when it shows the details.

When to install into your User certificate store. You'll then be asked if you want to install it into the default location or somewhere else. Select the other location option and select "Trusted Root CA Authorities"

Job Done

Thursday 27 March 2014

Free Training For Life

Free Training For Life - OK, I know I'm being selfish here, but would really appreciate any upvotes to get me this (worth a shot) :D

Wednesday 13 June 2012

Please support the campaign to save lennox

While I know my readership isn't high I wanted to share the following in the hope that if even one of you signs the petition then we can help push up the numbers to the point where we cannot be ignored.

If you care about animals and are as gobsmacked as me by the groundless adherance to the letter of the unjust Breed Specific Legislation in Belfast then please sign the petition to save lennox - condemned to be destroyed for basically looking like a dog that wouldn't necessarily be dangerous anyway - http://www.petitiononline.com/sl190510/petition.html

http://www.savelennox.co.uk/ for more - please spread the word and try and get this ruling overturned

Tuesday 12 June 2012

Microsoft conferences online

As this has come up a few times lately I just wanted to post some links up to the online archives for the various Microsoft conferences. I have to say the fact that you can get at these archives permanently, as well as when the conferences are still running is just one of the reasons I love MS's attitude to developer relationships.

The conferences do have different focuses, but these days you will normally find something on most subjects at any of the conferences.

Teched (which is running now, at the time I write this post) - this one is normally quite heavy on IT, but with some interesting dev pieces in there
Build - this one is for the devs, though quite a bit on design/UI as well. This first ran in 2011 to introduce us to Windows 8 / Visual Studio 2012 - http://channel9.msdn.com/events/BUILD/BUILD2011

The Professional Developer Conference (PDC) - the key's in the name - for devs mainly, was the forerunner to Build - Archives from 1991 to 2010 are at http://channel9.msdn.com/Events/PDC

MIX - mainly for designers tho with some dev in there aswell - archives from 2006 to 2011 are at http://channel9.msdn.com/Events/MIX

These are all worth a look through - there are 1000s of hours of the best insight to working with the MS platforms you could possibly hope for.


Sunday 26 February 2012

Accessing Printer Status using WinSpool in C#


After help from ambience I’ve managed to get the code working to access accurate printer status by using DllImport to access Winspool.drv (as opposed to using WMI, which had very mixed results).

There are 3 key functions you need to be aware of:

  • OpenPrinter
  • GetPrinter
  • ClosePrinter

The GetPrinter function is the one that does the real work we are interested in – accessing the PrinterInfo2 structure that gives you all the information you could possibly want on your named printer – driver, status, configuration, etc

Once you have the PrinterInfo2 you can than use bitwise operators along with known constants to find out the current status of the printer. One thing to note is that to detect whether a printer is marked as offline rather than physically offline you need to check the Attributes rather than Status – I’ll put a followup post shortly with details of a Helper class round the PrinterInfo2 status.

Here’s the code to help you out if, like me you need to get accurate printer info in C#:

//Code written by Mark Middlemist - @delradie 
//Made available at http://delradiesdev.blogspot.com
//Interop details from http://pinvoke.net/
using System;
using System.Runtime.InteropServices;

namespace DelradiesDev.PrinterStatus
  public class WinSpoolPrinterInfo
    [DllImport("winspool.drv", CharSet = CharSet.Auto, SetLastError = true)]
    public static extern int OpenPrinter(string pPrinterName, out IntPtr phPrinter, ref PRINTER_DEFAULTS pDefault);
    [DllImport("winspool.drv", SetLastError = true, CharSet = CharSet.Auto)]
    public static extern bool GetPrinter(IntPtr hPrinter, Int32 dwLevel, IntPtr pPrinter, Int32 dwBuf, out Int32 dwNeeded);
    [DllImport("winspool.drv", SetLastError = true)]
    public static extern int ClosePrinter(IntPtr hPrinter);
    public struct PRINTER_DEFAULTS
      public IntPtr pDatatype;
      public IntPtr pDevMode;
      public int DesiredAccess;
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
    public struct PRINTER_INFO_2
      public string pServerName;      
      public string pPrinterName;
      public string pShareName;
      public string pPortName;
      public string pDriverName;
      public string pComment;
      public string pLocation;
      public IntPtr pDevMode;
      public string pSepFile;
      public string pPrintProcessor;
      public string pDatatype;
      public string pParameters;
      public IntPtr pSecurityDescriptor;
      public uint Attributes;
      public uint Priority;
      public uint DefaultPriority;
      public uint StartTime;
      public uint UntilTime;
      public uint Status;
      public uint cJobs;
      public uint AveragePPM;
    public PRINTER_INFO_2? GetPrinterInfo(String printerName)
      IntPtr pHandle;      
      PRINTER_DEFAULTS defaults = new PRINTER_DEFAULTS();      
      PRINTER_INFO_2? Info2 = null;
      OpenPrinter(printerName, out pHandle, ref defaults);
      Int32 cbNeeded = 0;
      bool bRet = GetPrinter(pHandle, 2, IntPtr.Zero, 0, out cbNeeded);
      if (cbNeeded > 0)
        IntPtr pAddr = Marshal.AllocHGlobal((int)cbNeeded);
        bRet = GetPrinter(pHandle, 2, pAddr, cbNeeded, out cbNeeded);
        if (bRet)        
          Info2 = (PRINTER_INFO_2)Marshal.PtrToStructure(pAddr, typeof(PRINTER_INFO_2));
      return Info2;

Thursday 23 February 2012

Rebinding bug in WinForms ListBox when SelectionMode = None

Just a quick post to help out anyone who hits the same bug I have.

It appears that there are a couple of issues with WinForms ListBoxes when the SelectionMode is None (certainly in .net 2).

One that is pretty well documented is that if your SelectionMode is None then you can get problems on closing the form (https://connect.microsoft.com/VisualStudio/feedback/details/189700/listbox-selectionmode-none-bug)

One I've just hit on is that if you rebind/refresh a listbox's contents by changing the datasource then the list is cleared and left blank

The workaround is very simple (based on the one from Connect) - before manipulating the DataSource property on the ListBox change the SelectionMode to one of the other values, do your work, then change it back to None

Hope this helps

Wednesday 3 August 2011

Adding MVC 3 to an asp.net web site

OK, I've been off the blog for a while with things being very busy at work (some interesting changes coming up on public.presalesadvisor.com), but I'm back (for now at least).

I've been starting to tool up on MVC, and while I won't cover that right now as there's no shortage of coverage on that, I wanted to cover one bit that seems slightly lacking in coverage, which is specifically adding MVC 3 to ASP.Net Websites (not web projects).

The most excellent Mr Hanselman has already done the vast majority of the work for anyone wanting to add mvc to asp.net (and in fact pretty much all of it if you're using a web application) with his AddMvc3ToWebForms Nuget Package

There are however a couple more steps needed to get it working with a web site rather than web project

1) Install the nuget package as normal, either by selecting AddMvc3ToWebForms from the online gallery in Visual Studio's "Add Library Package Reference" option (Tools > Library Package Manager > Add Library Package Reference), or by typing the command Install-Package AddMvc3ToWebForms in the Package Manager Console (Tools > Library Package Manager > Package Manager Console)

When I ran this I got an error at the end but as far as I can tell, this isn't actually a problem, but caused by it not having a csproj file to work with:

 PM> Install-Package AddMvc3ToWebForms
'WebActivator (≥ 1.3)' not installed. Attempting to retrieve dependency from source...
Successfully installed 'WebActivator 1.3.2'.
Successfully installed 'AddMvc3ToWebForms 0.9.1'.
Successfully added 'WebActivator 1.3.2' to extranet.
Successfully added 'AddMvc3ToWebForms 0.9.1' to extranet.
Install-Package : Method invocation failed because [System.__ComObject] doesn't contain a method named 'Add'.
At line:1 char:16
+ Install-Package <<<<  AddMvc3ToWebForms
    + CategoryInfo          : NotSpecified: (:) [Install-Package], RuntimeException
    + FullyQualifiedErrorId : NuGetCmdletUnhandledException,NuGet.PowerShell.Commands.InstallPackageCommand

If you try and compile now it will fail, complaing of not being able to find MvcApplication1. This is because the view (cshtml) files it creates reference a standard namespace (MvcApplication1) that doesn't correspond to the ASP namespace in the created model/controller files. There is also the additional issue that in a web site the 'M&C' files should live under App_Code not in the rool directory.

2a) If you aren't interested in the initial files created by the package then you could simply create Models and Controllers directories under App_Code, delete the ones in the site root, and empty out the Views directory, then get on with your own coding

2b) If you do want to keep the Account/Home/Shared files that have been created then move the Models and Controllers files to App_Code

3) To resolve the namespace issue open up ChangePassword.cshtml, LogOn.cshtml, Register.cshtml in Views\Account and remove the 'MvcApplication1.' from the @model reference at the top

You should now be ready to roll - Enjoy