Thanks everyone who attended my talk at the Silverlight User Group last night.  I really enjoyed myself and was glad to see a hearty discussion around RIA services – which is surely the point of these sorts of events; to get everyone thinking and get the dialog going.

You can download my presentations from here: http://bit.ly/SlugSep09IB 

Ria Services

 

If you want to find out more about Ria Services then Brad Adams is the person to go to – if you haven’t seen his blog series that followed on from his Mix09 series then check that out now; it consists of >20 posts covering everything from using a ViewModel,  azure, authentication, multiple clients and data sources, SEO, Unit testing and much more.  And it’s ongoing with promises of new posts on modular development (will be interesting to see how that relates to Prism).  He has a summary of the current posts here: http://blogs.msdn.com/brada/archive/2009/08/02/business-apps-example-for-silverlight-3-rtm-and-net-ria-services-july-update-summary.aspx.

SilverlightPulse.net

 

I have placed the app on http://silverlightpulse.codeplex.com/  – if you haven’t used codeplex before it is Microsoft’s Open source repository – you can use a range of clients such as svn, team system etc to work with the source.  I have published under the Microsoft Public License (Ms-PL) which is the most permissive Open Souce MS license as I understand it

Anyone who is interested in helping out please get in touch (Ian@bbits.co.uk) – it would be really great to take this app and make it a great showcase for members of the Silverlight User Group in the UK.  There are lots of things we could explore both functionally (eg wordle maps, integrating other network api’s such as friendfeed, using the silverlight bing map), and technically (using Prism for modularity seems like a great fit, having more server processing to aggregate data for all perhaps (instead of doing the work on the client), adopting MVVM,  are a few that come to mind.  I actually think this could develop into something really interesting and I am sure there will be many ideas from the community that could be used.  Get your thinking caps on and get in touch – please :-)

Cheers

Ian

 

I am chuffed to be talking at the user group meeting tonight, along with Ian Smith.

Details here

I will be talking about two things :

Silverlightpulse.net

This was the micro app I created before the Mix 09 conference in anticipation for the Silverlight 3 announcements and why I was originally invited to talk at the user group meeting (it’s a bit of a long story as to why it has taken so long for that to actually materialize – filled with stories of product launches and twisted ankles!). 

I still think this is a very interesting app though, and has some potential that I have yet to realize.  Tonight I will be going into why I think that and asking: “is this a worthwhile community project and who wants to be involved?”

Using RIA Services with Silverlight

To me RIA services offer a prescriptive pattern that will be useful for a majority of projects.  ScottGu called out 80% as the take-up figure when he talked about it recently and John Papa said in a recent tweet “I think RIA Services could be the best thing to happen to Silverlight LOB apps”.  I agree, though some may not entirely.  I only have an hour in total this evening but I hope to give a flavour of RIA services and what it can bring to the party.

Hope to see you there!

Cheers

Ian

Thanks for everyone who entered the competition for a free ticket for the TDD Masterclass with Roy Osherove, 21-25 September in Kent, UK.

We had lots of entries and all the names were printed out and put into a bowl and then the winner was picked out at random by my 6 year old daughter this evening:

DSC_6756

I am pleased to announce that the winner is : Sara Stephens!
http://developerdame.blogspot.com/2009/08/tdd-masterclass-in-uk-free-ticket.html

Congratulations to Sara!  Sara – I will send you a separate email with details of your free ticket ;-)   It promises to be a fantastic course!

Once again thank you everyone who supported this promotion.

If anyone would still like to come to the course there are still a few places left if you would like to come – please contact me directly if you are interested as soon as possible!

Cheers

Ian

bbits are giving away a free ticket for Roy Osherove’s TDD Masterclass in September in the uk.

To enter the raffle all you need to do is use the exact text between these lines in your blog post, including all the links, then email Ian@bbits.co.uk with the url to the blog entry.

The draw will be made on September 1st and the winner notified by email and via this blog.

Good Luck!

————– TEXT STARTS HERE

