scriptcs and using Azure Mobile Services from your scripts

Β· 733 words Β· 4 minutes to read

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
scriptcs -install ScriptCs.AzureMobileServices  
  1. using scriptcs packages.config installation
<?xml version="1.0" encoding="utf-8"?> <packages> <package id="ScriptCs.AzureMobileServices" targetFramework="net45" /> </packages> 

scriptcs -install  
  1. using nuget.exe
<?xml version="1.0" encoding="utf-8"?> <packages> <package id="ScriptCs.AzureMobileServices" targetFramework="net45" /> </packages> 

nuget.exe install -o packages  
scriptcs -restore  

Usage πŸ”—

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

var zumo = Require<AzureMobileServices>();  

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:

var itemTable = zumo.GetTable<Item>("filipservice");  

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:

var itemTable = zumo.GetTable<Item>("filipservice","myKey");  

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

var itemTable = zumo.GetTable<Item>("filipservice","myKey","myTableName");  

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 πŸ”—

var item = itemTable.Add(new Item { Name = "Hello world" });  
//item has been added. The returned object has the id of the newly inserted item too.  

Update πŸ”—

var item = new Item { Name = "Hello world" };  
itemTable.Update(1, item);  
//item with id == 1 has been updated  

Get πŸ”—

var item = itemTable.Get(1);  
//gets item with id == 1  

Delete πŸ”—

itemTable.Remove(1);  
//deletes item with id == 2  

Get all πŸ”—

var items = itemTable.Get();  
//gets all items in the table  

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:

public class Item  
{  
public int? id { get; set; }  
public string Name { get; set; }  
}

var zumo = Require<AzureMobileServices>();  
var table = zumo.GetTable<Item>("filipservicetest");

//insert  
var newItem = table.Add(new Item { Name = "My new item" });  
Console.WriteLine(newItem.id + " " + newItem.Name);

//more insert  
table.Add(new Item { Name = "Another item" });  
table.Add(new Item { Name = "And another" });

//update  
newItem.Name = "New name!";  
var updatedItem = table.Update(newItem.id.Value, newItem);  
Console.WriteLine(updatedItem.id + " " + updatedItem.Name);

//get  
var item = table.Get(newItem.id.Value);  
Console.WriteLine(item.id + " " + item.Name);

//delete  
table.Remove(newItem.id.Value);

var items = table.Get();  
foreach (var i in items) {  
Console.WriteLine(i.id + " " + i.Name);  
}  

You can access the source of this project at Github.

About


Hi! I'm Filip W., a software architect from ZΓΌrich πŸ‡¨πŸ‡­. I like Toronto Maple Leafs πŸ‡¨πŸ‡¦, Rancid and quantum computing. Oh, and I love the Lowlands 🏴󠁧󠁒󠁳󠁣󠁴󠁿.

You can find me on Github, on Mastodon and on Bluesky.

My Introduction to Quantum Computing with Q# and QDK book
Microsoft MVP