Example #1
0
        public bool TryDeleteQueue(QueueInfo queue, out string errorMessage)
        {
            errorMessage = string.Empty;

            if (string.IsNullOrWhiteSpace(CurrentServer))
            {
                errorMessage = ERROR_SERVER_IS_NOT_DEFINED;
                return(false);
            }

            UseDatabase(DAJET_MQ_DATABASE_NAME);

            try
            {
                Dictionary <string, object> parameters = new Dictionary <string, object>()
                {
                    { "name", queue.Name }
                };
                SqlScripts.ExecuteProcedure(ConnectionString, "sp_delete_queue", parameters, out int result);
            }
            catch (Exception ex)
            {
                errorMessage = ExceptionHelper.GetErrorText(ex);
            }

            return(string.IsNullOrEmpty(errorMessage));
        }
Example #2
0
        public bool CreateQueue(QueueInfo queue, out string errorMessage)
        {
            errorMessage = string.Empty;

            if (string.IsNullOrWhiteSpace(CurrentServer))
            {
                errorMessage = ERROR_SERVER_IS_NOT_DEFINED;
                return(false);
            }
            if (string.IsNullOrWhiteSpace(CurrentDatabase))
            {
                errorMessage = ERROR_DATABASE_IS_NOT_DEFINED;
                return(false);
            }

            try
            {
                Guid brokerId = SqlScripts.ExecuteScalar <Guid>(ConnectionString, SqlScripts.SelectServiceBrokerIdentifierScript(CurrentDatabase));
                SqlScripts.ExecuteScript(ConnectionString, SqlScripts.CreateServiceQueueScript(brokerId, queue));
            }
            catch (Exception ex)
            {
                errorMessage = ExceptionHelper.GetErrorText(ex);
            }

            return(string.IsNullOrEmpty(errorMessage));
        }
Example #3
0
 public bool DaJetMQExists()
 {
     if (string.IsNullOrWhiteSpace(CurrentServer))
     {
         throw new InvalidOperationException(ERROR_SERVER_IS_NOT_DEFINED);
     }
     return(SqlScripts.ExecuteScalar <bool>(ConnectionString, SqlScripts.DaJetMQExistsScript()));
 }
Example #4
0
 public int GetServiceBrokerPortNumber()
 {
     if (string.IsNullOrWhiteSpace(CurrentServer))
     {
         throw new InvalidOperationException(ERROR_SERVER_IS_NOT_DEFINED);
     }
     return(SqlScripts.ExecuteScalar <int>(ConnectionString, SqlScripts.SelectServiceBrokerPortNumberScript()));
 }
Example #5
0
        public List <QueueInfo> SelectQueues(out string errorMessage)
        {
            errorMessage = string.Empty;
            List <QueueInfo> list = new List <QueueInfo>();
            string           sql  = SqlScripts.SelectQueuesScript();

            {
                SqlDataReader reader     = null;
                SqlConnection connection = new SqlConnection(ConnectionString);
                SqlCommand    command    = connection.CreateCommand();
                command.CommandType = CommandType.Text;
                command.CommandText = sql;
                try
                {
                    connection.Open();
                    reader = command.ExecuteReader();
                    while (reader.Read())
                    {
                        QueueInfo queue = new QueueInfo()
                        {
                            Name                  = reader.IsDBNull("name") ? string.Empty : reader.GetString("name"),
                            Status                = reader.IsDBNull("is_enqueue_enabled") ? false : reader.GetBoolean("is_enqueue_enabled"),
                            Retention             = reader.IsDBNull("is_retention_enabled") ? false : reader.GetBoolean("is_retention_enabled"),
                            Activation            = reader.IsDBNull("is_activation_enabled") ? false : reader.GetBoolean("is_activation_enabled"),
                            ProcedureName         = reader.IsDBNull("activation_procedure") ? string.Empty : reader.GetString("activation_procedure"),
                            MaxQueueReaders       = reader.IsDBNull("max_readers") ? (short)0 : reader.GetInt16("max_readers"),
                            PoisonMessageHandling = reader.IsDBNull("is_poison_message_handling_enabled") ? false : reader.GetBoolean("is_poison_message_handling_enabled")
                        };
                        queue.Name = string.IsNullOrWhiteSpace(queue.Name) ? string.Empty : GetQueueName(queue.Name);
                        list.Add(queue);
                    }
                }
                catch (Exception error)
                {
                    errorMessage = ExceptionHelper.GetErrorText(error);
                }
                finally
                {
                    if (reader != null)
                    {
                        if (reader.HasRows)
                        {
                            command.Cancel();
                        }
                        reader.Dispose();
                    }
                    if (command != null)
                    {
                        command.Dispose();
                    }
                    if (connection != null)
                    {
                        connection.Dispose();
                    }
                }
            }
            return(list);
        }
