Consuming WCF services in Silverlight using Async CTP

Here is a small sample of how you can consume WCF services using the new Async CTP features.

Example, filling a listbox with categories of some sort.

private async void FillCategories()
{
    var client = new MyServiceReference.MyServiceClient();
    //yield untill all categories have been fetched.
    var categories = await client.GetCategoriesTaskAsync();
    categoriesListBox.DataContext = categories;
}

..elsewhere..

//this extension makes it possible to get a Task of T back from our service client
public static class MyServiceClientExtensions
{
    public static Task<IList<Category>> 
                GetCategoriesTaskAsync(this MyServiceClient client)
    {
        var taskCompletion = new TaskCompletionSource<IList<Category>>();
        client.GetCategoriesCompleted += (s, e) =>
                {
                    if (e.Error != null)
                        taskCompletion.TrySetException(e.Error);
                    else
                        taskCompletion.TrySetResult(e.Result);
                };
        client.GetCategoriesAsync();

        return taskCompletion.Task;
    }  
}

Linq to SqlXml: Projections

I’ve managed to add projection support to my Linq to Sql Server Xml column implementation.

Executing this Linq query:

var query = (from order in ctx.GetCollection().AsQueryable()
                where order.OrderTotal > 100000000
                where order.ShippingDate == null
                where order.OrderDetails.Sum(d => d.Quantity * d.ItemPrice) > 10
                select new
                {
                    OrderTotal = order.OrderDetails.Sum(d => d.ItemPrice * d.Quantity),
                    CustomerId = order.CustomerId ,
                    Details = order.OrderDetails
                })
                .Take(5);

Will yeild this Sql + XQuery:

select top 5 Id,DocumentData.query(
'<object type="dynamic">
 <state>
  <OrderTotal type="decimal">
   {fn:sum( 
              for $A in /object[1]/state[1]/OrderDetails[1]/object/state[1] 
                      return ($A/ItemPrice[1] * $A/Quantity[1]))}  </OrderTotal>
  <CustomerId type="guid">
   {xs:string(/object[1]/state[1]/CustomerId[1])}
  </CustomerId>
  <Details type="collection">
   {/object[1]/state[1]/OrderDetails[1]/object}
  </Details>
 </state>
</object>') as DocumentData

from documents
where
CollectionName = 'Order'  and
(documentdata.exist('

/object/state[(fn:sum( 
        for $A in /object[1]/state[1]/OrderDetails[1]/object/state 
             return ($A/Quantity[1] * $A/ItemPrice[1])) > xs:decimal(10)) and

/object[1]/state[1]/ShippingDate[1][@type="null"] and
(/object[1]/state[1]/OrderTotal[1] > xs:decimal(100000000))]

')) = 1

Chest Reconstructuion – Nuss Procedure

This is a completely off topic post about my personal life.

I’m now back at home after one week at hospital due to chest reconstruction.
Since my teens I’ve got Pectus Excavatum, a chest deformity where the chest bone is deformed and gives the chest a concave appearance.
Pectus affects about 1 out of 1000 people.

In my case, the PE was not only cosmetic, it also gave me chest pains when deep breathing and also putting pressure on my heart.
When I was a teen, the only way to fix the problem was to cut the ribcage open and cut everything off and then rebuild a new chest.
That alternative wasn’t very appealing to me so I did not do anything about it at that point.

However, now some 20ish years later, I found out about the Nuss procedure, a procedure where you implant a steel bar into the chest, putting preassure on the chest bone and ribcage from the inside and thus forcing a correct shape.
The Nuss procedure have apparently been around for some 10 years, but I guess there are no newsletters for that kind of information ;-)

The reason why I write this post is because I found it very hard to find information regarding Nuss for adults, e.g. information about how long I should expect to be off from work, and what results to expect from the procedure.

So here are some facts.

Age: 34
Pectus: Mild/Medium
Procedure duration: 1.5 H
Time in hospital: 6 days
Pain after procedure:  Way less than expected, was up and walking the day after.

Current status: 9 days since procedure and I feel quite OK with some occasional pain spikes.
Putting on socks or getting in and out of the car is extremely painful.
And don’t even think about sneezing ;-)

Before images:

 

As can be seen on the “before images”, my chest bone was sunken into my chest by some 2-3cm.

After images one week post op:

 

These images are taken 9 days after the procedure.
The steel bar was inserted just below my man-boobs (where the tape is).

I will now have the bar for about 3 years before it is removed and hopefully the bones have been re-shaped by that time.

[Update]
After images, 4 months later

Changed my diet after the surgery, got rid of the worst parts of the pot belly.

X-Ray:

I guess I can’t get through airport security checks anymore…

Update
Two years later

RX Framework – Building a message bus

I’ve been toying around with the Reactive Extensions (RX) Framework for .NET 4 the last few days and I think I’ve found a quite nice usecase for it;
Since RX is all about sequences of events/messages, it does fit very well together with any sort of message bus or event broker.

Just check this out:

Our in proc message bus:

public class MiniVan
{
    private Subject<object> messageSubject = new Subject<object>();

    public void Send<T>(T message)
    {
        messageSubject.OnNext(message);
    }

    public IObservable<T> AsObservable<T>()
    {
        return this
            .messageSubject
            .Where(m => m is T)
            .Select(m => (T)m);
    }
}

Subscribing to messages:

bus.AsObservable<MyMessage>()
    .Do(m => Console.WriteLine(m))
    .Subscribe();

The nice thing about this is that you get automatic Linq support since it is built into RX.
So you can add message handlers that filters or transform messages.
Pretty slick isnt it?

I’m currenty writing an example IRC chat client based on this idea which I will publish in a week or two.

//Roger