Ejemplo n.º 1
0
        internal static bool Start(string connectionString, string queue, bool useDefaults)
        {
            if (string.IsNullOrEmpty(connectionString))
            {
                if (null == connectionString)
                {
                    throw ADP.ArgumentNull(nameof(connectionString));
                }
                else
                {
                    throw ADP.Argument(nameof(connectionString));
                }
            }

            if (!useDefaults && string.IsNullOrEmpty(queue))
            {                       // If specified but null or empty, use defaults.
                useDefaults = true;
                queue       = null; // Force to null - for proper hashtable comparison for default case.
            }

            // End duplicate Start/Stop logic.

            bool errorOccurred = false;
            bool result        = false;

            lock (s_startStopLock)
            {
                try
                {
                    if (null == s_processDispatcher)
                    { // Ensure _processDispatcher reference is present - inside lock.
                        s_processDispatcher = SqlDependencyProcessDispatcher.SingletonProcessDispatcher;
                    }

                    if (useDefaults)
                    { // Default listener.
                        string server = null;
                        DbConnectionPoolIdentity identity = null;
                        string user           = null;
                        string database       = null;
                        string service        = null;
                        bool   appDomainStart = false;

                        RuntimeHelpers.PrepareConstrainedRegions();
                        try
                        { // CER to ensure that if Start succeeds we add to hash completing setup.
                            // Start using process wide default service/queue & database from connection string.
                            result = s_processDispatcher.StartWithDefault(
                                connectionString,
                                out server,
                                out identity,
                                out user,
                                out database,
                                ref service,
                                s_appDomainKey,
                                SqlDependencyPerAppDomainDispatcher.SingletonInstance,
                                out errorOccurred,
                                out appDomainStart);
                        }
                        finally
                        {
                            if (appDomainStart && !errorOccurred)
                            { // If success, add to hashtable.
                                IdentityUserNamePair identityUser    = new IdentityUserNamePair(identity, user);
                                DatabaseServicePair  databaseService = new DatabaseServicePair(database, service);
                                if (!AddToServerUserHash(server, identityUser, databaseService))
                                {
                                    try
                                    {
                                        Stop(connectionString, queue, useDefaults, true);
                                    }
                                    catch (Exception e)
                                    { // Discard stop failure!
                                        if (!ADP.IsCatchableExceptionType(e))
                                        {
                                            throw;
                                        }

                                        ADP.TraceExceptionWithoutRethrow(e); // Discard failure, but trace for now.
                                    }
                                    throw SQL.SqlDependencyDuplicateStart();
                                }
                            }
                        }
                    }
                    else
                    { // Start with specified service/queue & database.
                        result = s_processDispatcher.Start(
                            connectionString,
                            queue,
                            s_appDomainKey,
                            SqlDependencyPerAppDomainDispatcher.SingletonInstance);
                        // No need to call AddToServerDatabaseHash since if not using default queue user is required
                        // to provide options themselves.
                    }
                }
                catch (Exception e)
                {
                    if (!ADP.IsCatchableExceptionType(e))
                    {
                        throw;
                    }

                    ADP.TraceExceptionWithoutRethrow(e); // Discard failure, but trace for now.

                    throw;
                }
            }

            return(result);
        }