Scott Hanselman

NoSQL .NET Core development using an local Azure DocumentDB Emulator

December 04, 2016 Comment on this post [19] Posted in ASP.NET MVC | Azure
Sponsored By

I was hanging out with Miguel de Icaza in New York a few weeks ago and he was sharing with me his ongoing love affair with a NoSQL Database called Azure DocumentDB. I've looked at it a few times over the last year or so and though it was cool but I didn't feel like using it for a few reasons:

  • Can't develop locally - I'm often in low-bandwidth or airplane situations
  • No MongoDB support - I have existing apps written in Node that use Mongo
  • No .NET Core support - I'm doing mostly cross-platform .NET Core apps

Miguel told me to take a closer look. Looks like things have changed! DocumentDB now has:

  • Free local DocumentDB Emulator - I asked and this is the SAME code that runs in Azure with just changes like using the local file system for persistence, etc. It's an "emulator" but it's really the essential same core engine code. There is no cost and no sign in for the local DocumentDB emulator.
  • MongoDB protocol support - This is amazing. I literally took an existing Node app, downloaded MongoChef and copied my collection over into Azure using a standard MongoDB connection string, then pointed my app at DocumentDB and it just worked. It's using DocumentDB for storage though, which gives me
    • Better Latency
    • Turnkey global geo-replication (like literally a few clicks)
    • A performance SLA with <10ms read and <15ms write (Service Level Agreement)
    • Metrics and Resource Management like every Azure Service
  • DocumentDB .NET Core Preview SDK that has feature parity with the .NET Framework SDK.

There's also Node, .NET, Python, Java, and C++ SDKs for DocumentDB so it's nice for gaming on Unity, Web Apps, or any .NET App...including Xamarin mobile apps on iOS and Android which is why Miguel is so hype on it.

Azure DocumentDB Local Quick Start

I wanted to see how quickly I could get started. I spoke with the PM for the project on Azure Friday and downloaded and installed the local emulator. The lead on the project said it's Windows for now but they are looking for cross-platform solutions. After it was installed it popped up my web browser with a local web page - I wish more development tools would have such clean Quick Starts. There's also a nice quick start on using DocumentDB with ASP.NET MVC.

NOTE: This is a 0.1.0 release. Definitely Alpha level. For example, the sample included looks like it had the package name changed at some point so it didn't line up. I had to change "Microsoft.Azure.Documents.Client": "0.1.0" to "Microsoft.Azure.DocumentDB.Core": "0.1.0-preview" so a little attention to detail issue there. I believe the intent is for stuff to Just Work. ;)

Nice DocumentDB Quick Start

The sample app is a pretty standard "ToDo" app:

ASP.NET MVC ToDo App using Azure Document DB local emulator

The local Emulator also includes a web-based local Data Explorer:

image

A Todo Item is really just a POCO (Plain Old CLR Object) like this:

namespace todo.Models
{
    using Newtonsoft.Json;

    public class Item
    {
        [JsonProperty(PropertyName = "id")]
        public string Id { get; set; }

        [JsonProperty(PropertyName = "name")]
        public string Name { get; set; }

        [JsonProperty(PropertyName = "description")]
        public string Description { get; set; }

        [JsonProperty(PropertyName = "isComplete")]
        public bool Completed { get; set; }
    }
}

The MVC Controller in the sample uses an underlying repository pattern so the code is super simple at that layer - as an example:

[ActionName("Index")]
public async Task<IActionResult> Index()
{
var items = await DocumentDBRepository<Item>.GetItemsAsync(d => !d.Completed);
return View(items);
}

[HttpPost]
[ActionName("Create")]
[ValidateAntiForgeryToken]
public async Task<ActionResult> CreateAsync([Bind("Id,Name,Description,Completed")] Item item)
{
if (ModelState.IsValid)
{
await DocumentDBRepository<Item>.CreateItemAsync(item);
return RedirectToAction("Index");
}

return View(item);
}

The Repository itself that's abstracting away the complexities is itself not that complex. It's like 120 lines of code, and really more like 60 when you remove whitespace and curly braces. And half of that is just initialization and setup. It's also DocumentDBRepository<T> so it's a generic you can change to meet your tastes and use it however you'd like.

The only thing that stands out to me in this sample is the loop in GetItemsAsync that's hiding potential paging/chunking. It's nice you can pass in a predicate but I'll want to go and put in some paging logic for large collections.

