Easy Background Processing with WebAPI and Windows Azure

The Problem

Assume, for example, that you are developing your own mobile app (or apps, in my case) and want to be able to send push notifications to these apps every once in a while.

You would require a server that will run somewhere in the background, collect new information every given interval and send relevant notifications to the client apps. You would also need to allow the mobile apps to register and unregister from that server, and supply a simple management console user interface for operations such as browsing though your logs, changing intervals and behaviors and so on.

Thinking it through you realize that you also require a SQL database, that the server has to be fully secured (you would not like anyone hacking in and pushing messages in your behalf into your mobile app, used by unknown users..), and that it has to be reliable and always ready.

Now, you could start by trying to locate a suitable and available server(s) and get it allocated for your purposes, or go out and purchase one. You would also have to make sure that the server (or servers) is licensed and installed with all that is required, from operating system through database engines, all the way to the web-server and other packages with the latest version. Your server should also be exposed to the internet but yet secured.

But even when you finally have that server running, who is going to make sure that the server is always up, operational and taken care of? What about variable bandwidth and storage capacities?

A simple and great solution for your background processing – Windows Azure.

The Solution

For my server, I start off by creating a Windows Azure SQL Database (No need to put up a fully secured, reliable, always ready SQL Server – CHECK!).

Back in my Visual Studio 2012, I use the “Server Explorer” tab to further create the tables, relations, keys etc., as Windows Azure SQL Databases act as any standard remote SQL server databases, just in the cloud. (No special tools – CHECK!)

Two pieces were still missing from the puzzle: the background service and a management console. Simple solution there – I create a Windows Azure Cloud Service composed of a Worker role and Web role. The Worker role would be used for background processing and the Web role would serve as the management console and Web-API host.

NOTE     A Windows Azure Web Site would not be a good enough solution for these purposes as it becomes dormant after a short timeout of not receiving new calls, thus any background service will stop working.

You have two options: create both elements through the Windows Azure console and download a ready-made Visual Studio project, or create what you need in Visual Studio using wizards and have Visual Studio take care of creating whatever is required on Azure for you.

I usually choose the latter as it is easier for me to do it all from the Visual Studio environment, where I have everything I need. You would need to install the Windows Azure SDK in order to have the cloud options in your Visual Studio.

Creating a cloud service is as simple as “File/New/Cloud/Windows Azure Cloud Service” (assuming you already have the required live account):

Adding a Windows Azure Cloud Service project

and selecting the desired roles:

image

The wizard creates ready-made solution and projects for the selected roles, already set to work with Entity Framework 5. The Worker role project already contains stubs for the “Run” and “OnStart” method overrides, allowing you to inject your business logic into the background service, while the Web role (if the preferred MVC 4 pattern selected) is already a full-blown website, with REST support, controllers, models, and everything that you need to form a secured websites, including pre-defined ASP.NET membership.

At this stage, both projects can already be published to Windows Azure.  You only have to worry about your specific code. You can also debug both projects using the Visual Studio and the “Windows Azure Compute Emulator” and “IIS Express”, that are installed as part of the Windows Azure SDK install.

So, what do we have here?

We just setup a WebAPI server, a website and a background service running against a SQL server in record time and with minimal effort. Having all of them running on Windows Azure, we also ensured reliability, manageability and consistency for our services.

Using the above steps, it took me less than a day to complete the rest of the code logic and get a push notifications server up and running. Want to see it in action? Check out the Toronto Events and Festivals Windows 8 and Windows Phone 8 apps as well as the Trashswag Windows 8 and Windows Phone 8 apps. Both use this background processing cloud service architecture to collect new information and send notifications to the apps.

If you’d like to go deeper into this concept, I’ve put some demo code on my SkyDrive. Going through the examples, you’ll see that there was very little code required in order to get Windows 8/Windows Phone 8 apps connected to the WebAPI based Windows Azure app. Please note that the WebAPI MVC 4 project uses a bunch of NuGet packages, automatically installed by the wizard within my project. Write me or comment below if you have any questions or need some help.

Featured on MSDN Canada’s Canadian Developer Connection

Developers, There Are UI Updates for You in Windows 8.1

Windows 8.1

With the RTM bits now available for MSDN subscribers, as you would have read in Jonathan Rozenblit’s editorial in the latest MSDN eNews newsletter, Windows 8.1 is here and should not be ignored.

