scriptcs and accessing Azure Mobile Services like a pro - StrathWeb

Strath

May 12th, 2013

scriptcs and using Azure Mobile Services from your scripts

Hopefully by now you have already heard about the sriptcs project, which allows you to write script based applications with C# and Nuget.

If not, have a look at the readme and the great introduction post by Scott Hanselman to get started.

Actually, just today, we have released v.0.4 of scriptcs! To celebrate that, let’s revisit one of our favorite topics – Azure Mobile Services – and how you can use it with scriptcs.

Extending scriptcs

Scriptcs provides a robust extensibility model, called script packs, where developers can provide their own extensions for script authors.

The extensions can be automatically discovered by scriptcs and accessed from script context using the node.js-like Require syntax.

You can find a master list of all script packs in the scriptcs wiki. Most importantly, script packs are supported both in regular script execution mode, as well as in the brand new scriptcs REPL (interactive shell) mode!

Azure Mobiles Services in scriptcs

Azure Mobile Services now provides a managed client library, to use in all .NET 4.5 projects, allowing you to access ZUMO easily from your .NET code.

However, the library uses async/await internally, which currently are not supported in the scriptcs (since they are not supported by Roslyn CTP), making it unusable in script context.

To remedy that, I have created an Azure Mobile Services script packs that solves all these problems – which is built around the REST API of Azure Mobile Services. This library, is currently synchronous (explicitly blocking), as the major point of script-based code is simplicity, and not dealing with Task continuations (and again, simple “await” is not yet doable).

To get started, install ScriptCs.AzureMobileServices from Nuget – and there are a few ways to do that.

1) using scriptcs direct installation

2) using scriptcs packages.config installation

3) using nuget.exe

Usage

You can now start writing a CSX script that is capable of accessing your Azure Mobile Services.

This grabs the Zumo script pack. You can now initialize an individual table, by passing in the name of your Zumo service and a Type to be used for serialization:

Note: due to the convention in Azure Mobile Services, the primary key column of your data object needs to be a nullable int with the name “id” (or marked with [JsonProperty("id")] or [DataMember(Name="id")]). Otherwise the serialization will fail.

If you require an AUTH key, you can pass that too:

The name of the table is inferred from the class you are using. If that’s different, you can pass that in as well:

That’s it!

Next you can start performing the typical CRUD operations on the table. Add and update operations will return the modified/created object.

Add

Update

Get

Delete

Get all

And that is it! You can combine this functionality with other script packs, or simply use in your scripts to easily persist/retrieve data and information.

This script pack works both from regular CSX script, and from the new scriptcs REPL.

Here is a full example with a screenshot:

You can access the source of this project at Github.

Be Sociable, Share!

  • Pingback: The Morning Brew - Chris Alcock » The Morning Brew #1354

  • http://twitter.com/gblock Glenn Block

    Nice Filip!

  • Pingback: WindowsAzureRocks

  • luisrudge

    Nice!

  • Pingback: Dew Drop – May 14, 2013 (#1,546) | Alvin Ashcraft's Morning Dew

  • Brandon

    Just getting started with ScriptCS tonight. Every time I try to -install a NuGet package (including the one outlined here) I get the following:

    C:UsersBrandon>scriptcs -install ScriptCs.AzureMobileServices
    INFO : Installing packages…
    INFO : Installation completed successfully.
    INFO : Copying assemblies to bin folder…

    Unhandled Exception: System.IO.FileNotFoundException: Could not find file ‘C:Us
    ersBrandonScriptCs.AzureMobileServices’.
    at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)

    Any ideas on what’s going on here?

    • http://www.strathweb.com/ Filip W

      we had a nasty bug in scriptcs 0.5. Upgrade to 0.5.1 (it’s on chocolatey now) and everything will be fine

  • Brandon

    Just getting started with ScriptCS tonight. Every time I try to -install a NuGet package (including the one outlined here) I get the following:

    C:UsersBrandon>scriptcs -install ScriptCs.AzureMobileServices
    INFO : Installing packages…
    INFO : Installation completed successfully.
    INFO : Copying assemblies to bin folder…

    Unhandled Exception: System.IO.FileNotFoundException: Could not find file ‘C:Us
    ersBrandonScriptCs.AzureMobileServices’.
    at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)

    Any ideas on what’s going on here?