public void EndConversation(TimeSpan timeout)
        {
            TimeoutHelper helper = new TimeoutHelper(timeout);

            try
            {
                string SQL = "END CONVERSATION @ConversationHandle";

                SqlCommand cmd = new SqlCommand(SQL, con);
                cmd.CommandTimeout = helper.RemainingTimeInMillisecondsOrZero();
                cmd.Parameters.Add("@ConversationHandle", SqlDbType.UniqueIdentifier).Value = conversation.ConversationHandle;

                cmd.ExecuteNonQuery();
                this.conversation = null;
            }
            catch (SqlException ex)
            {
                if (!helper.IsTimeRemaining)
                {
                    throw new TimeoutException(String.Format("Timed out while ending conversation {0}. Timeout value was {1} seconds", this.conversation.ConversationHandle, timeout.TotalSeconds), ex);
                }
                else
                {
                    throw new CommunicationException(String.Format("An exception occurred while ending conversation {0}.", this.conversation.ConversationHandle), ex);
                }
            }
        }
        public Guid BeginNewConversation(Guid conversationGroupId, TimeSpan timeout)
        {
            ThrowIfDisposedOrNotOpen();
            endConversationOnClose = false; //if a conversation is explicitly started, don't automatically close it.
            TimeoutHelper helper = new TimeoutHelper(timeout);

            try
            {
                string SQL = string.Format(
                    @"BEGIN DIALOG CONVERSATION @ConversationHandle 
                               FROM SERVICE @Source TO SERVICE @Target 
                               ON CONTRACT [{0}] WITH ENCRYPTION = {1}", contract, useEncryption?"ON":"OFF");
                if (conversationGroupId != Guid.Empty)
                {
                    SQL += String.Format(", RELATED_CONVERSATION_GROUP = '{0}'", conversationGroupId);
                }
                SqlCommand cmd = new SqlCommand(SQL, con);
                cmd.CommandTimeout = helper.RemainingTimeInMillisecondsOrZero();
                SqlParameter pconversationHandle = cmd.Parameters.Add("@ConversationHandle", SqlDbType.UniqueIdentifier);
                pconversationHandle.Direction = ParameterDirection.Output;

                SqlParameter pTarget = cmd.Parameters.Add("@Target", SqlDbType.VarChar);
                pTarget.Value = this.target;
                SqlParameter pSource = cmd.Parameters.Add("@Source", SqlDbType.VarChar);
                pSource.Value = this.source;

                cmd.ExecuteNonQuery();

                this.conversation = SsbHelper.GetConversationInfo((Guid)pconversationHandle.Value, con);
                return(this.conversation.ConversationHandle);
            }
            catch (SqlException ex)
            {
                if (!helper.IsTimeRemaining)
                {
                    throw new TimeoutException(String.Format("Timed out while beginning new conversation to service {0}. Timeout value was {1} seconds", this.target, timeout.TotalSeconds), ex);
                }
                else
                {
                    throw new CommunicationException(String.Format("An exception occurred while beginning new conversation to service {0}", this.target), ex);
                }
            }
            finally
            {
            }
        }
Esempio n. 3
0
        internal static ConversationInfo GetConversationInfo(Guid conversationHandle, SqlConnection con)
        {
            #region Build SqlCommand cmd
            string SQL = @"
select 
@ConversationGroupId = ce.conversation_group_id,
@ConversationId = ce.conversation_id,
@ServiceName = s.name,
@TargetServiceName = ce.far_service,
@QueueName = q.name,
@State = ce.state
from sys.conversation_endpoints ce
join sys.services s
  on ce.service_id = s.service_id
join sys.service_queue_usages u
  on u.service_id = s.service_id
join sys.service_queues q
  on q.object_id = u.service_queue_id
where ce.conversation_handle = @ConversationHandle;

if @@rowcount = 0
begin
  --raiserror('No Service Broker conversation found with handle: %s',16,1,@ConversationHandle);
  raiserror('Service Broker conversation not found.',16,1);
end

";

            SqlCommand cmd = new SqlCommand(SQL, con);

            SqlParameter pConversationId = cmd.Parameters.Add("@ConversationId", SqlDbType.UniqueIdentifier);
            pConversationId.Direction = ParameterDirection.Output;

            SqlParameter pConversationGroupId = cmd.Parameters.Add("@ConversationGroupId", SqlDbType.UniqueIdentifier);
            pConversationGroupId.Direction = ParameterDirection.Output;

            SqlParameter pServiceName = cmd.Parameters.Add("@ServiceName", SqlDbType.VarChar);
            pServiceName.Size      = 30;
            pServiceName.Direction = ParameterDirection.Output;

            SqlParameter pTargetServiceName = cmd.Parameters.Add("@TargetServiceName", SqlDbType.VarChar);
            pTargetServiceName.Size      = 30;
            pTargetServiceName.Direction = ParameterDirection.Output;

            SqlParameter pQueueName = cmd.Parameters.Add("@QueueName", SqlDbType.VarChar);
            pQueueName.Size      = 30;
            pQueueName.Direction = ParameterDirection.Output;

            SqlParameter pState = cmd.Parameters.Add("@State", SqlDbType.Char);
            pState.Size      = 2;
            pState.Direction = ParameterDirection.Output;

            SqlParameter pConversationHandle = cmd.Parameters.Add("@ConversationHandle", SqlDbType.UniqueIdentifier);
            pConversationHandle.Value = conversationHandle;
            #endregion

            try
            {
                cmd.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                throw new CommunicationException("An exception occurred while getting service information", ex);
            }



            string state = (string)pState.Value;
            if (state != "CO" && state != "SO")
            {
                throw new InvalidOperationException("Cannot open conversation in state "
                                                    + ConversationInfo.GetTransmissionStateDescription(state));
            }

            string serviceName         = (string)pServiceName.Value;
            string targetServiceName   = (string)pTargetServiceName.Value;
            string queueName           = (string)pQueueName.Value;
            Guid   conversationId      = (Guid)pConversationId.Value;
            Guid   conversationGroupId = (Guid)pConversationGroupId.Value;

            ConversationInfo ci = new ConversationInfo(serviceName, queueName, targetServiceName, conversationId, conversationHandle, conversationGroupId, state);



            return(ci);
        }
 public void OpenConversation(Guid conversationHandle, TimeSpan timeout)
 {
     ThrowIfDisposedOrNotOpen();
     this.conversation = SsbHelper.GetConversationInfo(conversationHandle, con);
 }
Esempio n. 5
0
        internal static ConversationInfo GetConversationInfo(Guid conversationHandle, SqlConnection con)
        {
            #region Build SqlCommand cmd
            string SQL = @"
            select
            @ConversationGroupId = ce.conversation_group_id,
            @ConversationId = ce.conversation_id,
            @ServiceName = s.name,
            @TargetServiceName = ce.far_service,
            @QueueName = q.name,
            @State = ce.state
            from sys.conversation_endpoints ce
            join sys.services s
              on ce.service_id = s.service_id
            join sys.service_queue_usages u
              on u.service_id = s.service_id
            join sys.service_queues q
              on q.object_id = u.service_queue_id
            where ce.conversation_handle = @ConversationHandle;

            if @@rowcount = 0
            begin
              --raiserror('No Service Broker conversation found with handle: %s',16,1,@ConversationHandle);
              raiserror('Service Broker conversation not found.',16,1);
            end

            ";

            SqlCommand cmd = new SqlCommand(SQL, con);

            SqlParameter pConversationId = cmd.Parameters.Add("@ConversationId", SqlDbType.UniqueIdentifier);
            pConversationId.Direction = ParameterDirection.Output;

            SqlParameter pConversationGroupId = cmd.Parameters.Add("@ConversationGroupId", SqlDbType.UniqueIdentifier);
            pConversationGroupId.Direction = ParameterDirection.Output;

            SqlParameter pServiceName = cmd.Parameters.Add("@ServiceName", SqlDbType.VarChar);
            pServiceName.Size = 30;
            pServiceName.Direction = ParameterDirection.Output;

            SqlParameter pTargetServiceName = cmd.Parameters.Add("@TargetServiceName", SqlDbType.VarChar);
            pTargetServiceName.Size = 30;
            pTargetServiceName.Direction = ParameterDirection.Output;

            SqlParameter pQueueName = cmd.Parameters.Add("@QueueName", SqlDbType.VarChar);
            pQueueName.Size = 30;
            pQueueName.Direction = ParameterDirection.Output;

            SqlParameter pState = cmd.Parameters.Add("@State", SqlDbType.Char);
            pState.Size = 2;
            pState.Direction = ParameterDirection.Output;

            SqlParameter pConversationHandle = cmd.Parameters.Add("@ConversationHandle", SqlDbType.UniqueIdentifier);
            pConversationHandle.Value = conversationHandle;
            #endregion

            try
            {
                cmd.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                throw new CommunicationException("An exception occurred while getting service information", ex);
            }

            string state = (string)pState.Value;
            if (state != "CO" && state != "SO")
            {
                throw new InvalidOperationException("Cannot open conversation in state "
                  + ConversationInfo.GetTransmissionStateDescription(state));
            }

            string serviceName = (string)pServiceName.Value;
            string targetServiceName = (string)pTargetServiceName.Value;
            string queueName = (string)pQueueName.Value;
            Guid conversationId = (Guid)pConversationId.Value;
            Guid conversationGroupId = (Guid)pConversationGroupId.Value;

            ConversationInfo ci = new ConversationInfo(serviceName, queueName, targetServiceName, conversationId, conversationHandle, conversationGroupId, state);

            return ci;
        }