The procedure for creating a service follows the same basic outline regardless of whether your service is an initiating service, a target service, or both.
The definition of a service specifies the contracts for which the service can be a target. In contrast, an application can use a service to initiate a conversation that uses any contract defined in the database. Service Broker takes this approach to enforce the general rule that a service should only receive messages that the application can process. To ensure that the application does not receive messages of an arbitrary or unknown type, Service Broker accepts a new dialog only if the dialog follows a contract specified in the service. An initiating service specifies the contract to use when the conversation begins, so an initiating service does not need to include the contract in the service definition.
To create the objects for a service, do the following:
-
Create message types that define the messages your service will use to accomplish any required communication. You can define these types yourself or obtain scripts to create the types from the creator of the service with which your service will communicate. You skip this step when the database already contains the message types that your service needs.
-
Create one or more contracts that define the structure of the conversations in which this service may participate. You can define this contract yourself or obtain scripts to create the contract from the creator of the service that your service will communicate with. You skip this step when the database already contains the contracts that your service needs.
-
Create a queue. Service Broker uses this queue to receive and store incoming messages for the service. For more information on creating queues, see CREATE QUEUE. Every service must have a queue. To make programming and administration more straightforward, each service generally uses a queue dedicated to that service. If your service requires message retention, specify message retention for the queue.
-
Create a service. The service definition specifies the queue that the service uses and the contracts for which this service is the target.
In most cases, you create the target service and then use the contracts and message types created for the target service to create an initiating service. In some cases, however, you may create a target service for an initiating service that is already defined. In these cases, the target service uses the message types and contracts that the initiating service uses. For example, if you are creating a target service to receive event notifications, you use the contract http://schemas.microsoft.com/SQL/Notifications/PostEventNotification, because this is the contract that the initiating service uses.