Thursday, July 31, 2014

What do expressions look like underneath the language?

Section 1.4 of the language specification deals with expressions.  Everything from the dot for member access to lambda expressions.  Here are some little programs which implement some of the interesting ones so that we can see how they are expressed as IL.  First, let's see what x++ looks like as IL.  From:

        static void Main(string[] args)
        {
            int x = 0;
            x++;
        }

we get:

    .method private hidebysig static void Main (
            string[] args
        ) cil managed
    {
        .entrypoint
        .locals init (
            [0] int32 x
        )
        IL_0000: nop
        IL_0001: ldc.i4.0
        IL_0002: stloc.0
        IL_0003: ldloc.0
        IL_0004: ldc.i4.1
        IL_0005: add
        IL_0006: stloc.0
        IL_0007: ret
    }

Digging into it, IL_0000 (henceforth line 0) is a no-op command, that is, do nothing.  Line 1 pushes a 0 onto the stack.  Line 2 pops the top value of the stack (the 0 just pushed) into the variable [0].  Line 3 pushes the value of variable [0] onto the stack.  Line 4 pushes a 1 onto the stack.  Line 5 adds the top two values in the stack and pushes the results.  Line 6 pops the stack value into variable [0].

Not very interesting in and of itself, but what if we change from post-increment to pre-increment.  What change would we see?  Well, none, because the compiler knows that it doesn't matter in this expression.  To see a difference, we need to get a little bit more complex.  How about something ridiculous like:

        static void Main(string[] args)
        {
            int x = 0;
            int y = 0;
            int z = 0;
            z = x++ + ++y;
        }

which becomes:

    .method private hidebysig static void Main (
            string[] args
        ) cil managed
    {
        .entrypoint
        .locals init (
            [0] int32 x,
            [1] int32 y,
            [2] int32 z
        )

        IL_0000: nop
        IL_0001: ldc.i4.0
        IL_0002: stloc.0
        IL_0003: ldc.i4.0
        IL_0004: stloc.1
        IL_0005: ldc.i4.0
        IL_0006: stloc.2
        IL_0007: ldloc.0
        IL_0008: dup
        IL_0009: ldc.i4.1
        IL_000a: add
        IL_000b: stloc.0
        IL_000c: ldloc.1
        IL_000d: ldc.i4.1
        IL_000e: add
        IL_000f: dup
        IL_0010: stloc.1
        IL_0011: add
        IL_0012: stloc.2
        IL_0013: ret
    }

A bit more complex, but Telerik JustDecompile does a great job turning it into readable C#:

        private static void Main(string[] args)
        {
            int x = 0;
            int y = 0;
            int z = 0;
            int num = x;
            x = num + 1;
            int num1 = y + 1;
            y = num1;
            z = num + num1;
        }

Notice the use of temporary variables to represent the pushed stack values?

So, that is it for right now, I want to look at some of the more complex expressions so we will stay with 1.4 next time.

CDN for Everyone, Except SharePoint

CloudFlare is a great CDN to use for your personal sites.  It helps make your site run faster, and protects you from hacking.  The only problem is that the reverse proxy mucks with SharePoint in a very bad way.  I haven't tracked the issue down, but I have felt the pain.

Tuesday, July 29, 2014

Free Server 2012 R2 Hyper-V

So am I the last to know that Microsoft is offering Microsoft Server 2012 R2 Core running Hyper-V for free?  It has been a while since I did any Hyper-V work, but the new incarnation is pretty awesome!  The free version offers the same level of functionality as the full versions, just without the full OS running outside of it.  You get the same scaling, live migration, DR-redundancy, and clustering.  It really makes me want to build a server to try it on. ;-)

Using the Where-Object in Powershell

Earlier I posted some scripts that I use with my Hyper-V instances to reduce clicking and mentioned filtering.  To Filter, just use a Where-Object like so:

Get-VM | Where-Object{$_.Name -Like "SQL*"} | Stop-VM

It makes the scripts from before more powerful, especially if you use some standard naming conventions for your lab VMs.  Technet has a good coverage of the parameters you can use in a Where-Object, but I mainly use the -Like comparison operator to match regular expressions.

Monday, July 28, 2014

Combine Azure and AutoSPSourceBuilder for the win!

Do you have a remote client and limited bandwidth at home?  Need to install SharePoint at the far end of a small pipe?  Want to be Made of Win?  Stand up an Azure VM and build your complete SharePoint installation source, including prerequisites and slip streamed updates using AutoSPSourceBuilder!  You can then zip up the (large) results and download it via a corporate pipe rather than wait the 9+ hours to upload it through your residential connection.  Bonus, install the Web Server (IIS) role on the VM and you can avoid issues with FTP.  Just make sure to do your downloading at night to not interfere with normal business Reddit browsing.

Scripting Hyper-V on Windows 8

I am working up a SharePoint farm in Hyper-V on my nifty new (and large) laptop and there are a few scripts that I use rather often.  I don't have any other VMs on my box, so they are non-discriminatory, but some simple naming conventions would allow extending them to respect labs.  Here they are:

PS D:\Lab Scripts> Get-VM | Stop-VM

PS D:\Lab Scripts> Get-VM | Checkpoint-VM -SnapshotName "Base"

PS D:\Lab Scripts> Get-VM | GET-VMSnapshot –Name "Base" | 
                            RESTORE-VMSnapshot –confirm:$False

PS D:\Lab Scripts> Get-VM | Start-VM

For a little more information about what is going on, Get-VM gets a list of the VMs on the machine and the pipe ( | ) passes the list as arguments to the next command.  I will leave the rest of the commands for you to look up, assuming they aren't perfectly obvious.

Wednesday, July 9, 2014

Changing the Subscription Name in Azure

When you set up your Windows Azure subscription and get ready to start scripting against it, you will probably find that the Subscription Name is something like "Windows Azure MSDN - Visual Studio Ultimate" or "Azure Free Trial". This isn't too bad, but once you start managing several subscriptions, you will need to differentiate them. It is pretty easy, just go to https://account.windowsazure.com/Subscriptions and click on the subscription you want to modify:
Then click on Edit subscription details:
And enter the name:

Wednesday, May 28, 2014

Endpoint ranges for Azure VMs made easy

Short and sweet, add multiple endpoints to your Azure VMs through scripting.  Here it is:

Thursday, May 22, 2014

Create an SSL Certificate for a lab server hosting multiple domains

Building a certificate for a lab machine can be a little bit challenging.  The web front end servers tend to have multiple DNS names that they are responsible for, and IIS doesn't like to expose more than one certificate.  Here are the steps to build a certificate that can be used for multiple domain names on Windows Server 2012.
1.       Open a command prompt and type MMC
2.       Click FileAdd/Remove Snap-in…

3.       Choose Certificates in the list of Available snap-ins and click Add

4.       Choose Computer account and click Next

5.       Choose Local computer and then click Finish and Ok


6.       Open the Certificates node and right click on Personal and choose All Tasks and Request New Certificate…

7.       Click Next on the first window and Next on the next window


8.       Choose the Web Server template and then click the More information is required to enroll for this certificate.  Click here to configure Settings. link

9.       On the Subject tab, add in all of the Type information

10.   Then add in all of the Alternative nameType information, including all of the DNS names you want the certificate to cover.

11.   Click OK and Enroll
Your certificate has now been created.  Export it and install it on whichever web front end servers you need to secure.