For those of you who already have some experience developing Windows 8 apps, I compiled a (partial) list of changes that I find notable on the UI side. For those that haven’t, the below are things that you should absolutely not think twice about implementing in your apps.

The best part about these changes – Visual Studio 2013 now includes new and updated controls for XAML/C# and HTML/JavaScript to make these UI updates simple to implement. So let’s to through them:

Tiles
  • Tiles now have two new sizes, while older ones has been renamed. The new sizes : “Small” and “Large”, while “Normal” was renamed to “Medium”.
  • This does not mean that your old code would stop working, as the older names are still mapped to new ones in the enums for backward compatibility, but if you want to support the newer tile types, you would have to update your code.
  • The only restriction is that in order to support the Large tiles, you will also have to support Wide tiles in you app.

Multiple windows/No more snap-view

  • Snap view was replaced by a variable size window mechanism allowing the user to have multiple apps and windows running on the screen at the same time, with different widths, while an app may also spawn other apps and share the available space with it.
  • On the code side, this means that the older code dealing with snap-view will now render obsolete, such as: snap-view visual elements in XAML which are not required anymore, and ApplicationView.Value property, ApplicationView.TryUnsnap method and ApplicationViewState enum references becoming obsolete in your C#/JavaScript.
  • You now have to the following new properties in the ApplicationView class: AdjacentToLeftDisplayEdge and AdjacentToRightDisplayEdge used to determine if your app window is adjacent to one of the edges, IsFullscreen and Orientation.

Search

  • The Search charm, while still there, will now act more of a global (computer-wide and/or web) search, and less of an in-app search. Visual Studio 2013 now offers new SearchBox control to be included in the apps themselves and used for in-app searches. Older, search-ready Windows 8 apps will still work with the search charm as they did before, but in order to search within the app the user would have to run it first and select it as target on the search target combobox. Not as intuitive as it used to be, or as using the in-app SearchBox control.

New/Improved Controls

  • Flyout: No need to use third party vendors’ flyout controls anymore. The Flyout control allows you to display a floating window which is not a dialog (unlike MessageDialog). Deriving from the Flyout control, you now also have the MenuFlyout and the SettingsFlyout controls.
  • Hub: New navigation control allowing you to to aggregate different types of information controls within one content control, therefore controlling the width of your app’s main screen. For example: Displaying a GridView of images in one hub section, a ListView of detailed information in another, and a form on a third section.
  • AppBar: Windows 8 already allowed you to include various controls in the app-bar, but Windows 8.1 offers the AppBarButton, AppBarToggleButton, and AppBarSeperator controls right out of the box to be used as part of the new CommandBar control within your app-bars. So, what is the difference? It’s all about styles and conventions. Using the new controls allows you to have your app-bars look and behave in a more conventional way while the CommandBar control automatically lays out the commands on the bar,
  • NavBar (HTML5/JavaScript only): A navigation list, similar to the AppBar, displaying hierarchical links at the top of each page.
  • BackButton (HTML5/JavaScript only): Similar to the back button that was already available in Windows 8 XAML code as part of the LayoutAware pages created by the wizard, this control allows the user to browse back through the page history up to the first page.
  • WebView: (HTML5/JavaScript only): Similar to the WebView control that was available in Windows 8 XAML, this control can be used to replace the use of iFrames to display external pages. The main advantage is in the content and browse history isolation.

As you can see, developing for Windows 8.1 is definitely not different than developing for Windows 8. It is actually easier. Migrating your apps to Windows 8.1 is mostly performed automatically, and you can make the required changes to support the new features in a fraction of the time it took you to develop your app originally. I would definitely suggest migrating your apps to allow your users to benefit from these improved UI capabilities.

While I did not cover all of the changes in Windows 8.1, and not even all of the changes to the UI, what you do have here are the ones that will make your development easier, while, and most importantly, enhancing the experience of your app for your users.

Featured on MSDN Canada’s Canadian Developer Connection

Tile? Secondary tiles? Live tile?

Thanks to everyone who attended my session about “Engaging users with live tiles and notifications” to the @DevTeachConfere.

The slides and demo code are available for download here.

Image

Azure Mobile Services vs. REST vs. XML Web-Services

You’re planning a new system, and already decided that you want the data to be hosted on the cloud.

