Silverlight Hack

Silverlight & related .NET technologies

About Me

Welcome to Silverlighthack.com.  This is a site where you can find many articles on Silverlight, Windows Phone 7 and .NET related technologies.  

My name is Bart Czernicki.  I have been working with computers since 1988 and have over 12 professional years in the IT field focusing on architecture, technology strategy and product management.  I currently work as a Sr. Software Architect at a large software development company.

Below is the cover of my new book that shows how Silverlight's unique RIA features can be applied to create next-generation business intelligence (BI 2.0) applications.

Silverlight 4 Business Intelligence Soft 

Contact: bartczernicki@gmail.com

View Bart Czernickis profile on LinkedIn

NONE of the comments or opinions expressed here should be considered ofmy past or current employer(s).  The code provided is as-is without anyguarantees or warranties.

Solution Composition in Windows Azure Cloud Service Project Template

Goal: This blog post aims to show you the new solution composition feature in the "Windows Azure Cloud Service" project type.  In addition, I posted some additional thoughts on how this could be expanded as a very useful starting point for all projects.

The new "Cloud Service" project template includes a new feature (This is my name until Microsoft officially names it) "solution composition".  In Visual Studio 2010 Beta 2 (if you install the Windows Azure SDK), you will see a new Cloud Service template.  Through the screen shots below, I am going to highlight some of the key features of "solution composition". 

Cloud Service (Azure) Project Type



Selecting the Cloud Service template brings up an additional dialog box that allows you to add a number of additional project types.  Currently VB.NET, C# and F# web role and worker role projects are supported.  Selecting the project and selecting the arrow button adds this project to the Cloud Service solution.



If you need several projects of the same type, you can add multiples of the same type.  Notice the screen shot below illustrates a solution with three seperate C# worker role projects (assemblies).




Once added, project names can be renamed or removed.  Red arrows below highlight the controls that allow editing of a project.




After clicking the OK button, a complete Cloud Service solution is created with all the projects properly added to the roles folder.




Why Does This Matter?

If you have architected large-scale n-layer prototypes or projects, you can clearly see the benefit of "solution composition".  This allows you to select the projects you want ahead of time and "compose" a single solution.  For example, imagine you want to create a Silverlight application with a WCF service.  You might start with a Silverlight ASP.NET project type.  However, then you need to manually add all the projects yourself and properly add the references between them in Visual Studio (i.e., WCF service project, BLL assembly, DAL assembly, LINQ to SQL dbml, Silverlight BLL, etc.).  It would be much better to simply select the projects that will compose your solution and have Visual Studio generate the whole solution. 

 

Solution Composition Features I Would Like to See

The Cloud Service template in Visual Studio 2010 Beta 2 is a step in the right direction; however, it can be greatly improved.  These are some of the items I would like to see:

  • Make "solution composition" available for all Visual Studio 2010 templates.
  • Allow the architect/developer to build a dependency/reference tree between projects.
  • Integrate the solution folders feature (Allows you to logically organize the solution).
  • Allow the architect/developer to add specific functionality to a project.
    •  For example, if I want to have a DAL that uses LINQ to SQL, the ability to "checkbox off" and add a LINQ to SQL class
  • Allow the architect/developer to specify the assembly references for each project (.NET, custom toolkit, etc.).
  • Include a "smart namespace naming" feature.  Assemblies are named namespace+project name.
  • Ability to save the entire "solution composition" as a template itself for future use
    • Integrate with TFS and replicate to all developers (?)
    • Add diagraming feature

How Useful is This?

After reading this, you might be thinking that this is overkill and how often are you creating "large solutions".  If you have prototyped a lot of solutions, this will be a Godsend.  Furthermore, a lot of consulting shops that have to quickly generate client-specific prototypes would greatly benefit from this feature  In my opinion, the more tedious (plumbing) tasks that are automated, the more I can focus on architecture or design of software solutions.

kick it on DotNetKicks.com
Posted: Nov 15 2009, 16:27 by Bart Czernicki | Comments (0) RSS comment feed |
  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Filed under: Azure | Visual Studio 2010
Tags:
Social Bookmarks: E-mail | Kick it! | DZone it! | del.icio.us

Silverlight 3 and F# Support in Visual Studio 2010

The goal of this blog post is to make you aware of F# support in Silverlight in Visual Studio 2010.  In addition, this blog post shows an example why F# is going to be very important for Silverlight architects and developers.  Note:  This is NOT an intro to F#.

Demo and Visual Studio 2010 source code are provided with this article.

