Search code examples
sql-serverservice-broker

Service broker - changing service to default contract causes error


In SQL Server 2016 a while ago, I created a service broker queue:

CREATE QUEUE [dbo].[NotificationsQueue] 
WITH STATUS=ON, 
RETENTION=OFF,
POISON_MESSAGE_HANDLING (STATUS=ON)
ON [PRIMARY]
GO

Also, I created a service:

CREATE SERVICE [ChangeNotifications]
AUTHORIZATION [dbo]
ON QUEUE [dbo].[NotificationsQueue]
(
[http://schemas.microsoft.com/SQL/Notifications/PostQueryNotification]
)
GO

This worked fine, allowing the sending out of XML messages as required from a bit of code that monitored the queue.

Our new requirement is that we don't want to be tied to XML anymore. So, as the system PostQueryNotification contract has xml validation in it I wanted to use the default that has no validation at all.

So I altered the service:

ALTER SERVICE ChangeNotifications (ADD CONTRACT [DEFAULT], DROP CONTRACT [http://schemas.microsoft.com/SQL/Notifications/PostQueryNotification])

Now, when I read the message_body field from the queue, I get

Target service 'ChangeNotifications' does not support contract 'http://schemas.microsoft.com/SQL/Notifications/PostQueryNotification'

So I'm now confused, as I've only done what a lot of blogs have told me to do when you want to change the validation.

What am I doing wrong?


Solution

  • Based on the names you're using, are you implementing Query Notifications? If so, I don't think you have control over what contract is being used. That is, any process trying to send a query notification will (at some point) issue something like the following:

    begin dialog @handle
      from service sourceService
      to service 'ChangeNotifications'
      on contract [http://schemas.microsoft.com/SQL/Notifications/PostQueryNotification]
    

    Because you've removed that contract from the service, that no longer works and errors out as it should.