And here is the associated contract that handles are inventory workflow (very rudimentary):
\
Instead we must DROP and reCREATE the CONTRACT. Something like this:
Note that we have changed the contract such that the INITIATOR is the only party that can post messages of type CancelInventoryItemRequest.
Why is there no ALTER CONTRACT?
In the real world you don't alter a legal contract. You generally make a new version with a new set of signatures. Certainly there are exceptions but contracts aren't meant to be broken. It's no different in the Service Broker world. CONTRACTs are not meant to be broken. Contracts are immutable. However, situations such as my inventory example are commonplace. When requirements, and hence CONTRACTs, must change BEFORE the system is released into the wild it is certainly reasonable to simply DROP/CREATE the contract like the above screenshot.
However, this is frowned upon once other systems begin to send messages to your service using that contract. Altering the contract could break other systems that are relying on its functionality. Since there may be a lot of moving parts and different systems with different release schedules you can't guarantee that everyone will be able to alter their functionality due to the change in your contract. Instead you should consider versioning your contracts and supporting multiple versions of a contract for a certain amount of time. A simple naming convention like CancelInventoryItemRequest_v1 is usually sufficient.
So if we should version our CONTRACTs, why not our SERVICEs...or...why is there an ALTER SERVICE command?
Versioning your SERVICEs is probably a best practice as well, but the fact is SQL Server does allow us an ALTER SERVICE command. Why?
Summary
So based on how Microsoft architected services, contracts, and message types you can see that serious thought was given to versioning the critical objects to help you ensure no service disruptions when there are changes to your design. If you understand why MS has given you these options you start to understand better how Service Broker really works.
You have just read "Service Broker Demystified - Why is there no ALTER CONTRACT statement?" on davewentzel.com. If you found this useful please feel free to subscribe to the RSS feed.
Dave Wentzel CONTENT
sql server service broker service broker demystified