Example #6
0
        public string GetUserCertificateBinaryData()
        {
            if (string.IsNullOrWhiteSpace(CurrentServer))
            {
                throw new InvalidOperationException(ERROR_SERVER_IS_NOT_DEFINED);
            }

            return(SqlScripts.ExecuteScalar <string>(ConnectionString, SqlScripts.SelectCertificateBinaryDataScript()));
        }
Example #7
0
        public void CreateQueue(string name)
        {
            if (string.IsNullOrWhiteSpace(CurrentServer))
            {
                throw new InvalidOperationException(ERROR_SERVER_IS_NOT_DEFINED);
            }

            Guid brokerId = SqlScripts.ExecuteScalar <Guid>(ConnectionString, SqlScripts.SelectServiceBrokerIdentifierScript());

            SqlScripts.ExecuteScript(ConnectionString, SqlScripts.CreateServiceQueueScript(brokerId, name));
        }
Example #8
0
        public List <RouteInfo> SelectRoutes(out string errorMessage)
        {
            errorMessage = string.Empty;
            List <RouteInfo> list = new List <RouteInfo>();
            string           sql  = SqlScripts.SelectRoutesScript();

            {
                SqlDataReader reader     = null;
                SqlConnection connection = new SqlConnection(ConnectionString);
                SqlCommand    command    = connection.CreateCommand();
                command.CommandType = CommandType.Text;
                command.CommandText = sql;
                try
                {
                    connection.Open();
                    reader = command.ExecuteReader();
                    while (reader.Read())
                    {
                        RouteInfo route = new RouteInfo()
                        {
                            Name    = reader.IsDBNull("remote_service_name") ? string.Empty : reader.GetString("remote_service_name"),
                            Broker  = reader.IsDBNull("broker_instance") ? string.Empty : reader.GetString("broker_instance"),
                            Address = reader.IsDBNull("address") ? string.Empty : reader.GetString("address")
                        };
                        route.Name = string.IsNullOrWhiteSpace(route.Name) ? string.Empty : GetQueueName(route.Name);
                        list.Add(route);
                    }
                }
                catch (Exception error)
                {
                    errorMessage = ExceptionHelper.GetErrorText(error);
                }
                finally
                {
                    if (reader != null)
                    {
                        if (reader.HasRows)
                        {
                            command.Cancel();
                        }
                        reader.Dispose();
                    }
                    if (command != null)
                    {
                        command.Dispose();
                    }
                    if (connection != null)
                    {
                        connection.Dispose();
                    }
                }
            }
            return(list);
        }
Example #9
0
        public void CreateRemoteUser(string targetQueueFullName, string certificateBinaryData)
        {
            if (string.IsNullOrWhiteSpace(CurrentServer))
            {
                throw new InvalidOperationException(ERROR_SERVER_IS_NOT_DEFINED);
            }

            Guid targetBroker = SqlScripts.GetBrokerId(targetQueueFullName);

            SqlScripts.ExecuteScript(ConnectionString, SqlScripts.CreateRemoteUserScript(targetBroker, certificateBinaryData));
        }
