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