New C# REPL and scripting capabilities

Overview

Along with a number of other welcome additions to Visual Studio, Visual Studio 2015 Update 1 brings back and old friend. the C# Interactive Window.  The C# Interactive Window is what’s known as a “Read-Eval-Print-Loop” (REPL).  Interactive Windows are great when you just need to test a simple block of code, and don’t want to create an entire sample project, or code file. 

Opening the C# Interactive Window

Once you have Visual Studio 2015 with Update 1 running, From the menu bar, select “View” | “Other Windows” | “C# Interactive

SNAGHTML1a4442a5

Once the “C# Interactive” window appears, you can issue a “#help” command to get basic help on the commands available:

image

For a full set of documentation, you can check out Interactive Window Documentation on GitHub. You should also check out Kasey Uhlenhuth’s “Introducing the Visual Studio ‘C# REPL’” video from the “Connect(); //2015” event.  The documentation and Kasey’s video gives a pretty good introduction to the functionality so I won’t try to repeat that here, however, there are a few extra things I’ve found that I thought I’d share with you.

Give it a Try

If you have the Interactive Window open, you can try just entering a simple expression like “Hello, “ + “World!” and pressing ENTER.  You should see the result “Hello, World!” printed out for you.

SNAGHTML1a69c7de

Let’s try something with variables (you can use the #cls or #clear commands to clear the interactive window before moving on if you like a clean slate):

SNAGHTML1a6ff114

If you want to reset the state of the interactive window, including any variable or method declarations, you can use the #reset command.  The reset command clears the state, and reloads the “CSharInteractive.rsp” file to provide a default set of references and namespace imports (“using” directives). For example, here you can see that once I issue the #reset, the hello and world variables are now undefined.

SNAGHTML1a73f130

To Semi-Colon or Not To Semi-Colon

When you type an expression (like “Hello, “ + “World!” ) into the window, you never use a semi-colon at the end, because it is an expression, not a statement.  The interactive window will always evaluate expressions, and output the result. 

When working with statements however, they MAY return a result (like in a method that returns a string).  For example, here I will create a method, called Concat that returns a string back to the caller.

SNAGHTML1a7bc779

Now, I’m going to call the method like an expression, WITHOUT a semi-colon.  Notice that the return value is displayed:

SNAGHTML1a83c8da

However, if I call it like a statement by adding a semi-colon.  The statement is executed, but the return value isn’t displayed. 

SNAGHTML1a854da5

Command Line REPL

Lastly, I’ll mention that there is a command line version of the REPL functionality.  It doesn’t support all of the same commands as the interactive window, notably #clear, #cls and #reset are missing.  For the #clear and #cls commands, you can just use the command prompt’s cls command.  For the #reset, just stop the REPL and restart it. 

You need the Visual Studio tools to be in the path, the easiest way is to just use the “Developer Command Prompt” shortcut installed with Visual Studio:

image

From there, you can use the “csi.exe” command line REPL utility.  Calling “csi” with a “/?” parameter shows it’s optional parameters:

SNAGHTML1a8f7eb7

Here is the command prompt version of our Concat method and calls from before:

SNAGHTML1a9116ac

You can exit the REPL by typing Ctrl-C.

CSX Scripts

Lastly, you can save your C# scripts into a text file with a .csx extension.  Actually, the extension isn’t really important as long as you reference the full name when call it.  For example, I’ve saved a version of our Concat method declaration and call from above in a text file:

image

With your CSX scripts, you can’t have loose expressions (like “Hello, “ + “World!”).   All code needs to be wrapped up in methods or be statements.  Also, output is not automatically displayed.  If you want to see something, output it yourself using Console.WriteLine(), etc.

SNAGHTML1a9ddc82

Wrap Up

It’s great to have the C# interactive window back.  I think there are a lot of use cases this will help solve and as a person who does demos a lot I think it will be a handy way to demo small concepts.  Check out the documentation, give it a try, and let me know what you think !

View your Application Insights data in Visual Studio

If you haven’t tried Microsoft Azure Application Insights before, it is a powerful tool for gaining insights in to how your web apps or mobile apps are being used, how they are performing, and what issues they may have.  You can even setup alerts in Application Insights to keep you informed when things happen that you care about.

Until now, you would primarily view the Application Insights data for your application using the Azure Portal (http://portal.azure.com)

SNAGHTML1d8838f

Now with the Visual Studio 2015 Update 1 release though, you can now view Application Insights Data from right within Visual Studio 2015. To do so, from the Visual Studio 2015 menu bar, select “View” | “Other Windows” | “Application Insights Search

SNAGHTML1dbffa6

Then, when prompted select the Application Insights Resource you want to use:

image

Finally, you can use the search an filter criteria to zero in on exactly the Application Insights data you want to see

SNAGHTML1e5e8a3

This new features makes it even easier to gain meaningful insight into your applications.  Give it a try and let me know what you think!

PowerShell script to open RDP session with an Azure VM

My Teammate Jeremy Foster recently shared some azure goodness that he added into his PowerShell Profile.  Then today, I ran across Brian Farnhill’s “Opening RDP session to an Azure VM with PowerShell” blog post and was inspired to add a simple function to my own PowerShell profile to simplify making RDP connections with my Azure VMs.

From a PowerShell command prompt, I opened my profile in notepad (or the text editor of your choice)

notepad $profile

Then, to the bottom of my profile I added the following function

function rdpvm ($ServiceName,$Name) {
  $vm = (Get-AzureVM -ServiceName $ServiceName -Name $Name)
  if($vm -and $vm.InstanceStatus -eq 'ReadyRole') {
    $rdp = (Get-AzureEndpoint -VM $vm | where { $_.LocalPort -eq 3389})
    $fqdn = (New-Object System.URI $vm.DNSName).Authority
    $port = $rdp.Port
    Write-Host "Opening Remote Desktop Session with $($fqdn):$($port)..."
    Start-Process "mstsc" -ArgumentList "/V:$($fqdn):$($port)"
  }
  else {
    Write-Warning "The VM $($vm.Name) is not running ($($vm.InstanceStatus)).  You should start it first"
  }
}

Now, in the future, when I am working with Azure in PowerShell I can simply run the following to open an RDP session with a VM.  Of course, this assumes I’ve already used “Add-AzureAccount” to sign into my azure subscriptions.

rdpvm -ServiceName <MyCloudServiceName> -Name <MyVMName>

Enabling Remote PowerShell Access for Secondary Administrators

If you want to use PowerShell Remoting to run scripts on a remote computer using an account other than the target machine’s built-in administrator account, you will  need to do a couple of things:

  1. On the target machine, create another user account and add it to the built-in “Administrators” group
  2. Set the HKLM/Software/Microsoft/Windows/CurrentVersion/Policies/System/LocalAccountTokenFilterPolicy (DWORD) to a value of 1 (you may need to add the LocalAccountTokeFilterPolicy DWORD value if it doesn’t exist). See http://support.microsoft.com/kb/942817/en-us for more details. 

Once you can do that you can use a Enter-PSSession or Invoke-Command with the credentials you created to remotely run PowerShell scripts against the target machine.

And yes, this applies to remoting into Azure Virtual Machines as well.  Just be aware that for Azure VMs you will need to import the certificate for the remote VM before you can successfully authenticate. 

For more info, check out Michael Washam’s “Introduction to Remote PowerShell with Windows Azure” and Jennelle Crother’s “The Imperfect Lab: Letting Additional Administrators Remotely Connect to Servers” .

Bret’s 70-532 MVA JumpStart Demo Files…

I’m presenting the Developing Microsoft Azure Solutions MVA (http://www.microsoftvirtualacademy.com/liveevents/developing-microsoft-azure-solutions) with Sidney Andrews today (12/19/2014).

We’ll be covering a number of topics:

  1. Azure Web Sites
  2. Cloud Services
  3. Virtual Machines
  4. Azure SQL Database
  5. Storage Services
  6. Application Architecture

Specifically, I’m covering the even numbered modules, on Cloud Services, Azure SQL Database, and Application Architecture.

You can grab a copy of my demos from http://aka.ms/BretsMVA532Demos .  Make sure to restore the NuGet packages in each solution.

Enjoy1

Azure Hackathon Resources

At a Hackathon?  Need quick access to some “Azure How To’s to get you started?” Here are some links.  These resources are just to get your started.  If you need something more in depth, check out “Microsoft Virtual Academy

  1. Windows Azure Pass Usage Tutorial if you don’t have an Azure Pass code, you can always create a free trial
  2. Azure Documentation – Everything you need to know about Microsoft Azure
  3. Quick 3-Minute getting started with Azure videos
  4. Creating a Windows Virtual Machine in Azure – Azure Virtual Machines are a great way to get started with the cloud
  5. Creating a Linux Virtual Machine in Azure – Yep! You can create Linux VMs in Azure.   You can also learn how to install the LAMP stack on your Linux VM 
  6. Create a Node.JS Website in Azure and Deploy from GitHub – Azure websites support the platforms you love like ASP.NET, PHP, Python and Node.js and you can deploy from source control services like Visual Studio Online or GitHub
  7. Visual Studio Online – You can create FREE, PRIVATE projects in Visual Studio Online.  Get more than just source control.
  8. Create an Azure Mobile Service for your Android App – Azure Mobile Services are an extremely powerful way to create backends for your mobile apps!
  9. Create an Azure Mobile Service for your iOS App – And iOS apps
  10. Create an Azure Mobile Service for your HTML5 Client App!
  11. Getting User Data with Azure Mobile Services

“DevTest Scenarios in the DevOps World” Resources

Today, 10/20/2014 Cale Teeter, Charles Sterling, Jeff Levinson and I will be delivering a live session on Microsoft Virtual Academy on “Dev/Test Scenarios in the DevOps World”. 

You can grab a copy of Bret’s Demo Files here: http://1drv.ms/1r0CFJw

Spark Core enabled Compressed Air Rocket Launcher

Earlier this month some teammates and I were lucky enough to participate in TechCrunch Disrupt Hackathon in San Francisco

SNAGHTML16ced63

Our team project was called “Notifly” and was an online social media aggregation and activation platform.  In the end, we used it to watch a twitter hashtag, and launch a compressed air rocket when a threshold was crossed! We used a Spark Core from Spark.with a Relay Shield to activiate the rocket via a REST API!  Super cool Here’s a pic of everything assembled. 

image

Anyhow, if you would like to build your own, you can find the instructions for doing so on github at http://github.com/dxdisrupt/rocket

SNAGHTML1802406

Dollar Store Food Containers Make Great Project Enclosures…

At the end of summer when my kids were in need of school supplies, we ended up passing through the local dollar store to find a few things. Right next to the pencil boxes and binders were plastic food containers for the kids to bring lunches and snacks to school in.  As soon as I saw them, I realized they would make fantastic project enclosures!  Here is the dollar sandwich container housing a Spark Core and a Relay Shield for my Compressed Air Rocket Launcher (I didn’t have a 9V battery clip on me when I put this together so the pink bubble wrap is there to keep the battery from knocking around):

image

The even smaller “snack” size boxes came in packs of three for $1.  Each one is a perfect (well, maybe a little tight) enclosure for an Arduino Uno with a a Shield and a 9V battery:

image

The one thing you do need to look out for is that sometimes the plastic can crack when you are drilling holes, so take your time, go slow.  And if you end up breaking one, it was only a buck!

Also, I went back just the other week, and the “back-to-school” stock has been pulled and replaced with Halloween items.  I couldn’t find these containers in my local dollar stores any more. There were others, but I didn’t like them as well.  However with some searching, I was able to find some online on dollartree.com

Just ordered my Edison!

I’ve been having a ton of fun with my Intel Galileo Blog Posts, but I have been anxiously awaiting the Intel Edison, a much smaller, but still “arduino compatible” development board.  On September 9, at the Intel Developer Forum (IDF) Intel announced the Edison, and showed off a lot of cool things you can do with it.

I just jumped on SparkFun and ordered both the Edison with the Arduino Breakout Kit as well as the Edison with the Mini Breakout Kit and some of the SparkFun “Blocks”.  Now I just have to wait 6-8 weeks to get them (fingers drumming on table).

Intel® Edison and Arduino Breakout Kit