Rants, rambles, news and notes from another geek

Fun With WSE 2.0

My boss has a demo coming up where he wanted to have a little private wireless network (not Internet connected) and he wanted to be able to send IM-like messages to all of the machines. He wanted the little “piece of toast” popup and to be able to send an arbitrary piece of text and a hyperlink.

Also, I’ve had a couple of WSE2 articles and MSDN TV downloads in my “To Be Reviewed” folder for a while now… why not use WSE2 to build this IM thingy?

I decided to use the WSE 2.0 Messaging API, which is very easy to use. You can literally send a message in less than 10 lines…

private void sendButton_Click(object sender, System.EventArgs e)
{
    Uri destination = new Uri("soap.tcp://" +
        machineNameTextBox.Text + ":8888/WseMessenger");
    SoapSender soapSender = new SoapSender(destination);
    SoapEnvelope envelope = new SoapEnvelope();
    envelope.Context.Action = "Receiver";
    XmlElement body = envelope.CreateBody();
    body.InnerText = messageTextBox.Text;
    soapSender.Send(envelope);
}

Pretty slick eh? Receiving is a little more complicated only in that you need to derive a new class from SoapReceiver:

using Microsoft.Web.Services;
using Microsoft.Web.Services.Messaging;
using System;
public delegate void MessageReceivedEvent( object sender,   
                        SoapEnvelope envelope );
  
public class MyReceiver : SoapReceiver
{
    public event MessageReceivedEvent MessageReceived;
    protected override void Receive(SoapEnvelope envelope)
    {
        if( MessageReceived != null )
            MessageReceived( this, envelope );
    }
}

Then from my Receiver Form I just add MyReceiver to the SoapReceivers collection and subscribe to the event:

private void Form1_Load(object sender, System.EventArgs e)
{
    MyReceiver receiver = new MyReceiver();
    receiver.MessageReceived += 
        new MessageReceivedEvent(receiver_MessageReceived);
    SoapReceivers.Add( "soap.tcp://" + 
        System.Net.Dns.GetHostName() +
        ":8888/WseMessenger", receiver );
}

Easy as pie! So now the question is… when will WSE 2.0 ship?

PS. This was all moot of course. I came into the office this morning and we decided to go with Microsoft Office Live Communication Server instead. Since we were running on a private little demo network anyway, why not? It was still fun to play around with the Messaging API.

PPS. If you are looking for a “piece of toast” control for C#/.NET, check out TaskbarNotifier by John O’Byrne. It is fully skinnable and very easy to use.