Google+

Build 2015 Mega Post

This is currently a work in progress. This message will be removed in the next few days.

This outline contains all of the announcements from //Build/ 2015 that I that were noteworthy. This post will be updated as I fill in the details and link to the appropriate announcements and videos.

Keynote Highlights

Windows

Azure

  • SDK 2.6
  • Data lake service
    • HDFS compatible file system
    • Unlimited storage
    • Optimized for high throughput, low latency
  • Project Oxford
    • Face detection
    • Speech recognition
    • Vision information API
  • Service Fabric
  • SQL Database
    • SQL DB Transparent data encryption public preview
    • SQL DB Service Tier Advisor public preview - asses performance needs
    • Elastic Data Pool
    • Full-text Search
  • More than 500 new features in Azure in the past year
  • Over 50 trillion storage objects

Visual Studio

Misc

  • Project Spartan is now Edge
  • .NET support on Linux in Docker
  • .NET core preview for Linux and Mac
    • Additionally, run and debug .NET on Linux, in a Docker container
  • .NET 4.6 RC for Windows
  • Nano server
    • Admin GUIs on servers are poison
    • It's just a deployment option
    • Features are pulled in separately, not on disk
    • Hyper-V, aspnet, clustering support
    • Configuration via core PowerShell and WMI
    • Uses core Clr, most core engine components
    • Nano server is a subset of the full server surface area
    • An app that works on nano server will run on the full server
    • Optional Reverse forwarders package allows you run non-nano apps and silently fail on unsupported api calls
    • Lots of things can run with the reverse forwarders like Java, node.js
    • Less than 3 minutes to install
    • 400mb vs 5+gb for full server
    • Inject drivers in image before install
  • Office has a unified endpoint for all API calls
    • Add-ins run everywhere that office does *

Like this post? Please share it!

See a mistake? Edit this post!

Designing for Testability

This is a segment that I wrote for a book used internally and with Microsoft partners.

Building a car may seem like a complicated process (because it is), but imagine if the design of the car changed on a daily basis. Also imagine if the tools and processes changed daily. With few things being constant, creating a quality automobile would become increasingly difficult. Welcome to the world of software engineering. The factory is your favorite editor, the cars are your code, and the customer is everyone.

The reality is that all developers test their code. How many times have you made a change, compiled the code, waited for the application to run, and then navigated through the application to execute the code you just wrote? The problem is that efficiency goes down dramatically the longer you have to wait to see a result from the changes you make. Looking back at the car example, would you rather buy a car whose quality was checked in every step during construction, or would it be acceptable if the testing was done only once the car was assembled? Considering the higher potential for significant re-work and the associated schedule impact when testing is deferred to the end, which approach do you think will prove faster?

Avoiding bugs is much more enjoyable than having to fix bugs. Features such as type systems exist to join the “result” of the code with the creation of the code. Defining a variable to be a particular type is actually a specification or test definition that runs at compile time against all usages of that variable. Languages like TypeScript allow us to use the full power of a dynamic language like JavaScript, with the safety of a type system.

Since software development is highly dynamic, there is no single way to build applications, but we can increase code and app quality by following proven principles. For example, the SOLID principles published by Robert C. Martin provide some fundamental tenants.

SOLID is actually an acronym of 5 other acronyms, defined below. With good design principles comes testability. Testing is a word that typically strikes fear into the hearts of developers. Like many fears, understanding removes the fear. In fact, a good unit test should be simple, useful, and actually saves development time. Using that definition, it’s not possible to have a unit test that will “cost” you time.

Instead they provide you with immediate value.

SRP The Single Responsibility Principle A class should have one, and only one, reason to change.
OCP The Open Closed Principle You should be able to extend a class’s behavior, without modifying it.
LSP The Liskov Substitution Principle Derived classes must be substitutable for their base classes.
ISP The Interface Segregation Principle Make fine grained interfaces that are client specific.
DIP The Dependency Inversion Principle A class should have one, and only one, reason to change.
SRP The Single Responsibility Principle Depend on abstractions, not on concretions.

