Service Broker programs may be written as stand-alone applications that establish a connection with SQL Server or as stored procs that are activated internally. You can leverage the .NET platform in both scenarios since SQL Server 2005 integrates CLR and provides the ability of writing stored procs in CLR-based languages like C# or Visual Basic.

In order to help you to write Service Broker applications on the .NET platform, the Service Broker team has provided a class library called Microsoft.SqlServer.Broker as a sample. The library provides an object model for abstracting Service Broker concepts as a layer on top of ADO.NET. It also provides a framework for writing common event-driven service programs. Using the library, your code can look a lot like a Web Service program. Here's an example snippet:

public class HelloService : Service

{

    [BrokerMethod("HelloRequest")]

    public void ExecuteHello(Message msgIn)

    {

        MemoryStream body = new MemoryStream

            (Encoding.Unicode.GetBytes("Hello!"));

        Message msgOut = new Message("HelloResponse", body);

        Conversation conversation = msgIn.Conversation;

        conversation.Send(msgOut);

    }

 

    [BrokerMethod(Message.EndDialogType)]

    public void EndConversation(Message msgIn)

    {

       Conversation conversation = msgIn.Conversation;

       conversation.End();

    }

    ...   

}

You can obtain the library here.