/// <devdoc>
        /// Invalidates a cache entry.
        /// </devdoc>
        internal void InvalidateCacheEntry()
        {
            string          key   = CreateMasterCacheKey();
            DataSourceCache cache = Cache;

            Debug.Assert(cache != null);
            cache.Invalidate(key);
        }
예제 #2
0
        protected virtual string GetXmlDataFromUrl(string datasource)
        {
            if (DataSourceCache != null && DataSourceCache.ContainsKey(datasource))
            {
                var xmlString = DataSourceCache[datasource];
                if (String.IsNullOrEmpty(xmlString))
                {
                    Logger.AddError("Xml was null or empty in GetxmlDataFromUrl. Proceed without cache.",
                                    String.Format("The Xml retrieved from cache was null or empty. datasource: {0}.", datasource));
                }
                return(xmlString);
            }
            if (!String.IsNullOrEmpty(datasource) && datasource.StartsWith(UrlPrefix))
            {
                var myUri            = new Uri(datasource);
                var myHttpWebRequest = (HttpWebRequest)WebRequest.Create(myUri);

                try
                {
                    var myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
                    using (var streamResponse = myHttpWebResponse.GetResponseStream())
                    {
                        if (streamResponse != null)
                        {
                            using (var xmlResponse = XmlReader.Create(streamResponse))
                            {
                                var xDoc      = XDocument.Load(xmlResponse);
                                var xmlString = xDoc.ToString();
                                if (DataSourceCache != null)
                                {
                                    if (String.IsNullOrEmpty(xmlString))
                                    {
                                        Logger.AddInfo("Xml was null or empty",
                                                       String.Format(
                                                           "Xml retrieved in GetXmlDataFromUrl was null or empty. Datasource: {0}.",
                                                           datasource));
                                    }
                                    else
                                    {
                                        if (DataSourceCache.ContainsKey(datasource))
                                        {
                                            DataSourceCache[datasource] = xmlString;
                                        }
                                        else
                                        {
                                            DataSourceCache.Add(datasource, xmlString);
                                        }
                                    }
                                }
                                return(xmlString);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Logger.AddError("Error",
                                    String.Format("Reading the Url in XmlFileData failed with an exception. Exception: {0}.", ex));
                }
                Logger.AddError("Error",
                                String.Format(
                                    "The URL provided failed loading any xml data. DataSource: '{0}'",
                                    DataSourceString));
                return(String.Empty);
            }
            return(String.Empty);
        }
        protected internal override IEnumerable ExecuteSelect(DataSourceSelectArguments arguments)
        {
            SqlCacheDependency dependency;

            if (this.SelectCommand.Length == 0)
            {
                return(null);
            }
            DbConnection connection = this._owner.CreateConnection(this._owner.ConnectionString);

            if (connection == null)
            {
                throw new InvalidOperationException(System.Web.SR.GetString("SqlDataSourceView_CouldNotCreateConnection", new object[] { this._owner.ID }));
            }
            DataSourceCache cache          = this._owner.Cache;
            bool            flag           = (cache != null) && cache.Enabled;
            string          sortExpression = arguments.SortExpression;

            if (this.CanPage)
            {
                arguments.AddSupportedCapabilities(DataSourceCapabilities.Page);
            }
            if (this.CanSort)
            {
                arguments.AddSupportedCapabilities(DataSourceCapabilities.Sort);
            }
            if (this.CanRetrieveTotalRowCount)
            {
                arguments.AddSupportedCapabilities(DataSourceCapabilities.RetrieveTotalRowCount);
            }
            if (flag)
            {
                if (this._owner.DataSourceMode != SqlDataSourceMode.DataSet)
                {
                    throw new NotSupportedException(System.Web.SR.GetString("SqlDataSourceView_CacheNotSupported", new object[] { this._owner.ID }));
                }
                arguments.RaiseUnsupportedCapabilitiesError(this);
                DataSet set = this._owner.LoadDataFromCache(0, -1) as DataSet;
                if (set != null)
                {
                    IOrderedDictionary parameterValues = this.FilterParameters.GetValues(this._context, this._owner);
                    if (this.FilterExpression.Length > 0)
                    {
                        SqlDataSourceFilteringEventArgs args = new SqlDataSourceFilteringEventArgs(parameterValues);
                        this.OnFiltering(args);
                        if (args.Cancel)
                        {
                            return(null);
                        }
                    }
                    return(FilteredDataSetHelper.CreateFilteredDataView(set.Tables[0], sortExpression, this.FilterExpression, parameterValues));
                }
            }
            DbCommand command = this._owner.CreateCommand(this.SelectCommand, connection);

            this.InitializeParameters(command, this.SelectParameters, null);
            command.CommandType = GetCommandType(this.SelectCommandType);
            SqlDataSourceSelectingEventArgs e = new SqlDataSourceSelectingEventArgs(command, arguments);

            this.OnSelecting(e);
            if (e.Cancel)
            {
                return(null);
            }
            string sortParameterName = this.SortParameterName;

            if (sortParameterName.Length > 0)
            {
                if (command.CommandType != CommandType.StoredProcedure)
                {
                    throw new NotSupportedException(System.Web.SR.GetString("SqlDataSourceView_SortParameterRequiresStoredProcedure", new object[] { this._owner.ID }));
                }
                command.Parameters.Add(this._owner.CreateParameter(this.ParameterPrefix + sortParameterName, sortExpression));
                arguments.SortExpression = string.Empty;
            }
            arguments.RaiseUnsupportedCapabilitiesError(this);
            sortExpression = arguments.SortExpression;
            if (this.CancelSelectOnNullParameter)
            {
                int count = command.Parameters.Count;
                for (int i = 0; i < count; i++)
                {
                    DbParameter parameter = command.Parameters[i];
                    if (((parameter != null) && (parameter.Value == null)) && ((parameter.Direction == ParameterDirection.Input) || (parameter.Direction == ParameterDirection.InputOutput)))
                    {
                        return(null);
                    }
                }
            }
            this.ReplaceNullValues(command);
            IEnumerable enumerable = null;

            switch (this._owner.DataSourceMode)
            {
            case SqlDataSourceMode.DataReader:
            {
                if (this.FilterExpression.Length > 0)
                {
                    throw new NotSupportedException(System.Web.SR.GetString("SqlDataSourceView_FilterNotSupported", new object[] { this._owner.ID }));
                }
                if (sortExpression.Length > 0)
                {
                    throw new NotSupportedException(System.Web.SR.GetString("SqlDataSourceView_SortNotSupported", new object[] { this._owner.ID }));
                }
                bool flag4 = false;
                try
                {
                    if (connection.State != ConnectionState.Open)
                    {
                        connection.Open();
                    }
                    enumerable = command.ExecuteReader(CommandBehavior.CloseConnection);
                    flag4      = true;
                    SqlDataSourceStatusEventArgs args6 = new SqlDataSourceStatusEventArgs(command, 0, null);
                    this.OnSelected(args6);
                }
                catch (Exception exception2)
                {
                    bool flag5;
                    if (!flag4)
                    {
                        SqlDataSourceStatusEventArgs args7 = new SqlDataSourceStatusEventArgs(command, 0, exception2);
                        this.OnSelected(args7);
                        if (!args7.ExceptionHandled)
                        {
                            throw;
                        }
                        return(enumerable);
                    }
                    exception2 = this.BuildCustomException(exception2, DataSourceOperation.Select, command, out flag5);
                    if (flag5)
                    {
                        throw exception2;
                    }
                    throw;
                }
                return(enumerable);
            }

            case SqlDataSourceMode.DataSet:
                dependency = null;
                if (flag && (cache is SqlDataSourceCache))
                {
                    SqlDataSourceCache cache2 = (SqlDataSourceCache)cache;
                    if (string.Equals(cache2.SqlCacheDependency, "CommandNotification", StringComparison.OrdinalIgnoreCase))
                    {
                        if (!(command is SqlCommand))
                        {
                            throw new InvalidOperationException(System.Web.SR.GetString("SqlDataSourceView_CommandNotificationNotSupported", new object[] { this._owner.ID }));
                        }
                        dependency = new SqlCacheDependency((SqlCommand)command);
                        break;
                    }
                }
                break;

            default:
                return(enumerable);
            }
            DbDataAdapter adapter      = this._owner.CreateDataAdapter(command);
            DataSet       dataSet      = new DataSet();
            int           affectedRows = 0;
            bool          flag2        = false;

            try
            {
                affectedRows = adapter.Fill(dataSet, base.Name);
                flag2        = true;
                SqlDataSourceStatusEventArgs args3 = new SqlDataSourceStatusEventArgs(command, affectedRows, null);
                this.OnSelected(args3);
            }
            catch (Exception exception)
            {
                if (flag2)
                {
                    bool flag3;
                    exception = this.BuildCustomException(exception, DataSourceOperation.Select, command, out flag3);
                    if (flag3)
                    {
                        throw exception;
                    }
                    throw;
                }
                SqlDataSourceStatusEventArgs args4 = new SqlDataSourceStatusEventArgs(command, affectedRows, exception);
                this.OnSelected(args4);
                if (!args4.ExceptionHandled)
                {
                    throw;
                }
            }
            finally
            {
                if (connection.State == ConnectionState.Open)
                {
                    connection.Close();
                }
            }
            DataTable table = (dataSet.Tables.Count > 0) ? dataSet.Tables[0] : null;

            if (flag && (table != null))
            {
                this._owner.SaveDataToCache(0, -1, dataSet, dependency);
            }
            if (table == null)
            {
                return(enumerable);
            }
            IOrderedDictionary values = this.FilterParameters.GetValues(this._context, this._owner);

            if (this.FilterExpression.Length > 0)
            {
                SqlDataSourceFilteringEventArgs args5 = new SqlDataSourceFilteringEventArgs(values);
                this.OnFiltering(args5);
                if (args5.Cancel)
                {
                    return(null);
                }
            }
            return(FilteredDataSetHelper.CreateFilteredDataView(table, sortExpression, this.FilterExpression, values));
        }
        private int ExecuteDbCommand(DbCommand command, DataSourceOperation operation)
        {
            int  affectedRows = 0;
            bool flag         = false;

            try
            {
                if (command.Connection.State != ConnectionState.Open)
                {
                    command.Connection.Open();
                }
                affectedRows = command.ExecuteNonQuery();
                if (affectedRows > 0)
                {
                    this.OnDataSourceViewChanged(EventArgs.Empty);
                    DataSourceCache cache = this._owner.Cache;
                    if ((cache != null) && cache.Enabled)
                    {
                        this._owner.InvalidateCacheEntry();
                    }
                }
                flag = true;
                SqlDataSourceStatusEventArgs e = new SqlDataSourceStatusEventArgs(command, affectedRows, null);
                switch (operation)
                {
                case DataSourceOperation.Delete:
                    this.OnDeleted(e);
                    return(affectedRows);

                case DataSourceOperation.Insert:
                    this.OnInserted(e);
                    return(affectedRows);

                case DataSourceOperation.Select:
                    return(affectedRows);

                case DataSourceOperation.Update:
                    this.OnUpdated(e);
                    return(affectedRows);
                }
                return(affectedRows);
            }
            catch (Exception exception)
            {
                bool flag2;
                if (!flag)
                {
                    SqlDataSourceStatusEventArgs args2 = new SqlDataSourceStatusEventArgs(command, affectedRows, exception);
                    switch (operation)
                    {
                    case DataSourceOperation.Delete:
                        this.OnDeleted(args2);
                        break;

                    case DataSourceOperation.Insert:
                        this.OnInserted(args2);
                        break;

                    case DataSourceOperation.Update:
                        this.OnUpdated(args2);
                        break;
                    }
                    if (!args2.ExceptionHandled)
                    {
                        throw;
                    }
                    return(affectedRows);
                }
                exception = this.BuildCustomException(exception, operation, command, out flag2);
                if (flag2)
                {
                    throw exception;
                }
                throw;
            }
            finally
            {
                if (command.Connection.State == ConnectionState.Open)
                {
                    command.Connection.Close();
                }
            }
            return(affectedRows);
        }
예제 #5
0
        private void UpdateUserContacts(DateTime now,
                                        UserEntity userEntity,
                                        IList <UserContactLoAModel> userContacts,
                                        UserAction action,
                                        string externalProviderName = null
                                        )
        {
            var userContactsDict     = m_userContactRepository.GetUserContacts(userEntity.Id)?.ToDictionary(x => x.Type);
            var userContactsToUpdate = new List <UserContactEntity>();

            var levelOfAssuranceCache = new LevelOfAssuranceCache(m_levelOfAssuranceRepository);
            var dataSourceCache       = new DataSourceCache(m_dataSourceRepository, m_externalLoginProviderRepository);

            foreach (var userContactModel in userContacts)
            {
                var userContact = userContactModel.UserContact;

                if (userContactsDict != null && userContactsDict.ContainsKey(userContact.Type))
                {
                    var userContactEntity = userContactsDict[userContact.Type];

                    if (!userContact.Value.Equals(userContactEntity.Value))
                    {
                        userContactEntity.LevelOfAssurance = levelOfAssuranceCache.GetByEnum(userContactModel.LevelOfAssuranceEnum);
                        userContactEntity.DataSource       = dataSourceCache.GetByExternalProvider(externalProviderName);
                    }

                    userContactEntity.Value = userContact.Value;

                    if (
                        userContact.ConfirmCode != null &&
                        !userContact.ConfirmCode.Equals(userContactEntity.ConfirmCode)
                        )
                    {
                        userContactEntity.ConfirmCode           = userContact.ConfirmCode;
                        userContactEntity.ConfirmCodeChangeTime = now;
                    }

                    userContactsToUpdate.Add(userContactEntity);
                }
                else
                {
                    // expect this mean it is new "row"

                    userContact.User = userEntity;
                    userContact.ConfirmCodeChangeTime = now;

                    userContact.LevelOfAssurance = levelOfAssuranceCache.GetByEnum(userContactModel.LevelOfAssuranceEnum);
                    userContact.DataSource       = dataSourceCache.GetByExternalProvider(externalProviderName);

                    userContactsToUpdate.Add(userContact);
                }
            }

            switch (action)
            {
            case UserAction.Create:
                m_userContactRepository.CreateUserContacts(userContactsToUpdate);
                break;

            case UserAction.Update:
                m_userContactRepository.UpdateUserContacts(userContactsToUpdate);
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(action), action, null);
            }
        }