Roy Osherove is giving an hands-on TDD Masterclass in the UK, September 21-25. Roy is author of "The Art of Unit Testing" (http://www.artofunittesting.com/), a leading tdd & unit testing book; he maintains a blog at http://iserializable.com (which amoung other things has critiqued tests written by Microsoft for asp.net MVC – check out the testreviews category) and has recently been on the Scott Hanselman podcast (http://bit.ly/psgYO) where he educated Scott on best practices in Unit Testing techniques. For a further insight into Roy’s style, be sure to also check out Roy’s talk at the recent Norwegian Developer’s Conference (http://bit.ly/NuJVa). 

Full Details here: http://bbits.co.uk/tddmasterclass

bbits are holding a raffle for a free ticket for the event. To be eligible to win the ticket (worth £2395!) you MUST paste this text, including all links, into your blog and email Ian@bbits.co.uk with the url to the blog entry.  The draw will be made on September 1st and the winner informed by email and on bbits.co.uk/blog

————- TEXT ENDS

Cheers

Ian

bbits have produced 2 new Silverlight 3 courses; one targeted at designers, the other at developers building line of business applications.

Both are bang up to date, including Sketchflow and Blend 3 for designers and RIA Services, PRISM and M-V-VM for developers.

Take a look:

bbits courses can be customised at no extra cost, so if you can’t take the 5 days for the full developers course, you can cherry pick the bits you want and just do them!

Cheers

Ian

Technorati Tags: ,,

During a recent course with Stan James  we discussed how Silverlight 3 could be used with duplex services to push data from the server to the clients; I wanted to produce a simple example of how to do this.  Pushing data this way has a number of benefits:

  • You reduce the load on the server; instead of multiple clients polling the server periodically, we send the data from the server to the clients as and when we want to.
  • We can eliminate or reduce data lag in time sensitive applications – so as soon as we have the data available we can push it to Silverlight to react to.  This can be very important for applications that need to show data changes as they happen, such as trading systems or perhaps live statistics from sports events such as Formula 1.
  • All clients receive updates at the same time.

There is a nice sample application from Mix09 available here which creates a chat application as well as an example of a server push.  However I felt a simpler example would have a benefit to many trying to get to grips with this, together with a bit more explanation.  We will however use generic DuplexService class that is created in that project (it would make sense for a class like this to be available in the framework, or at least a template within Visual studio)

What our application will do

We want a simple example, and that is what we will get.  The server will periodically push a “LiveData” message to any Silverlight client that has connected, and the client will display it.  I am not concerned how the data gets to the server here, only how to send it out to the client once we have it. 

How the project fits together

image

Creating the Sample

Creating the Project

First create a new Silverlight project; let’s call it DuplexExample – ensure you include a Asp.Net Web Application with the default name (DuplexExample.Web) .  In the web site add a reference to the System.ServiceModel.PollingDuplex (make sure you get the correct one – the v3.0 one in the Server folder.)

image

And in the Silverlight project also add a reference to System.ServiceModel.PollingDuplex (but this time in the Client folder):

 image

Download the Duplex sample from http://code.msdn.microsoft.com/silverlightws/Release/ProjectReleases.aspx?ReleaseId=2401, extract it and grab the DuplexService.cs file and drop it into your web site.

Creating the Service

Now we could add a Wcf service using Visual Studio templates, but that would actually create a lot of stuff we don’t need which we would just have to delete, so instead just add a text file but call it LiveDataService.svc . This will give us a blank file into which you can add the following:

<%@ ServiceHost
       
Language=”C#”
       
Debug=”true”
       
Factory=”DuplexExample.Web.LiveDataServiceFactory”
       
Service=”DuplexExample.Web.LiveDataService”
        
%>

 

The only difference here to the contents of a normal svc file is the fact that there is no CodeBehind value specified and we have explicitly specified a Factory class.  (For more details on these options see here: http://msdn.microsoft.com/en-us/library/aa967286.aspx) Since we have specified a LiveDataService for our service and LiveDataServiceFactory class that will be used to instantiate the factory service, we will need to go and create these. 

But  before we can do that we need a DataContract class called LiveDataMessage – this is the simple class that our service will send to Silverlight.

[DataContract]
public class LiveDataMessage: DuplexMessage
{
    [DataMember]
    public int Value { get; set; }
    [DataMember]
    public string Description { get; set; }
}

Ok so now we can create our “LiveDataService” – add a class to your web site called LiveDataService.cs  and add the following code.  This is fairly simple – we are using a Timer to send a message to all clients every 5 seconds:
 

using System;
usingMicrosoft.Silverlight.Cdf.Samples.Duplex;
usingSystem.Threading;

namespace DuplexExample.Web
{
    public class LiveDataService :DuplexService
  
{
        Timer liveDataTimer;

        public LiveDataService()
        {
            //Set up a an update every 5 seconds
          
this.liveDataTimer = newTimer(newTimerCallback(LiveDataUpdate),null, 0, 5000);
        }

        void LiveDataUpdate(objecto)
        {
            LiveDataMessage liveDataMessage = newLiveDataMessage()
            {
                Description = “Live Data at “+ DateTime.Now.ToLongTimeString(),
                Value = newRandom().Next(0, 100)
            };
            PushToAllClients(liveDataMessage);
        }
    }
}

The DuplexService Class

You will notice that our LiveDataService inherits from the DuplexService we added to our project – this provides all the plumbing work for the duplex service:

image

Crucially this class is also marked with the following attribute, meaning our service can keep track of clients that have connected, because it stays in memory and is not recycled’:

image

Ok – so the DuplexService provides plenty of goodness for free, though there is one thing we need to alter in this file – you may have noticed already that it is not compiling.  Down at the bottom you will see a list of KnownTypes.  These related to the sample project you downloaded and not what we want.  We only have one specific KnownType (apart from the Connect and Disconnect) called LiveDataMessage. 

So you need to make the following change to the DuplexService.cs file (at the bottom) :

image

The LiveDataServiceFactory class

Finally we need to add the LiveDataServiceFactory class  – which inherits from the DataServiceFactory class in the DuplexService.cs file (and in fact that’s all it will do)

namespace DuplexExample.Web
{
    public class LiveDataServiceFactory : DuplexServiceFactory<LiveDataService> {}

}

If you go and look at the DuplexServiceFactory we are inheriting from (again in the DuplexService.cs file) you will see that it provides the configuration for the service with a custom binding (this is an alternative to providing configuration for doing this)

image

The Silverlight Project

Make sure you can build your solution, then add a Service Reference from the Silverlight project to the LiveDataService you have created:

image

I created a very simple UI – just a button and a ListBox shown below:

<Grid x:Name="LayoutRoot" Background="White">
    <Grid.RowDefinitions>
        <RowDefinition Height="75"/>
        <RowDefinition Height="225"/>
    </Grid.RowDefinitions>
    <Button Content="Connect"             Click="ButtonConnect_Click"             x:Name="ButtonConnect" Margin="10"></Button>
    <ListBox Grid.Row="1"             ScrollViewer.VerticalScrollBarVisibility="Visible"              x:Name="ListBox1"></ListBox>
</Grid>

image

Here is all the code in MainPage.Xaml.cs.  Take a look at it here then we’ll discuss it:

public partial class MainPage : UserControl
{
    DuplexServiceClient receiver;
    ObservableCollection<string> liveDataMessages =                                 new ObservableCollection<string>();
    CustomBinding binding = new CustomBinding(
                                new PollingDuplexBindingElement(),
                                new BinaryMessageEncodingBindingElement(),
                                new HttpTransportBindingElement()
                                );

    public MainPage()
    {
        InitializeComponent();

        ListBox1.ItemsSource = liveDataMessages;

        //make sure the endpoint address is correct
        receiver = new DuplexServiceClient(binding,                  new EndpointAddress("http://localhost:6719/LiveDataService.svc"));
        receiver.SendToClientReceived += (sender, e) =>
            {
                if (e.msg is LiveDataMessage)
                {
                    LiveDataMessage msg = (LiveDataMessage)e.msg;
                    liveDataMessages.Add(string.Format("{0}.                      Value = {1}", msg.Description, msg.Value));
                }
            };
    }

    bool connected = false;
    private void ButtonConnect_Click(object sender, RoutedEventArgs e)
    {
        if (connected)
        {
            Disconnect();
        }
        else
        {
            Connect();
        }
    }

    private void Connect()
    {
        ButtonConnect.Content = "Connecting...";
        receiver.SendToServiceCompleted += (sender, e) =>
        {
            ButtonConnect.Content = "Connected (click to disconnect)";
            connected = true;
        };
        receiver.SendToServiceAsync(new ConnectMessage());
    }

    private void Disconnect()
    {
        ButtonConnect.Content = "Disconnecting...";
        receiver.SendToServiceCompleted += (sender, e) =>
            {
                ButtonConnect.Content = "Disconnected (click to connect)";
                connected = false;
            };
        receiver.SendToServiceAsync(new DisconnectMessage());
    }

}

At the top of this code, we declare 3 variables: the DuplexServiceClient,a  CustomBinding (which matched the binding of the service we created on the server) and an ObservableCollection to store the messages we get from the server.  We instantiate the DuplexServiceClient in the class constructor using the custom binding and and endpoint (make sure this is the correct address for your service) and wire up the SendToClientReceived event which is where we add the messages we get from the server to the observableCollection (which in turn shows them in the ListBox)

However we also need to provide a Connect method (which sends a ConnectMessage to the server) – this is to essentially to let the Duplex service on the server know we are here, and allow it to keep track us via a Session ID.  You can see this in DuplexService.cs file as shown below (line 133):

image

The disconnect method does the reverse by sending a DisconnectMessage to the server which reacts like this (removing the client from it’s tracked session id’s)

image

Testing Your Applicationimage

Press F5 to run the application, then click the Connect  button  – you will see messages sent to Silverlight from the Server every 5 seconds.  To make it more interesting, start a second browser instance and run the same application and connect that too.  You will see both clients being updated at the exactly the same time.

 

Hope this is of use.

You can download the source code from here: http://cid-fb8b852ef1ab0b35.skydrive.live.com/self.aspx/SampleCode/DuplexExample.zip

Cheers

Ian

Technorati Tags: ,,

Silverlight for Business has been hosted over on Live Spaces for the last 6 months.  However there are quite a few issues with using that platform which don’t look like been fixed any time soon.  So for that reason I am moving it here.

All the old posts are still available with the original links, and I have summarised them here:

By the way, I have a stack of new posts on the way that I have been building up, so stay tuned.

Cheers

Ian