Search code examples
sessionazureazureservicebusservicebus

Azure, SubscriptionClient.OnMessage, and Sessions


Does the Azure Service Bus Subscription client support the ability to use OnMessage Action when the subscription requires a session?

I have a subscription, called "TestSubscription". It requires a sessionId and contains multipart data that is tied together by a SessionId.

if (!namespaceManager.SubscriptionExists("TestTopic", "Export"))
{
    var testRule = new RuleDescription
    {
        Filter = new SqlFilter(@"(Action='Export')"),
        Name = "Export"
    };

var subDesc = new SubscriptionDescription("DataCollectionTopic", "Export")
{
    RequiresSession = true
};
namespaceManager.CreateSubscription(sub`enter code here`Desc, testRule);
}

In a seperate project, I have a Service Bus Monitor and WorkerRole, and in the Worker Role, I have a SubscriptionClient, called "testSubscriptionClient":

testSubscriptionClient = SubscriptionClient.CreateFromConnectionString(connectionString, _topicName, CloudConfigurationManager.GetSetting("testSubscription"), ReceiveMode.PeekLock);

I would then like to have OnMessage triggered when new items are placed in the service bus queue:

testSubscriptionClient.OnMessage(PersistData);

However I get the following message when I run the code:

InvalidOperationException: It is not possible for an entity that requires sessions to create a non-sessionful message receiver

I am using Azure SDK v2.8.

Is what I am looking to do possible? Are there specific settings that I need to make in my service bus monitor, subscription client, or elsewhere that would let me retrieve messages from the subscription in this manner. As a side note, this approach works perfectly in other cases that I have in which I am not using sessioned data.


Solution

  • Can you try this code:

    var messageSession=testSubscriptionClient.AcceptMessageSession();
    
    messageSession.OnMessage(PersistData);
    

    beside of this:

    testSubscriptionClient.OnMessage(PersistData);
    

    Edit: Also, you can register your handler to handle sessions (RegisterSessionHandler). It will fire your handle every new action.

    I think this is more suitable for your problem.

    He shows both way, in this article. It's for queue, but I think you can apply this to topic also.