Example #10
0
        public string ReceiveMessage(string queueFullName)
        {
            if (string.IsNullOrWhiteSpace(CurrentServer))
            {
                throw new InvalidOperationException(ERROR_SERVER_IS_NOT_DEFINED);
            }

            string connectionString = AlterDatabaseConnectionString();
            string message          = SqlScripts.ExecuteScalar <string>(connectionString, SqlScripts.SimpleReceiveMessageScript(queueFullName));

            return(message);
        }
Example #11
0
        public IMessageConsumer CreateMessageConsumer(string queueName)
        {
            if (string.IsNullOrWhiteSpace(CurrentServer))
            {
                throw new InvalidOperationException(ERROR_SERVER_IS_NOT_DEFINED);
            }

            Guid   brokerId      = SqlScripts.ExecuteScalar <Guid>(ConnectionString, SqlScripts.SelectServiceBrokerIdentifierScript());
            string queueFullName = SqlScripts.CreateQueueName(brokerId, queueName);

            return(new MessageConsumer(CurrentServer, queueFullName));
        }
Example #12
0
        public Guid GetServiceBrokerIdentifier()
        {
            if (string.IsNullOrWhiteSpace(CurrentServer))
            {
                throw new InvalidOperationException(ERROR_SERVER_IS_NOT_DEFINED);
            }

            UseDatabase(DAJET_MQ_DATABASE_NAME);

            SqlScripts.ExecuteProcedure(ConnectionString, "fn_service_broker_guid", null, out Guid brokerGuid);

            return(brokerGuid);
        }
Example #13
0
        public void SetupServiceBroker()
        {
            if (string.IsNullOrWhiteSpace(CurrentServer))
            {
                throw new InvalidOperationException(ERROR_SERVER_IS_NOT_DEFINED);
            }

            SqlScripts.ExecuteScript(ConnectionString, SqlScripts.CreateDatabaseScript());
            Guid brokerId = SqlScripts.ExecuteScalar <Guid>(ConnectionString, SqlScripts.SelectServiceBrokerIdentifierScript());

            SqlScripts.ExecuteScript(ConnectionString, SqlScripts.CreateDatabaseUserScript(brokerId));
            SqlScripts.ExecuteScript(ConnectionString, SqlScripts.CreateChannelsTableScript());
        }
Example #14
0
        public void SendMessage(string routeName, string payload)
        {
            if (string.IsNullOrWhiteSpace(CurrentServer))
            {
                throw new InvalidOperationException(ERROR_SERVER_IS_NOT_DEFINED);
            }

            Guid   handle = SqlScripts.ExecuteScalar <Guid>(ConnectionString, SqlScripts.SelectDialogHandleScript(routeName));
            string sql    = SqlScripts.SendMessageToChannelScript();
            Dictionary <string, object> parameters = new Dictionary <string, object>();

            parameters.Add("handle", handle);
            parameters.Add("message", payload);
            SqlScripts.ExecuteScript(ConnectionString, sql, parameters);
        }
Example #15
0
        public void CreateRoute(string name, string sourceQueueFullName, string targetAddress, string targetQueueFullName)
        {
            if (string.IsNullOrWhiteSpace(CurrentServer))
            {
                throw new InvalidOperationException(ERROR_SERVER_IS_NOT_DEFINED);
            }

            Guid   sourceBroker    = SqlScripts.GetBrokerId(sourceQueueFullName);
            Guid   targetBroker    = SqlScripts.GetBrokerId(targetQueueFullName);
            string sourceQueueName = SqlScripts.GetQueueName(sourceQueueFullName);
            string targetQueueName = SqlScripts.GetQueueName(targetQueueFullName);

            string script = SqlScripts.CreateRouteScript(name, sourceBroker, sourceQueueName, targetAddress, targetBroker, targetQueueName);

            SqlScripts.ExecuteScript(ConnectionString, script);
        }