public static async Task<T> GetItemAsync(string id)
{
    try
    {
        Document document = await client.ReadDocumentAsync(UriFactory.CreateDocumentUri(DatabaseId, CollectionId, id));
        return (T)(dynamic)document;
    }
    catch (DocumentClientException e)
    {
        if (e.StatusCode == System.Net.HttpStatusCode.NotFound){
            return null;
        }
        else {
            throw;
        }
    }
}

public static async Task<IEnumerable<T>> GetItemsAsync(Expression<Func<T, bool>> predicate)
{
    IDocumentQuery<T> query = client.CreateDocumentQuery<T>(
        UriFactory.CreateDocumentCollectionUri(DatabaseId, CollectionId),
        new FeedOptions { MaxItemCount = -1 })
        .Where(predicate)
        .AsDocumentQuery();

    List<T> results = new List<T>();
    while (query.HasMoreResults){
        results.AddRange(await query.ExecuteNextAsync<T>());
    }

    return results;
}

public static async Task<Document> CreateItemAsync(T item)
{
    return await client.CreateDocumentAsync(UriFactory.CreateDocumentCollectionUri(DatabaseId, CollectionId), item);
}

public static async Task<Document> UpdateItemAsync(string id, T item)
{
    return await client.ReplaceDocumentAsync(UriFactory.CreateDocumentUri(DatabaseId, CollectionId, id), item);
}

public static async Task DeleteItemAsync(string id)
{
    await client.DeleteDocumentAsync(UriFactory.CreateDocumentUri(DatabaseId, CollectionId, id));
}

I'm going to keep playing with this but so far I'm pretty happy I can get this far while on an airplane. It's really easy (given I'm preferring NoSQL over SQL lately) to just through objects at it and store them.

In another post I'm going to look at RavenDB, another great NoSQL Document Database that works on .NET Core that s also Open Source.


Sponsor: Big thanks to Octopus Deploy! Do you deploy the same application multiple times for each of your end customers? The team at Octopus have taken the pain out of multi-tenant deployments. Check out their latest 3.4 release

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook bluesky subscribe
About   Newsletter
Hosting By
Hosted on Linux using .NET in an Azure App Service

I suck at vacation - What I did this week

December 01, 2016 Comment on this post [16] Posted in Musings
Sponsored By

Well, it seems I'm lousy at vacation. I'm still learning what I'm supposed to do. My wife is working and the kids are still in school so here was my week.

3D Printed Brackets for my new HTC Vive

I treated myself to an HTC Vive Room-Scale VR system. I'll blog extensively about this later but let me just tell you. It's AMAZING. I've used Google Cardboard, I've used Gear VR, I've used Oculus. Vive is it. Full Room-scale VR with something like the Doom 3 VR Mode is amazing. This fellow has a version of Doom 3 coded up at GitHub that modifies your existing purchased version and adds a REALLY compelling VR experience. I will say spent less time fighting demons and more time looking closely at wall textures. I admit it.

There's a joke about folks who have 3D Printers. We just end up printing brackets to hold stuff.  Well, I got a Vive so I wanted a nice way to mount it. Problem solved.

I dig #3Dprinting because you can make EXACTLY the brackets you need in a few hours!

A photo posted by Scott Hanselman (@shanselman) on

3D Printed a Rifle Stock for the Vive

There's a popular VR game called Onward. It's basically a Call of Duty-type squad shooter with a focus on squad teamwork and realism. However, holding two VR controllers up to your cheek and pretending they are a rifle doesn't really work. Fortunately an intrepid maker named SGU7 made a prototype you can 3D Print.

I made one first in Yellow but it broken because it lacked enough infill. I made it again in black (because I had a lot of black. I wish it looked less aggressive, though) and it works great. Note that the part in my hand is a controller and the other controller is attached to the front. The front one can pop off and act as your left hand to reload and throw grenades.

It was a challenging print with five large pieces and two small along with screws and nuts to hold it together. However, it was super fun and it makes the game WAY more realistic. More on this later. I've also been experimenting with some new exotic filaments.

37b7b79b793db7b489b50496b1f5787a_preview_featured

Made an AdaFruit Cupcade Raspberry Pi MAME Arcade