In Visual Studio 2010, F# is a first-class language.  Over the last several months, the language has been "productized."  It is officially part of the Visual Studio 2010 ecosystem (tools and .NET 4.0 framework).  However, F# is not limited to the full .NET framework; it is also available in the Silverlight runtime.  This is really exciting as this allows you to take advantage of functional programming features in Silverlight.

 

In Visual Studio 2010, the "F# Silverlight Library" project is natively supported.

 

Silverlight 3 application that includes an F# assembly.

  

Why should I care about F# in Silverlight?

Most C# developers/architects who do not have experience with functional programming language features could assume that F# is another language like Visual Basic, J# or C++.  It would be wrong to compare F# to any of these languages (including C#).  As a functional language, F# allows developers to design software using the functional paradigm.  What does that mean?  This means functional patterns such as interactive programming, pattern matching, immutable data types, type inference, lambda expressions, workflows, higher-order functions, discriminated unions, etc., are all included in F#.  While taking advantage of these features, a developer using F# can use the type system, implement object-oriented design and make use of the .NET classes.  This makes F# a very unique language that combines all the features of a first-class .NET language (i.e., C#) and a professional functional language.

Note: F# compiles to IL just like any other .NET language.  However, F# has been designed from the ground up over the last couple of years.  Therefore, the IL it generates has been optimized and many new language optimization features have been implemented.  For example, you may have seen blog posts that compare C# and F# syntax that shows F# compiling into cleaner IL and faster code.  Conversely, C#'s core functionality has not been redesigned in about a decade (C#/.NET has been extended with functional features such as type inference, extension methods, lambdas, AST, LINQ, etc.)

 

Show me some code that proves how F# can add value to Silverlight

This short blog post is not meant to be an F# introduction (Please see links below for intro material).  However, I did want to pick at least one compelling feature that really shows off the power and functional brevity of F# married with Silverlight functionality.  The feature that I decided to show off in a Silverlight F# module is a concurrent programming pattern.