Example #16
0
        private IList <string> DoReceive(int numberOfMessages, int timeoutMilliseconds)
        {
            List <string> messages = new List <string>();

            {
                SqlCommand    command = null;
                SqlDataReader reader  = null;
                try
                {
                    command             = _connection.CreateCommand();
                    command.CommandType = CommandType.Text;
                    command.CommandText = SqlScripts.ReceiveListOfMessagesScript(QueueName, numberOfMessages, timeoutMilliseconds);
                    command.Transaction = _transaction;

                    reader = command.ExecuteReader();
                    while (reader.Read())
                    {
                        // 0 - dialog_handle : uniqueidentifier
                        // 1 - message_type  : nvarchar(256)
                        if (!reader.IsDBNull(2))
                        {
                            messages.Add(reader.GetString(2)); // message_body
                        }
                    }
                }
                catch
                {
                    throw;
                }
                finally
                {
                    if (reader != null)
                    {
                        if (reader.HasRows)
                        {
                            command.Cancel();
                        }
                        reader.Dispose();
                    }
                    if (command != null)
                    {
                        command.Dispose();
                    }
                }
            }
            return(messages);
        }
Example #17
0
        private string DoReceive(int timeoutMilliseconds)
        {
            string message_body = null;

            {
                SqlCommand    command = null;
                SqlDataReader reader  = null;
                try
                {
                    command             = _connection.CreateCommand();
                    command.CommandType = CommandType.Text;
                    command.CommandText = SqlScripts.ReceiveOneMessageScript(QueueName, timeoutMilliseconds);
                    command.Transaction = _transaction;

                    reader = command.ExecuteReader();
                    if (reader.Read())
                    {
                        if (!reader.IsDBNull(0))
                        {
                            message_body = (string)reader[0];
                        }
                    }
                }
                catch
                {
                    throw;
                }
                finally
                {
                    if (reader != null)
                    {
                        if (reader.HasRows)
                        {
                            command.Cancel();
                        }
                        reader.Dispose();
                    }
                    if (command != null)
                    {
                        command.Dispose();
                    }
                }
            }
            return(message_body);
        }
Example #18
0
        public string GetQueueFullName(string queueName)
        {
            if (string.IsNullOrWhiteSpace(CurrentServer))
            {
                throw new InvalidOperationException(ERROR_SERVER_IS_NOT_DEFINED);
            }

            UseDatabase(DAJET_MQ_DATABASE_NAME);

            Dictionary <string, object> parameters = new Dictionary <string, object>()
            {
                { "name", queueName }
            };

            SqlScripts.ExecuteProcedure(ConnectionString, "fn_create_queue_name", parameters, out string queueFullName);

            return(queueFullName);
        }
Example #19
0
        public bool DaJetMQExists()
        {
            if (string.IsNullOrWhiteSpace(CurrentServer))
            {
                throw new InvalidOperationException(ERROR_SERVER_IS_NOT_DEFINED);
            }
            bool result = false;

            try
            {
                result = SqlScripts.ExecuteScalar <bool>(ConnectionString, SqlScripts.DaJetMQExistsScript());
            }
            catch (Exception error)
            {
                // TODO: handle error
                result = false;
            }
            return(result);
        }
Example #20
0
        public void CreatePublicEndpoint(string name, int port)
        {
            if (string.IsNullOrWhiteSpace(CurrentServer))
            {
                throw new InvalidOperationException(ERROR_SERVER_IS_NOT_DEFINED);
            }

            { /* start of limited scope */
                SqlConnection connection = new SqlConnection(ConnectionString);
                SqlCommand    command    = connection.CreateCommand();
                command.CommandType = CommandType.Text;
                command.CommandText = SqlScripts.CreatePublicEndpointScript(name, port);
                try
                {
                    connection.Open();
                    int result = command.ExecuteNonQuery();
                }
                catch (Exception error)
                {
                    // TODO: log error
                    _ = error.Message;
                    throw;
                }
                finally
                {
                    if (command != null)
                    {
                        command.Dispose();
                    }
                    if (connection != null)
                    {
                        connection.Dispose();
                    }
                }
            } /* end of limited scope */
        }