(source: Agile Principles, Patterns, and Practices in C# by Robert C. Martin)

The figure below illustrates this difference. Code not designed for testability will be, well, largely untestable. This is what typically deters developers from making testing part of their development process. As we learn how to craft our code into well designed components, the amount of code that is easily tested will be the majority. There will be some additional code that has a questionable return on testing investment, and code that will never make sense to test. The Y-Axis is the degree of testability, or the ease of which a particular piece of logic can be tested. The X-Axis is defined as “benefit,” which is comprised of how often the code will be reused, the importance of the code, the complexity of the code, and the risk level associated with the code not working as expected.

Testability

Like this post? Please share it!

See a mistake? Edit this post!

Sending Real-Time Sensor Data to Clients Using SignalR

You don't have to go very far into the past to find yourself in a time where pushing data from a server to a client was a huge pain. Sure, you could use tricks like polling or long-polling, but it was difficult and error prone. Fortunately, we have technologies today that make it drop dead simple without complexity or bad performance.

If you're using a technology like Node.js, then something like Socket.io or Faye is for you. They're not the focus of this post, but they are both extremely easy to use.

Instead, I'll be focusing on an ASP.NET MVC application that will send data in near real-time to a web client.

SignalR

SignalR is based on the concept of hubs. You create hubs in your .NET application (ASP.NET/MVC/etc). Here is what a simple hub can look like:

public class ChatHub : Hub
{
    public void Send(string name, string message)
    {
        Clients.All.broadcastMessage(name, message);
    }
}

If you haven't used SignalR, Be sure to familiarize yourself with the above example before moving on. The server calls the Send method, and that method calls broadcastMessage on all of the connected clients.

SignalR is awesome for a few reasons:

  • It makes our life easy by hiding the complex process of determining how to connect to a server.
  • SignalR is also extremely fast. It's hard to find benchmarks, but I've read reports of getting 10,000+ calls/sec with a single server.
  • It is possible to scale out to multiple servers.

The Scenario

In my manufacturing projects, I wanted an easy way for web clients to subscribe to real-time data streams.

Real-Time Data Feeds

On the left is a list of streams that the user can subscribe to, and the chart on the right plots those values as they arrive from the server.

The Server Implementation

The easiest way to wire up SignalR for our scenario is to use the Microsoft ASP.NET SignalR OWIN Nuget Package. OWIN gives us an easy way to inject functionality into our server. To enable OWIN, we'll also need to pull in the Microsoft.Owin.Host.SystemWeb Nuget Package.

Once those packages are installed, SignalR can be wired up simply by adding a file with the following contents:

using Microsoft.Owin;
using Owin;

[assembly: OwinStartup(typeof(MyNamespace.Startup))]

namespace MyNamespace
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            app.MapSignalR();
        }
    }
}

That's how easy it is.

Let's talk hubs. A hub is just a regular class that inherits from Hub. Just keep in mind that hubs are transient, meaning that anything you store in a member property will be lost in the next call. That's actually a good thing since we may be spanning multiple servers. Storing any state should happen outside of the hub.

Now, let's create our first Hub:

using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Hubs;

namespace Manufacturing.Api.Hubs
{
    public class DatasourceRecord : Hub
    {
    }
}

It doesn't do anything yet, but it gives us a place to put our methods.

To allow clients to subscribe to the streams they're interested in, I'm going to use a feature in SignalR called Groups. Groups give us a way to fan out data to clients that belong to that group. Remember how I said that hubs can't store state? Groups do store the mapping between clients and group names.

Let's provide some methods for the client to register for streams they're interested in:

(Note: I call them Datasources)

    public const string GroupLabelPrefix = "Datasource_";

    public void Register(int datasourceId)
    {
        Groups.Add(Context.ConnectionId, GroupLabelPrefix + datasourceId);
    }

    public void Unregister(int datasourceId)
    {
        Groups.Remove(Context.ConnectionId, GroupLabelPrefix + datasourceId);
    }

Context is an inherited member that allows us to look up the unique ID of the client making the request. By adding and removing clients in the groups, they're explicitly subscribing to what they're interested in. Security could easily be layered into these methods as needed.

Here is the method call to get data to the subscribed clients:

public static void NewDataRecord(DataRecord record)
{
    var context = GlobalHost.ConnectionManager.GetHubContext<DatasourceRecord>();

    var groupName = GroupLabelPrefix + record.DatasourceId;
    var group = context.Clients.Group(groupName);
    if (group != null)
    {
        group.newRecord(record);
    }
}

Notice that this method is static, which was intentional. This allows us to easily call into the hub from code elsewhere. In my case, I'm receiving data through Azure Event Hubs.

The Client Implementation

For the web client, we need to pull in the SignalR client libraries. We can get these with the Microsoft.AspNet.SignalR.JS Nuget package.

Now add a reference to the client library in your HTML. We also need to reference a special script called signalr/hubs. This second reference is a dynamically generated client library based on the server methods that we'll define later.

<script src="Scripts/jquery.signalR-2.2.0.min.js"></script>
<script src="signalr/hubs"></script>

Now, we can reference our hub and handle server events:

dataHub = $.connection.DatasourceRecord;

dataHub.client.newRecord = function (record) {
    console.log('Record from server: ' + msg);
}

SignalR is handling all of the serialization/deserialization of the record, so we actually get back a JavaScript object, not a string.

It's important to subscribe to at least 1 event before starting SignalR so that the hub gets started. Now, let's initiate the connection:

$.connection.hub.start().done(function () {
    console.log('Connected to SignalR hub')
}).fail(function (err) {
    console.log('Failed to connect to SignalR hub ' + err);
});

And when we want to call the server to subscribe to a stream:

dataHub.server.Register(1);

CORs

CORS is a horrible, horrible pain that masquerades as security. If you want your SignalR hub to be hosted on a server that is different than the server that hosts your front-end, you'll need to include the Microsoft.Owin.Cors Nuget package and use the following code in your OWIN startup:

app.Map("/signalr", map =>
{
    map.UseCors(CorsOptions.AllowAll);
    var hubConfiguration = new HubConfiguration();
    map.RunSignalR(hubConfiguration);
});

More Information

Like this post? Please share it!

See a mistake? Edit this post!

Jason Young I'm Jason Young, software developer at heart, technical evangelist for Microsoft by day. This blog contains my opinions, of which my employer may not share.

@ytechieGitHubLinkedInStack Overflow