If you do not already know, Silverlight supports distributing logic up to eight processor cores (This frustrates me that Microsoft doesn't adverstise this enough). In my opinion, concurrent programming is THE biggest feature of Silverlight over other RIA technologies (i.e., Flash, JavaFX).  However, implementing asynchronous patterns in Silverlight is not trivial.  The callback pattern is messy and leads to a lot of code.  Silverlight runs in an STA mode and requires UI elements to only be modified by the main thread (dispatching or synchronization).  Even asynchronous constructs like the BackgroundWorker that are supposed to make concurrent programming simple lead to many messy lines of code with events.  Wouldn't it be great to get the benefit of asynchronous programming without having to write many lines of additional code?

One of the fantastic features of F# is that the default type immutability and functional design allows the language to express the desired result declaratively.  This means you write F# syntax by telling the program what you want it to do rather than how to do it.  For example, if you are using constructs that use secondary threads and allocate code to these threads, then you are writing imperative code.  A lot of lines of code are wasted telling the code how we want it to behave.  Wouldn't it be better to define pieces of logic and simply say that we want them to execute in parallel?  Functional declarative code in F# can do exactly that.  F# includes a feature called asynchronous workflows that allow you to write logic by composition with the aim to be executed in parallel.

Initial Steps

  • I created a simple Silverlight application.
  • I added an "F# Silverlight Library" project
  • In the F# assembly, I added an F# code file called ConcurrencyTest.fs.
  • I created an F# function called addNumbers that takes two parameter:
    • The first parameter is an accumulator that is the existing sum in the running sequence (This will be passed by the reduce sequence function).
    • The second parameter is the number in the sequence we want to add.
  • I created an F# function called squareNumber that takes two parameters:
    • The first parameter is an accumulator that is the existing square sum in the running sequence (This will be passed by the reduce sequence function).
    • The second parameter is the number in the sequence we want to square.
  • I created an F# function (equivelant to a C# method) that takes no input parameters named PerformTwoLongRunningTasks:
    • Inside that function, I defined two tasks that use the addNumbers and squareNumber functions to add the numbers across a sequence of numbers from 0 to 10,000,000
    • The PerformTwoLongRunningTasks function returns an array of 64-bit integers.  The array of 64-bit integers are the results of the two aggregates functions.
    • The first function returns the sum of the numbers.  The second function returns the square of the numbers.

The F# static method/function is shown below (Some familiarity with F# is required to understand what is going on, but I commented on the code for those unfamiliar with F#):


Now that we have this function (static method) defined in our F# library, it is ready to be used.  In the Silverlight application, we can add a reference to the F# library and then we can call the function (static method) as shown below in our C# Silverlight MainPage class.  Note that below we are calling the function just like we would call a static method with the [Name of Class].[Name of public function].  Notice also that because F# uses the base .NET type system, the returned array of integers does not need special casting/marshalling to C#.

Parallizing the Tasks

Now it is time to parallize the two tasks and scale it across multiple logical/physical processor cores.  As mentioned earlier, we are going to accomplish this with asynchronous workflows. In the figure below, I highlighted the changes to the function and it now scales across multiple cores.  This was done in two steps:

  • Wrap the body of the member functions with the async {... } syntax.
    • Add a return keyword (This signifies the return parameter/completion of the body).
    • The body of the function Seq.reduce addNumbers {0L..10000000L} now becomes async { return Seq.reduce addNumbers {0L..10000000L} }.
  • The main function (PerformTwoLongRunningTasks) now returns an array of async tasks.  These can be thought of delegates that need an Invoke-type command to execute them.  We simply change let results = [| task1; task2 |] to let results = Async.RunSynchronously (Async.Parallel [ task1; task2 ]).
    • All this does is tells the F# compiler to parallize these tasks.  However, run the result synchronously.  Therefore, the code returns to the main thread and we do not need dispatching/synchornization, etc.  This is analogous to creating a wait event in C# and having the two pieces of logic scheduled on secondary threads and waiting for this process to come back.


Asynchronous workflows allowed us to simply wrap the tasks as an "async function" declaratively.  This is a very important distinction as we just declared the logic and told it we want this to run in parallel.  Notice we didn't tell it "how to run in parallel" (imperative code).  Therefore, we did not have to:

  • explicitly start, stop threads (i.e., Thread.Start)
  • use BackgroundWorker
  • use callbacks (AsyncCallBack)
  • use BeginExecute asynchronous pattern

We simply told the code we want the tasks distributed among multiple cores and let the F# compiler figure out the hard part and generate the IL.  This is really powerful and in the very near future of "declarative parallel processing".  This is an example of what I mentioned earlier that the F# compiler has been designed recently and can include this type of "magic" right in the language.  C# does not have this feature and needs to be extended to provide this kind of automation.


Can't I do this with PLINQ?

Those familiar with the Parallel Extensions Libraries that will be added to .NET 4.0 might be aware that LINQ has been extended with parallelism features.  This allows you to extend your existing LINQ queries with parallism features.  This is known as PLINQ (parallel LINQ).  For example, a screen shot below shows a LINQ query that can be easily parallized by simply adding the AsParallel() extension method.

Unfortunately, the Parallel Extensions Library will NOT be available for Silverlight for the Visual Studio 2010 release.  However, I think that there is a good chance future versions of Silverlight will eventually get this feature.  There are some big features in the Silverlight assemblies that are simply missing and need to be added before features like PLINQ can be added.  This is exactly where asynchronous workflows can be substituted for PLINQ in a Silverlight application.  If you are working with large data sets, complex math computations, AI for a game, etc., parallelizing your code in F# libraries makes pefect sense for a Silverlight application.  This is MUCH easier than writing your own imperative multithreaded code.

How about the Performance?

I extended the functions and created functions for two, four and eight tasks and put this in a Silverlight application.  On my workstation (four cores), there was about a 50% improvement by parallelizing the two tasks.  With four tasks, the improvement in performance was about 3.5x.  As you can see, asynchronous workflows can easily dramatically improve the performance of your code on the Silverlight client.

Click the picture below to launch the demo application

 

Getting started with F#

Information on F# has been around for several years.  There are many articles, white papers, and books on F#.  However, you do have to be careful and get content that is recent and relative to the current F# release.  The F# specification has changed dramatically over the last several months as the language was being "productized" and many methods/functions simply don't exist in F# anymore.   Below are some links I put together where to get started.

Videos

  • Getting Started with F# - This video (from the 2008 PDC) is the place to start if you have zero F# experience and/or if you want a really good introduction into functional programming and some F# examples.

Books (I have read four different F# books and the two below are by far the best ones and most current.  Don Syme's F# book is good but a little outdated.)

Websites

  • http://www.fsharp.net/- This the main Microsoft F# page.  Includes numerours links, downloads for Visual Studio, F# extensions, etc.
  • Don Syme's blog - Don Syme is one of the main "creators" of F#.  His blog is a must read to get insight into advanced topics and upcoming F# news.

 

I hope this post gets Silverlight architects/developers exited about upcoming F# support.  F# is another tool that software architects can use that helps them create better software.  Furthermore, F# suport is another example of Microsoft's ability to integrate the Silverlight runtime across the .NET development stack.

kick it on DotNetKicks.com
Posted: Nov 04 2009, 16:16 by Bart Czernicki | Comments (0) RSS comment feed |
  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:
Social Bookmarks: E-mail | Kick it! | DZone it! | del.icio.us

Visual Studio 2010 - How It Should Have Looked

A few years ago (before Twitter, StackOverflow, DotNetKicks), one of my great sources of info was CodeProject.com.  I remember some of the great articles from that time.  When Visual Studio 2010 came out, it reminded me of something I saw over 4 years ago on that site a concept of the Visual Studio 2010 IDE.

If you have installed Visual Studio 2010, you can note it has a new look and feel.  It has a new skin/theme.  It resembles an Expression Blend feel.  The shell of the IDE is using WPF technology.  It pretty much looks like the screen shot below:

As mentioned above, a while ago I came across an article on CodeProject that really had a great set of ideas.  I really hoped that Microsoft would adopt some of these concepts.  Take a look at some of the screens below:

Visual stack trace idea is really neat and the concept UI looks a lot "cleaner" and softer on they eyes than Microsoft's version

Concept IDE that shows error trapping

 

You can check out the full article with all of the author's ideas on CodeProject here (http://www.codeproject.com/KB/cs/concept_ide.aspx). It is a shame that Microsoft's version hasn't really changed all that much since Visual Studio 2003. Obviously, a lot of these concepts are provided in different ways and with other add-ins.  In fact, I think with the new WPF shell we might even see these new concepts come to life.  However, you can see that over 4 years ago the concept IDE was pretty advanced and even holds up by today's standards of modern UIs.

kick it on DotNetKicks.com
Posted: May 26 2009, 14:38 by Bart Czernicki | Comments (3) RSS comment feed |
  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:
Social Bookmarks: E-mail | Kick it! | DZone it! | del.icio.us

Windows Server 2008 Web Edition - Ready For Primetime

Summary

Windows Server 2008 Web Edition is not the Web Edition we knew from theSever 2003 platform. Microsoft has removed the big limitation ofinstalling some enterprise applications on the OS (i.e., SQL Server).Furthermore, IIS 7.0 is now the core web server and it can take advantage ofhardware specs similar to Standard Edition. With these changes andmore, Windows Server 2008 Web Edition is now a viable/affordableupgrade path from Windows Server 2003 Standard.

 

Are you getting ready for all the good stuff Visual Studio 2010 will bring?  Visual Studio 2010 will bring a lot of great features for ASP.NET enterprise services and tons of great features.  Some key features like ASP.NET, ASP.NET MVC, Silverlight 2/3, WCF/WF 4.0 will all run faster and better on the IIS 7.0 platform because of its key architecture features.  Platforms like Silverlight 2/3 or ASP.NET MVC will be easier to deploy on the IIS 7.0 web server.  Lastly, some features of WCF 4.0/WF 4.0 will require IIS 7.0 to utilize all of their respective features (WAS in IIS 7.0).

IIS 7.0 is not a decoupled application you can install on any OS.  Currently the IIS web server is coupled to the Vista/Windows 7 workstation OSes and the Windows 2008 server OS.  Therefore, if you want to use IIS 7.0 in production, you need the Windows Server 2008 Operating System.  I have a couple of projects coming up that I am developing and wanted to utilize some of IIS 7.0's features.  Furthermore, having the projects ready to take advantage of future .NET enhancements is another added bonus.  I decided to upgrade my server with the new OS.

I have a dedicated server that I lease through softlayer.com and I looked at my upgrade options.  I was currently running Windows 2003 Standard so upgrading to Windows 2008 Standard made perfect sense.  As part of my hosting package, I was not being charged extra for my OS; however, upgrading to Windows 2008 Standard was an extra $70/month.  That is over $800/year just to upgrade to the "equivalent" version of Windows 2008 Server.  I did notice that Windows 2008 Web Edition was being offered at the same rate as my current OS.  However, I was really apprehensive about upgrading/downgrading to a "web edition" of an OS. 

Windows 2003 Server Web Edition is really limited in what it was licensed to do.  Essentially it limited you to using the server as a web server.  By not allowing you to run any of the server services, it prevented you from installing enterprise applications and limited the hardware specifications.  The biggest limitation was the inability to install SQL Server.  If you ran Windows 2003 Server Web Edition, you were limited to SQL Server Express (unless you wanted to install an open source alternative or hack the OS).  I think Microsoft dropped the ball big time, as any web applications worth its weight requires some kind of database backend.  Most web sites (even ones that get a decent amount of traffic: top 20,000 worldwide) can be run from a single server.  Furthermore, any web application worth its weight usually is dynamic and has some kind of backend database.  If Windows 2008 Server Web Edition were the same, then this was obviously not an option for me.

Windows 2008 Server Web Edition is different than its 2003 counterpart.  While the 2008 Web Edition still had the hardware/service limitations, the OS did eliminate the big limitation of not being able to install SQL Server on the OS (SQL Server Enterprise editions still do require a higher version of Windows 2008 Server).  The Windows 2008 Server Web Edition does support a decent amount of RAM (32 Gig on x64 hardware) and allows you to run on a max of 4 processors (which is definitely a beefy server) which is equivalent to the Windows 2008 Server Standard Edition.  Therefore, Windows 2008 Server Web Edition is actually a pretty good equivalent to Windows 2003 Server Standard as a web server.

Why upgrade to Windows Server 2008

There are a lot of neat features in Windows Server 2008; however, IIS 7.0 for me is the biggest.  I mentioned performance, Silverlight and ASP.NET as my big reasons as to why I decided to upgrade.  Let's look at it a little deeper.

What makes IIS 7.0 great (my main reasons for going to it.  I know there are several others not mentioned below.):

  • Performance:
    • Modular Web Server Architecture allows you to turn features off and on like components.  Apache has allowed you to do this for a while now.  IIS has finally caught up and this improves performance as you do not need to install/run services that you do not use.
    • Easy Dynamic Caching & Compression.  This is finally built into IIS and allows you to quickly configure these settings.  IIS 6.0 was a pain in the butt to do this.  There are third party add-ons that do a very good job at this; however, it's free in IIS 7.0.
  • Windows Process Activation Services (WPAS or WAS)
    • Before IIS 7.0 if you wanted to host your Windows Communication Foundation (WCF) services, you had two choices: http or self-hosting.  Both choices have their pros and cons.  One coupled you to http via IIS and the other you were responsible for writing your own hosting module.  With WAS you can now host your WCF services and they can actually activate/deactivate themselves via listeners.  For example, imagine having a web site that is not turned on, taking any hardware resources and is ONLY activated when a request is made.  Most WCF experts recommend WAS hosting because of its many features.
  • Smooth Streaming
    • If you want to deliver rich media experiences in Silverlight like nbcolympics.com, you will need this feature.  This allows you to stream video content and it automatically throttles the quality of the stream if the network latency/connection is poor on the client's end.  This is very cool as even stable Internet connections can slow down for a few seconds.  You may receive a few frames at lower quality; however, your media will continue to play.  This is especially important for larger streams where the buffering is determined at the initial connection.
  • Extensability
    • Writing custom handlers/modules and deploying them to IIS has never been easier.  Several months ago, I showed how one could write an http handler for a dynamic clientaccesspolicy.xml file rather than placing an XML file on root of the web server and it is a lot simpler in IIS 7.0 than IIS 6.0.
    • Microsoft has a download page where the community has made several extensions that you can drop into IIS 7.0.  This of course was available in IIS 6.0; however, the management and deployment of this wasn't 100% there.
  • Ease of use for current and future applications (These are minor but make my life easier.)
    • Silverlight 2 hosting does not require additional MIME types (like it does in IIS 6.0)
    • ASP.NET MVC does not require wildcard mapping (like it does in IIS 6.0)
    • Want to create a clean REST service URLs (removing the .svc)?  Just as in ASP.NET MVC; there is no wildcard mapping required.

After doing the research over the last couple of days, I decided to bite the bullet and upgrade the OS to Windows 2008 Server Web Edition on Sunday (lowest bandwidth for my websites).  I installed SQL Server 2005 Standard, restored my databases and upgraded my web sites to IIS 7.0 and everything worked out great.  Having upgraded to Windows 2008 Server, I am really looking forward to using the IIS 7.0 features for my future projects and I will be ready for everything that .NET 4.0 and Visual Studio 2010 has to offer.

kick it on DotNetKicks.com
Posted: Mar 01 2009, 16:47 by Bart Czernicki | Comments (0) RSS comment feed |
  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Filed under: Visual Studio 2010
Tags:
Social Bookmarks: E-mail | Kick it! | DZone it! | del.icio.us