Now, should you use web-services? REST (using WEB-API for example)? maybe Windows Azure Mobile Services?

Let’s see the differences between these technologies, but first- a short overview:

XML Web-services

The old and well-established type of remote operation mechanism, allowing you to expose your server application’s abilities and services using SOAP-XML contracts that can later be consumed by every client app capable of encoding and decoding XML messages.

This mechanism is platform-independent, and if you stick to basic SOAP-XML structures you can have clients running Microsoft operating-systems, Apple’s, Android or anything else working against your server application, having SOAP-XML as the lowest-common-denominator and the language spoken between your server application and the clients.

XML Web-Services may (and usually will) also supply metadata (WSDL/UDDI) that allows clients to work with your application server without any previous knowledge or information about the server application’s API structure or technology, and may also adapt to API format changes quite easily.

This type of services are cross-platform, self-explanatory, mobilizes well structured data and supports additional communication layers such as WS-Security or WS-Addressing.

REST:

“REST” (stands for “representational state transfer”) uses a much simpler approach than XML Web-Services.

While XML Web-Services rely on HTTP or other types of transport layers to send and receive XML messages to and from the server, REST goes back to the source and uses the HTTP command verbs themselves to perform operations and transfer information to and from your application server.

This protocol is much looser than XML Web-Services and will attempt to deduce what operation to perform and how to use the transferred parameters from the HTTP VERB and URI used.

The actual data can be transferred in any format, such as XML or JSON and is not bound to comply with any type of pre-defined protocol.

REST is light-weight, portable and easy to consume by HTTP enabled clients. You can have your service up and running in practically no-time.

On the other hand, though these services may also be self-explanatory (using “WADL”), REST might be less suitable if you require a more strict service API approach, defining exactly what operations to perform and the data to be transferred.

Also, additional communication layers are not supported, beyond HTTP and HTTPS.

Both XML Web-Services and REST services can be hosted on Windows Azure quite easily, and can both be accessed by mobile clients.

Azure Mobile Services:

Windows Azure Mobile Services is a set of Windows Azure services exposed as a pre-defined set of WEB-API hosted REST services designed to be consumed by mobile clients.

When creating mobile services, you can use the Windows Azure wizards to also create Windows 8, Windows Phone 8, Android or iOS client apps, already set-up to connect to your service using dedicated libraries for each platform.

The server is created for you, pre-defined to manage data on the cloud, manage users and authentication, schedule maintenance scripts and custom logic and integrate push notifications.

Azure Mobile Services biggest advantage is that it takes most of the common aspects of the design, coding and testing of the application server and mobile clients off your hands and allows you to focus on the custom features only.

“So, what should I use?”

Well, if you’re application server is intended to serve older/existing XML web-service clients, XML web-services are your only hope, though you might want to consider adding a REST interface to your application server as well.

But, if you are planning an application server that is intended to serve mobile clients, there’s no reason for you to re-invent the wheel when Windows Azure Mobile Services can have you kick-started very quickly with many aspects of the application server.

Moreover, whenever you are required to create the complete solution you can use the Windows Azure wizards to have your client apps already created (for all supported platforms) and set-up to use your application server.

As these services are exposed using REST, you can also have your application server accessible for other types of clients, just like any other REST application server, consulting the Windows Azure Mobile Services REST API Reference.

Debugging Windows Phone 8 apps with the emulator using Hotel Wi-Fi

Have you ever tried to run/debug your Windows Phone 8 apps when using Hotel mode Wi-Fi?

Hotel-mode Wi-Fi are guest access Wi-Fi routers that do not require a password to connect, but rather the first time you attempt to open a web-site you are routed to a login form, where you’re prompted with either a confirmation page or a user-id/password page.

So, where’s the problem?

You have your Visual Studio 2012 open, you already connected to the Hotel-mode router and made sure to open a page (google.com/microsoft.com/whatever) in order to go through the intermediate page and get internet connected.

Now, you launch your app, causing the emulator to load (takes about forever…) and the app to run just to find out that your app cannot connect to the internet!…

Checking again, you discover that your computer somehow reset the connection with the router and you have to go through that login page again. But, that’s not enough, as the connection will keep resetting every few seconds, making it almost impossible for you to run and debug your app, assuming it requires internet connection.

