Send Messages from TopicClient to WCF Subscription Service

How to send Message to ServiceBus topic using TopicClient and receive it with WCF as Subscription Service that acts as a Subscription client, without the use of SubscriptionClient classes of Service Bus?

Relevant Facts and Documentaion:


Since the sender and receiver are based on two different technologies we have to make sure the encoding on both sides match and a WCF data contract is defined. Please follow the steps below. Also, here's a good blog where Abhishek (from Service Bus product group) talks about different ways of formatting the content for Service Bus Messages -

  • Define the data contract

   static class Constants


        public const string ContractNamespace = "urn:wcsubscriptionservice";



    [DataContract(Namespace = Constants.ContractNamespace)]

    public class TestMessage



        public string MsgNumber { get; set; }


        public string MsgContent { get; set; }



  •  Format the brokered message when sending from client.

    BrokeredMessage message = new BrokeredMessage(new TestMessage() { MsgNumber = 1, MsgContent = "Test Message" }, new DataContractSerializer(typeof(TestMessage)));

                     // Send message to the topic


  • Set the ListenURI in WCF Service:


private static Uri serviceBusEndpointAddress = new Uri("sb://<namespace><topic>");

        private static Uri subscriptionUri = new




           var endpoint = new ServiceEndpoint(contract, binding, new


            endpoint.ListenUri = subscriptionUri;


            endpoint.Name = "ReceiveMessage";


  • Manually Control the Receive Context, if you would like the user code to control the incoming message for any further processing purposes.


    public interface IServiceBusReader


        [OperationContract(IsOneWay = true, Action = "*"), ReceiveContextEnabled(ManualControl = true)]

        void ReceiveMessage(TestMessageContract someMsg);



    [ServiceBehavior(AddressFilterMode = AddressFilterMode.Any)]

    public class ServiceBusReader : IServiceBusReader


        public void ReceiveMessage(TestMessageContract someMsg)


            TestMessage msg = someMsg.TestMessage;

            var incomingProperties = OperationContext.Current.IncomingMessageProperties;

            var property = incomingProperties[BrokeredMessageProperty.Name] as BrokeredMessageProperty;

            ReceiveContext receiveContext;

            if (ReceiveContext.TryGet(incomingProperties, out receiveContext))






                throw new InvalidOperationException("...");





I've attached the sample code, please feel free to use it. Just make sure, you input the values for service bus namespace, key, topic name and subscription name.

Files: TopicSender.cs, WCFSubscriptionService.cs

Comments (3)
  1. Matt says:

    Thanks for this great article!  I'm actually trying to do the opposite…  I want to use a MessageReciever to get dead-letter messages from a topic subscription that were originally sent to the topic via WCF using the NetMessageBinding.

    I'm receiving the messages just fine, but am unable to deserialize the body.  I think what I'm missing is they type of serializer that is used by the NetMessageBinding.  Any idea how to do this?


  2. Thanks for your feedback.

    Application using WCF and NetMessagingBinding wraps the message in soap envelope and encodes it.…/hh532034.aspx

    Have you tried something like?

    receiveMessage.GetBody<T>(new DataContractSerializer(typeof(T)))

    Please refer to Abhishek's blog for more information on formatting of brokered messages.…/formatting-the-content-for-service-bus-messages

  3. Mist says:

    Hey I am tried using your code and service starts running but no messages on subscriber side. I am using this code on Windows Service Bus. Can you please help if I need to do some more configurations

Comments are closed.

Skip to main content