Why This Error Happens
Error 13157 occurs within SQL Server Service Broker when a dialog conversation is forcibly closed, typically because the receiving endpoint sent an “END CONVERSATION WITH ERROR” message or because the conversation timed out due to an unhandled exception in the processing stored procedure. This is often triggered by malformed message payloads, missing error handling in activation procedures, or a mismatch between the contract and the message type being sent. In Windows Server 2025 / Windows 11 environments, this can also surface due to corrupted Service Broker queues after an improper shutdown or failed database restore.
Step-by-Step Resolution Guide
Step 1: Identify the Failing Conversation Handle
- Query
sys.transmission_queueto view the exact error message tied to the conversation_handle. - Run:
SELECT conversation_handle, transmission_status FROM sys.transmission_queue; - Cross-reference the
transmission_statuscolumn text, which usually contains the root cause (e.g., “contract mismatch” or “message body cannot be parsed”).
Step 2: Review and Patch the Activation Stored Procedure
- Open the stored procedure bound to the queue via
ALTER QUEUEdefinition in SQL Server Management Studio (SSMS). - Ensure a
TRY...CATCHblock wraps allRECEIVEstatements to prevent unhandled exceptions from silently ending the conversation. - Add explicit handling for
END CONVERSATIONanderrormessage types alongside your standard message types.
Step 3: Validate Service Broker Configuration and Permissions
- Confirm Service Broker is enabled on the database:
SELECT is_broker_enabled FROM sys.databases WHERE name = 'YourDB'; - If disabled, run:
ALTER DATABASE YourDB SET ENABLE_BROKER; - Verify the SQL Server service account (under Windows Server 2025 services manager) has appropriate permissions on the queue objects (
CONTROL,RECEIVE,SEND).
Step 4: Clear Poisoned Messages and Reset the Queue
- Check if the queue is disabled due to poison message handling:
SELECT is_receive_enabled FROM sys.service_queues WHERE name = 'YourQueueName'; - If disabled, re-enable it:
ALTER QUEUE YourQueueName WITH STATUS = ON; - Restart the SQL Server service via
services.mscon Windows Server 2025 to clear any cached broker session state.
Alternative or Verification Steps
- Verify the fix by sending a test message through the conversation and confirming it processes without appearing in
sys.transmission_queue. - If the issue persists, drop and recreate the Service Broker queue, service, and contract objects entirely, then re-initiate the conversation from the initiator endpoint.