So what can you do if you just have to debug or demo your app from the emulator on such a network, assuming you do not have a Windows Phone 8 device around?

After many tries (and failures), I found only two possible solutions:

The first is to use your phone’s mobile data and tether the internet connection through Wi-Fi to your computer. But, mobile data doesn’t come cheap and you would rather use that complimentary Wi-Fi for your tests.

The solution I found was to run a USB tethering app on my Android phone, have the phone connect to the Hotel-mode Wi-Fi and tether the connection to the computer using cable.

That way, the connection is not being reset when using the emulator and you can run/debug your Windows Phone 8 app.

Demo code from my “Azure as the backbone” talk @DevTeachConfere

Thanks to everyone who attended my “Azure as the backbone” web-api based talk today @DevTeachConfere in Montreal.

Demo code can be found on my sky-drive here.

See how easy it is to create a web-api based app using the Visual Studio 2012 wizard, deploy it to Azure and then create Windows 8 and Windows Phone 8 apps that uses the service from within Azure.

Going through these examples you’ll see that there was very little code required in order to get these Windows 8/Windows Phone 8 apps connected to the web-api based Azure app.

Please note that the web-api MVC 4 project uses a bunch of NuGet packages, automatically installed by the wizard within my project.

Write me or comment for questions!

Demo code from my Practical Kinect session @DevTeachConfere

Thanks to everybody who attended my session about practical kinect at the Dev-Teach conference (@devteachconfere) today, to hear about the Kinect SDK, the different types of interactions and the way to implement them in Windows.

The demo code can be downloaded from my SkyDrive: here

The demo code includes two solutions:

KinectPointers: demonstrating how to react to hand movement by moving the mouse and generating a mouse click upon  a 1 second hover at a certain position

KinectWPFDemo: demonstrating how to create a media-player that uses postures to play, pause, fast-forward, rewind and alter the playback volume.

I’d love to hear your suggestions for practicle apps using the Kinect SDK!

Kinect SDK and Windows 8

Microsoft Kinect SDK is claimed to work under Windows 8.

Well, before you run-off starting to develop your next Kinect multi-touch experience for the Windows 8 UI, it is important that you know that there are limitations to that.

Although using the latest Kinect SDK driver, released October 2012, you can develop and run apps under the Windows 8 operating system, it only works for developing traditional desktop apps, meaning what you would call “Windows 7 apps” and not what is called: “Windows Store apps”, also formerly known as “Metro-style apps”.

The Kinect libraries are not compatible with Windows Store apps and therefore you cannot add a reference to a legacy library that works against the Kinect SDK. Visual Studio 2012 prompts you with an error message saying: “Unable to add reference”.

I’m currently working on a work-around that may enable a small portion of the Kinect features in Windows Store apps and will surely update when I come-up with something.

Latest Kinect SDK information and download to be found here: http://www.microsoft.com/en-us/kinectforwindows/develop/new.aspx

What’s new in Windows Azure?

In June 2012 Microsoft released a major update for Windows Azure, including IaaS (mounting virtual machines), websites on a shared infrastructure and a variety of frameworks, virtual networks, a new SDK, and support for many new languages including Java, Python, PHP and JavaScript.

This release puts Microsoft’s flagship cloud offering in great competition with Amazon in the IaaS market and with Google App Engine in the website market.

On the coming Sela Dev Academy I will be training a whole day tutorial in which I will present the new Azure portal and demonstrate the new features in the SDK.

Are you already using Azure? Are you developing against the SDK?

If you’re using Amazon or Google’s platforms you might want to hear this one out.

From Windows 8 to the Store

So, you’ve created an amazing new app for the Windows 8.

Everybody loves it and tell you that you should make it public and that people should pay you to use it.

But, how do you do that? It sounds complicated, isn’t it so?

It sounds like something companies does, not private people or small groups!

Well, it’s not that complicated and surely everybody can do that. It just takes some learning and a few correct steps and you’re there, at the app store, with your brand new app waiting to be downloaded and purchased!

So, to help you jump the hedge, I’ll be giving a lecture @devacademytoronto titled: “From a New Windows 8 Project to the Store“, in which I’ll show the list of actions to be taken in order to have an app in the store, explain the  methods and show the pitfalls to avoid along the way.

See you there!

Follow

Get every new post delivered to your Inbox.

Join 69 other followers