title: SQL Server 2008 Service Broker tutorial -- cannot receive the message (exception in transmission_status)
body: <p>I am learning how to use the Service Broker of SQL Server 2008 R2. When following the tutorial <a href="http://msdn.microsoft.com/en-us/library/bb839495%28v=sql.105%29" rel="noreferrer">Completing a Conversation in a Single Database</a>. Following the <a href="http://msdn.microsoft.com/en-us/library/bb839488%28v=sql.105%29" rel="noreferrer">Lesson 1</a>, I have successfully created the message types, contract, the queues and services. Following the <a href="http://msdn.microsoft.com/en-us/library/bb839499%28v=sql.105%29" rel="noreferrer">Lesson 2</a>, I have probably sent the message. However, when trying to receive the message, I get the NULL for the <code>ReceivedRequestMsg</code> instead of the sent content.</p>
  
  <p>When looking at the <code>sys.transmission_queue</code>, the <code>transmission_status</code> for the message says:</p>
  
  <blockquote>
    <p>An exception occurred while enqueueing a message in the target queue. Error: 15517, State: 1. Cannot execute as the database principal because the principal "dbo" does not exist, this type of principal cannot be impersonated, or you do not have permission.</p>
  </blockquote>
  
  <p>I have installed SQL Server using the Windows login like <code>Mycomp\Petr</code>.  I am using that login also for the lessons.</p>
  
  <p>Can you guess what is the problem? What should I check and or set to make it working?</p>
  
  <p><strong>Edited 2012/07/16:</strong> For helping to reproduce the problem, here is what I did. Can you reproduce the error if you follow the next steps? </p>
  
  <p>Firstly, I am using Windows 7 Enterprise SP1, and Microsoft SQL Server 2008 R2, Developer Edition, 64-bit (ver. 10.50.2500.0, Root Directory located at C:\Program Files\Microsoft SQL Server\MSSQL10_50.SQL_PRIKRYL05\MSSQL). </p>
  
  <ol>
  <li><p>Following the tutorial advice, I have downloaded the AdventureWorks2008R2_Data.mdf sample database, and copied it into C:\Program Files\Microsoft SQL Server\MSSQL10_50.SQL_PRIKRYL05\MSSQL\DATA\AdventureWorks2008R2_Data.mdf</p></li>
  <li><p>The SQL Server Management Studio had to be launched "As Administrator" to be able to attach the data later. Then I connected the SQL Server.</p></li>
  <li><p>Right click on Databases, context menu Attach..., button Add..., pointed to AdventureWorks2008R2_Data.mdf + OK. Then selected the AdventureWorks2008R2_Log.ldf from the grid below (reported as Not found) and pressed the Remove... button. After pressing OK, the database was attached and the AdventureWorks2008R2_log.LDF was created automatically.</p></li>
  <li><p>The following queries were used for looking at "Service Broker enabled/disabled", and for enabling (the Service Broker was enabled successfully for the database):</p></li>
  </ol>
  
  <hr>
  
  <pre><code>USE master;
  GO
  
  SELECT name, is_broker_enabled FROM sys.databases;
  GO
  
  ALTER DATABASE AdventureWorks2008R2
        SET ENABLE_BROKER
        WITH ROLLBACK IMMEDIATE;
  GO
  
  SELECT name, is_broker_enabled FROM sys.databases;
  GO
  </code></pre>
  
  <ul>
  <li>Then, following the tutorial, the queries below were executed to create the message types, the contract, the queues, and the services:</li>
  </ul>
  
  <hr>
  
  <pre><code>USE AdventureWorks2008R2;
  GO
  
  CREATE MESSAGE TYPE
         [//AWDB/1DBSample/RequestMessage]
         VALIDATION = WELL_FORMED_XML;
  CREATE MESSAGE TYPE
         [//AWDB/1DBSample/ReplyMessage]
         VALIDATION = WELL_FORMED_XML;
  GO
  
  CREATE CONTRACT [//AWDB/1DBSample/SampleContract]
        ([//AWDB/1DBSample/RequestMessage]
         SENT BY INITIATOR,
         [//AWDB/1DBSample/ReplyMessage]
         SENT BY TARGET
        );
  GO
  
  CREATE QUEUE TargetQueue1DB;
  
  CREATE SERVICE
         [//AWDB/1DBSample/TargetService]
         ON QUEUE TargetQueue1DB
         ([//AWDB/1DBSample/SampleContract]);
  GO
  
  CREATE QUEUE InitiatorQueue1DB;
  
  CREATE SERVICE
         [//AWDB/1DBSample/InitiatorService]
         ON QUEUE InitiatorQueue1DB;
  GO
  </code></pre>
  
  <p>So far, so good. </p>
  
  <ul>
  <li>Then the following queries are used to look at the queues (now empty when used):</li>
  </ul>
  
  <hr>
  
  <pre><code>USE AdventureWorks2008R2;
  GO
  
  SELECT * FROM InitiatorQueue1DB WITH (NOLOCK);
  SELECT * FROM TargetQueue1DB WITH (NOLOCK);
  SELECT * FROM sys.transmission_queue;
  GO
  </code></pre>
  
  <ul>
  <li>The problem manifests when the message is sent:</li>
  </ul>
  
  <hr>
  
  <pre><code>BEGIN TRANSACTION;
  
  BEGIN DIALOG @InitDlgHandle
       FROM SERVICE
        [//AWDB/1DBSample/InitiatorService]
       TO SERVICE
        N'//AWDB/1DBSample/TargetService'
       ON CONTRACT
        [//AWDB/1DBSample/SampleContract]
       WITH
           ENCRYPTION = OFF;
  
  SELECT @RequestMsg =
         N'&lt;RequestMsg&gt;Message for Target service.&lt;/RequestMsg&gt;';
  
  SEND ON CONVERSATION @InitDlgHandle
       MESSAGE TYPE 
       [//AWDB/1DBSample/RequestMessage]
       (@RequestMsg);
  
  SELECT @RequestMsg AS SentRequestMsg;
  
  COMMIT TRANSACTION;
  GO  
  </code></pre>
  
  <p>When looking at the queues, the <code>Initiator...</code> and the <code>Target...</code> queues are empty, and the sent message can be found in <code>sys.transmission_queue</code> with the above mentioned error reported via the <code>transmission_status</code>.</p>