My teenage nephew and I worked on a Cupcade a few months ago but it was his. I 3d printed and made a PiGrrl (Raspberry Pi GameBoy) last year, so I figured I'd make a Cupcade (Raspberry Pi tiny Multi-Arcade Machine Emulator) as well. It's also somewhat challenging but I never really had the time until vacation. You can get the plans and source many of the parts locally, or you can get a complete kit from Adafruit. I did the partial kit for cheaper without the plastic case, then had a local makerspace lasercut a $5 piece of clear acrylic.

Set up Alexa to talk to my Nightscout-based Blood Sugar system

I got a few Amazon Alexa "Echo Dot" devices, so now we have three around the house. I upgraded my Nightscout Site (this is the Azure-based system that that allows remote management and viewing of my blood sugar as a Type 1 Diabetic.

The most recent update of Nightscout added Alexa support. I headed over to https://developer.amazon.com and made a dev account and got it all working. It's pretty slick. I can ask it all kinds of things (as can my kids. They love to know about how I'm doing when I'm out of town.)

image

Here's a video of it working!

"Alexa, what's my blood sugar?" #Diabetes @nightscoutproj #video

A video posted by Scott Hanselman (@shanselman) on


Basically I've been just making stuff and fixing stuff around the house. I even sat in a café and read the news. Madness.

I wonder if I could do this full time? I guess that's called retirement. ;)


Sponsor: Big thanks to Octopus Deploy! Do you deploy the same application multiple times for each of your end customers? The team at Octopus have taken the pain out of multi-tenant deployments. Check out their latest 3.4 release

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook bluesky subscribe
About   Newsletter
Hosting By
Hosted on Linux using .NET in an Azure App Service

Docker on a Synology NAS - Also running ASP.NET and .NET Core!

November 30, 2016 Comment on this post [27] Posted in Open Source
Sponsored By

Docker on Synology is amazingI love my Synology NAS (Network Attached Storage) device. It has sat quietly in my server closet for almost 5 years now. It was a fantastic investment. I've filled it with 8TB of inexpensive Seagate Drives and it does its own flavor of RAID to give me roughly 5TB (which, for my house is effectively infinite) of storage. In my house it's just \\SERVER or http://server. It's a little GNU Linux machine that is easier to manage and maintain (and generally deal with) that just chills in the closet. It's a personal cloud.

It also runs:

  • Plex - It's a media server with over 15 years of home movies and photos. It's even more magical when used with an Xbox One. It transcodes videos that then download to my Windows tablets or iPad...then I watch them offline on the plane.
  • VPN Server - I can remotely connect to my house. Even stream Netflix when I'm overseas.
  • Surveillance Station - It acts as a DVR and manages streams from a dozen cameras both inside and outside the house, scanning for motion and storing nearly a week of video.
  • Murmur/Mumble Server - Your own private VOIP chat service. Used for podcasts, gaming, private calls that aren't over Skype, etc.
  • Cloud Sync/Backup - I have files in Google Drive, Dropbox, and OneDrive...but I have them entirely backed up on my Synology with their Cloud Sync.

51FvMne3PyL._SL1280_Every year my Synology gets better with software upgrades. The biggest and most significant upgrade to Synology has been the addition of Docker and the Docker ecosystem. There is first class support for Docker on Synology. There are some Synology devices that are cheaper and use ARM processors. Make sure you get one with an Intel processor for best compatibility. Get the best one you can and you'll find new uses for it all the time! I have the 1511 (now 1515) and it's amazing.

ASP.NET Core on Docker on Synology

A month ago Glenn Condron and I did a Microsoft Virtual Academy on Containers and Cross-Platform .NET (coming soon!) and we made this little app and put it in Docker. It's "glennc/fancypants." That means I can easily run it anywhere with just:

docker run glennc/fancypants

Sometimes a DockerFile for ASP.NET Core can be as basic as this:

FROM microsoft/aspnetcore:1.0.1
ENTRYPOINT ["dotnet", "WebApplication4.dll"]
ARG source=.
WORKDIR /app
EXPOSE 80
COPY $source .

You could certainly use Docker Compose and have your Synology running Redis, MySql, ASP.NET Core, whatever.

Even better, since Synology has such a great UI, here is Glenn's app in the Synology web-based admin tool:

Docker on Synology - Node and ASP.NET Core Apps 

I can ssh into the Synology (you'll need to SSH in as root, or you'll want to set up Docker to allow another user to avoid this) and run docker commands directly, or I can use their excellent UI. It's really one of the nicest Docker UIs I've seen. I was able to get ASP.NET Core and the Node.js Ghost blog running in minutes with modest RAM requirements.

image

Once Containers exist in Docker on Synology you can "turn them on and off" like any service.

ASP.NET Core on Docker on Synology

This also means that your Synology can now run any Docker-based service like a private version of GitLab (good instructions here)! You could then (if you like) do cool domain mappings like gitlab.hanselman.com:someport and have your Synology do the work. The Synology could then run Jenkins or Travis as well which makes my home server fit nicely into my development workflow without use any compute resources on my main machine (or using any cloud resource at all!)

The next step for me will be to connect to Docker running on Synology remotely from my Windows machine, then setup "F5 Docker Debugging" in Visual Studio.

image

Anyone else using a Synology?

* My Amazon links pay for tacos. Please use them.


Sponsor: Big thanks to Octopus Deploy! Do you deploy the same application multiple times for each of your end customers? The team at Octopus have taken the pain out of multi-tenant deployments. Check out their latest 3.4 release!

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook bluesky subscribe
About   Newsletter
Hosting By
Hosted on Linux using .NET in an Azure App Service

Publishing ASP.NET Core 1.1 applications to Azure using git deploy

November 23, 2016 Comment on this post [13] Posted in ASP.NET | Azure
Sponsored By

I took an ASP.NET Core 1.0 application and updated its package references to ASP.NET 1.1 today. I also updated to my SDK to the Current (not the LTS - Long Term Support version). Everything worked great building and running locally, but then I made a new Web App in Azure and did a quick git deploy.

Deploying ASP.NET Core 1.1 to Azure

Basically:

c:\aspnetcore11app> azure site create "aspnetcore11test" --location "West US" --git
c:\aspnetcore11app> git add .
c:\aspnetcore11app> git commit -m "initial"
c:\aspnetcore11app> git push azure master

Then I watched the logs go by. You can watch them at the command line or from within the Azure Portal under "Log Stream."

The deployment failed when Azure was building the app and got this error:

Build started 11/25/2016 6:51:54 AM.
2016-11-25T06:51:55         1>Project "D:\home\site\repository\aspnet1.1.xproj" on node 1 (Publish target(s)).
2016-11-25T06:51:55         1>D:\home\site\repository\aspnet1.1.xproj(7,3): error MSB4019: The imported project "D:\Program Files (x86)\dotnet\sdk\1.0.0-preview3-004056\Extensions\Microsoft\VisualStudio\v14.0\DotNet\Microsoft.DotNet.Props" was not found. Confirm that the path in the <Import> declaration is correct, and that the file exists on disk.
2016-11-25T06:51:55         1>Done Building Project "D:\home\site\repository\aspnet1.1.xproj" (Publish target(s)) -- FAILED.
2016-11-25T06:51:55    

See where it says "1.0.0-preview3-004056" in there? Looks like Azure Web Apps has some build that isn't the one that I have. Theirs has the new csproj/msbuild stuff and I'm staying a few steps back waiting for things to bake.

Remember that unless you specify the SDK version, .NET Core will use whatever is the latest one on the box.

Well, what do I have locally?

C:\aspnetcore11app>dotnet --version
1.0.0-preview2-1-003177

OK, then that's what I need to use so I'll make a global.json at the root of my project, then move my code into a folder under "src" for example.

{
"projects": [ "src", "test" ],
"sdk": {
"version": "1.0.0-preview2-1-003177"
}
}

Here I'm "pinning" the same SDK version. This will tell Azure (or you, if I gave you the code) to use this SDK version when building.

Now I'll redeploy with a git add, commit, and push. It works.

I think this should be easier, but I'm not sure how it should be easier. Does this mean that everyone should have a global.json with a preferred version? If you have no preferred version should Azure give a smarter error if it has an incompatible newer one?

The learning here for me is that not having a global.json basically says "*.*" to any cloud build servers and you'll get whatever latest SDK they have. It could stop working any day.


Sponsor: Big thanks to Octopus Deploy! Do you deploy the same application multiple times for each of your end customers? The team at Octopus have taken the pain out of multi-tenant deployments. Check out their latest 3.4 release!

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook bluesky subscribe
About   Newsletter
Hosting By
Hosted on Linux using .NET in an Azure App Service

The 2016 Christmas List of Best STEM Toys for your little nerds and nerdettes

November 19, 2016 Comment on this post [13] Posted in Musings
Sponsored By

Last year my 9 year old asked, "are we nerds yet?" Being a nerd doesn't have the negative stigma it once did. A nerd is a fan, and everyone should be enthusiastic about something. You might be a gardening nerd or a woodworking nerd. In this house, we are Maker Nerds. We've been doing some 3D Printing lately, and are trying to expand into all kinds of makings.

NOTE: We're gearing up for another year of March Is For Makers coming soon in March of 2017. Now is a great time for you to catch up on the last two year's amazing content with made in conjunction with http://codenewbie.org!

Here's a Christmas List of things that I've either personally purchased, tried for a time, or borrowed from a friend. These are great toys and products for kids of all genders and people of all ages.

Sphero Star Wars BB-8 App Controlled Robot

Sphero was a toy the kids got for Christmas last year that they are still playing with. Of course, there's the Original Sphero that's just a white ball with zero personality. I remember when it  came out and I was like, "meh, ok." But then Star Wars happened and I tell ya, you add a little head on the thing and give it some personality and it's a whole new toy.

Sphero Star Wars BB-8 App Controlled Robot

The Sphero team continues to update the firmware and software inside BB-8 even now and recently added a new "Sphero Force Band" so you can control Sphero with gestures.

However, the best part is that Sphero supports a new system called "The SPRK Lightning Lab" (available for Android, iOS, or other devices) that lets kids program BB-8 directly! It's basically Scratch for BB-8. You can even use a C-style language called OVAL when you outgrow their Scratchy system.

Meccano Micronoids

81r9vmEHZvL._SL1500_

I grew up in a world of Lincoln Logs and Erector Sets. We were always building something with metal and screws. Well, sets like this still exist with actual screws and metal...they just include more plastic than before. Any of these Meccano sets are super fun for little builders. They are in some ways cooler than LEGO for my kids because of the shear size of them. The Meccano Meccanoid 2.0 is HUGE at almost two feet tall. It's got 6 motors and there's three ways to program it. There's a large variety of Meccano robot and building kids from $20 on up, so they fit most budgets.

Arduino UNO Project Super Starter Kit from Elegoo

91XepSwZP5L._SL1457_

Arduino Kits are a little touch and go. They usually say things like "1000 pieces!"...but they count all the resistors and screws as a single part. Ignore that and try to look at the underlying pieces and the possibilities. Things move quickly and you'll sometimes need to debug Arudino Programs or search for updates but the fundamentals are great for kids 8-13.

I particularly like this Elegoo Arduino UNO Starter Kit as it includes everything you'll need and more to start playing immediately. If you can swing a little more money you can add on touchscreens, speakers, and even a little robot car kit, although the difficulty ratchets up.

Snap Circuits

Snap Circuits

I recommended these before on twitter, and truly, I can't sing about them enough. I love Snap Circuits and have blogged about them before on my blog. We quickly outgrew the 30 parts in the Snap Circuits Jr. Even though it has 100 projects, I recommend you get the Snap Circuits SC-300 that has 60 parts and 300 projects, or do what we did and just get the Snap Circuits Extreme SC-750 that has 80+ parts and 750 projects. I like this one because it includes a computer interface (via your microphone jack, so any old computer will work!) as well as a Solar Panel.

In 2016 Snap Circuits added a new "3D" kit that lets you build not just on a flat surface but expands building up walls! If you already have a SnapCircuits kit, remember that they all work together so you can pick this one up as well and combine them!

91NYoJujYYL._SL1500_

Secret Messages Kit

It's a fact - little kids LOVE secret messages. My kids are always doing secret notes with lemon juice as invisible ink. This kit brings a ton of "hidden writing systems" together in one inexpensive package. Ciphers, Braille, Code Breaking, and more are all combined into a narrative of secret spy missions.

817VYGOwvkL._SL1200_

What educational toys do YOU recommend this holiday season?

FYI: These Amazon links are referral links. When you use them I get a tiny percentage. It adds up to taco money for me and the kids! I appreciate you - and you appreciate me-  when you use these links to buy stuff.


Sponsor: Help your team write better, shareable SQL faster! Discover how your whole team can write better, shareable SQL faster with a free trial of SQL Prompt. Write, refactor and share SQL effortlessly, try it now.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook bluesky subscribe
About   Newsletter
Hosting By
Hosted on Linux using .NET in an Azure App Service

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.