/// <summary>
        /// Удаляет сущность с именем entityName из кеша
        /// </summary>
        /// <param name="xrmServiceContext"></param>
        /// <param name="entityName"></param>
        public static void ClearXrmCache(this XrmServiceContext xrmServiceContext, string entityName)
        {
            var dependency = String.Format("xrm:dependency:entity:{0}", entityName).ToLower();
            var cache      = ObjectCacheManager.GetInstance(null);

            cache.Remove(dependency);
        }
        protected IAsyncResult ExecuteSelect(
            IOrganizationService client,
            QueryBase query,
            AsyncCallback asyncCallback,
            object asyncState)
        {
            Request.Query = query;

            if (Owner.CacheParameters.Enabled)
            {
                string cacheKey = GetCacheKey(query);

                // try load from cache
                IEnumerable selectResult = ObjectCacheManager.GetInstance().Get(cacheKey) as IEnumerable;

                if (selectResult != null)
                {
                    Tracing.FrameworkInformation("CrmDataSourceView", "ExecuteSelect", "Found in cache: {0}", cacheKey);
                    Tracing.FrameworkInformation("CrmDataSourceView", "ExecuteSelect", "End");

                    return(new SynchronousAsyncSelectResult(selectResult, asyncCallback, asyncState));
                }
            }

            _execute = new Func <QueryBase, EntityCollection>(client.RetrieveMultiple);
            return(_execute.BeginInvoke(query, asyncCallback, asyncState));
        }
        protected IAsyncResult ExecuteSelect(
            IOrganizationService client,
            QueryBase query,
            Fetch fetch,
            AsyncCallback asyncCallback,
            object asyncState)
        {
            string cacheKey = string.Empty;

            if (query != null)
            {
                Request.Query = query;
                cacheKey      = GetCacheKey(query);
            }
            else if (fetch != null)
            {
                Fetch.Entity = fetch.Entity;
                cacheKey     = GetCacheKey(fetch.ToFetchExpression());
            }

            if (Owner.CacheParameters.Enabled)
            {
                // try load from cache
                IEnumerable selectResult = ObjectCacheManager.GetInstance().Get(cacheKey) as IEnumerable;

                if (selectResult != null)
                {
                    ADXTrace.Instance.TraceInfo(TraceCategory.Application, string.Format("Found in cache: {0}", cacheKey));
                    ADXTrace.Instance.TraceInfo(TraceCategory.Application, "End");

                    return(new SynchronousAsyncSelectResult(selectResult, asyncCallback, asyncState));
                }
            }

            if (Owner.IsSingleSource)
            {
                _executeSingle = new Func <Fetch, Entity>(f => client.RetrieveSingle(f));
                return(_executeSingle.BeginInvoke(fetch, asyncCallback, asyncState));
            }
            else
            {
                if (fetch != null)
                {
                    _executeFetchMultiple = new Func <Fetch, EntityCollection>(f => client.RetrieveMultiple(f));
                    return(_executeFetchMultiple.BeginInvoke(fetch, asyncCallback, asyncState));
                }
            }

            _execute = new Func <QueryBase, EntityCollection>(client.RetrieveMultiple);
            return(_execute.BeginInvoke(query, asyncCallback, asyncState));
        }
        /// <summary>
        /// Sets the challenge text for a particular ID.
        /// </summary>
        /// <param name="challengeId">The ID of the challenge with which this text should be associated.</param>
        /// <param name="text">The text to store along with the challenge ID.</param>
        /// <param name="expiration">The expiration date fo the challenge.</param>
        internal static void SetChallengeText(Guid challengeId, string text, DateTime expiration, string objectCacheName)
        {
            var key = GetChallengeCacheKey(challengeId);

            if (text == null)
            {
                ObjectCacheManager.GetInstance(objectCacheName).Remove(key);
            }
            else
            {
                var policy = new CacheItemPolicy {
                    Priority = CacheItemPriority.NotRemovable
                };
                ObjectCacheManager.GetInstance(objectCacheName).Insert(key, text, policy);
            }
        }
        protected override IEnumerable EndExecuteSelect(IAsyncResult asyncResult)
        {
            ADXTrace.Instance.TraceInfo(TraceCategory.Application, "Begin");

            if (_client == null)
            {
                ADXTrace.Instance.TraceInfo(TraceCategory.Application, "End: _client=null");

                return(null);
            }

            IEnumerable selectResult = null;
            int         rowsAffected = 0;

            try
            {
                SynchronousAsyncSelectResult syncResult = asyncResult as SynchronousAsyncSelectResult;

                if (syncResult != null)
                {
                    ADXTrace.Instance.TraceInfo(TraceCategory.Application, "syncResult");

                    selectResult = syncResult.SelectResult;
                }
                else
                {
                    ADXTrace.Instance.TraceInfo(TraceCategory.Application, "EndExecute");

                    if (!Owner.IsSingleSource)
                    {
                        var response = _execute != null?_execute.EndInvoke(asyncResult) : _executeFetchMultiple.EndInvoke(asyncResult);

                        if (response != null)
                        {
                            var entities = response.Entities;
                            rowsAffected = entities.Count;
                            selectResult = ExecuteSelect(entities).ToList();

                            if (Owner.CacheParameters.Enabled)
                            {
                                IEnumerable <string> dependencies;
                                string cacheKey;
                                if (Request.Query != null)
                                {
                                    dependencies = GetCacheDependencies(Request.Query, selectResult);
                                    cacheKey     = GetCacheKey(Request.Query);
                                }
                                else
                                {
                                    dependencies = GetCacheDependencies(Fetch, selectResult, Owner.IsSingleSource);
                                    cacheKey     = GetCacheKey(Request.Query);
                                }

                                // insert into cache
                                ObjectCacheManager.GetInstance().Insert(cacheKey, selectResult, dependencies);
                            }
                        }
                    }
                    else
                    {
                        var entity = _executeSingle.EndInvoke(asyncResult);
                        if (entity != null)
                        {
                            selectResult = ExecuteSelect(new [] { entity }).ToList();
                            if (Owner.CacheParameters.Enabled)
                            {
                                var dependencies = GetCacheDependencies(Fetch, selectResult, Owner.IsSingleSource);
                                var cacheKey     = GetCacheKey(Fetch.ToFetchExpression());

                                // insert into cache
                                ObjectCacheManager.GetInstance().Insert(cacheKey, selectResult, dependencies);
                            }
                        }
                    }
                }
            }
            catch (System.Web.Services.Protocols.SoapException ex)
            {
                ADXTrace.Instance.TraceError(TraceCategory.Application, string.Format("{0}\n\n{1}", ex.Detail.InnerXml, ex.ToString()));
            }
            catch (Exception e)
            {
                ADXTrace.Instance.TraceError(TraceCategory.Application, string.Format("Exception: {0}", e.ToString()));

                // raise post-event with exception
                CrmDataSourceStatusEventArgs selectedExceptionArgs = new CrmDataSourceStatusEventArgs(0, e);
                OnSelected(selectedExceptionArgs);

                if (!selectedExceptionArgs.ExceptionHandled)
                {
                    throw;
                }

                return(selectResult);
            }
            finally
            {
                _client = null;
            }

            // raise post-event
            CrmDataSourceStatusEventArgs selectedArgs = new CrmDataSourceStatusEventArgs(rowsAffected, null);

            OnSelected(selectedArgs);

            ADXTrace.Instance.TraceInfo(TraceCategory.Application, "End");

            return(string.IsNullOrEmpty(Owner.StaticEntityWrapperTypeName) ? selectResult : CreateEntities(selectResult));
        }
 /// <summary>
 /// Gets the challenge text for a particular ID.
 /// </summary>
 /// <param name="challengeId">The ID of the challenge text to retrieve.</param>
 /// <returns>
 /// The text associated with the specified ID; null if no text exists.
 /// </returns>
 public static string GetChallengeText(Guid challengeId, string objectCacheName)
 {
     return(ObjectCacheManager.GetInstance(objectCacheName).Get(GetChallengeCacheKey(challengeId)) as string);
 }
Esempio n. 7
0
 private ObjectCache GetCache()
 {
     return(ObjectCacheManager.GetInstance(ObjectCacheName));
 }
        protected override IEnumerable EndExecuteSelect(IAsyncResult asyncResult)
        {
            Tracing.FrameworkInformation("CrmDataSourceView", "EndExecuteSelect", "Begin");

            if (_client == null)
            {
                Tracing.FrameworkInformation("CrmDataSourceView", "EndExecuteSelect", "End: _client=null");

                return(null);
            }

            IEnumerable selectResult = null;
            int         rowsAffected = 0;

            try
            {
                SynchronousAsyncSelectResult syncResult = asyncResult as SynchronousAsyncSelectResult;

                if (syncResult != null)
                {
                    Tracing.FrameworkInformation("CrmDataSourceView", "EndExecuteSelect", "syncResult");

                    selectResult = syncResult.SelectResult;
                }
                else
                {
                    Tracing.FrameworkInformation("CrmDataSourceView", "EndExecuteSelect", "EndExecute");

                    var response = _execute.EndInvoke(asyncResult);

                    if (response != null)
                    {
                        var entities = response.Entities;
                        rowsAffected = entities.Count;
                        selectResult = ExecuteSelect(entities).ToList();

                        if (Owner.CacheParameters.Enabled)
                        {
                            var dependencies = GetCacheDependencies(Request.Query, selectResult);

                            string cacheKey = GetCacheKey(Request.Query);

                            // insert into cache
                            ObjectCacheManager.GetInstance().Insert(cacheKey, selectResult, dependencies);
                        }
                    }
                }
            }
            catch (System.Web.Services.Protocols.SoapException ex)
            {
                Tracing.FrameworkError("CrmDataSourceView", "EndExecuteSelect", "{0}\n\n{1}", ex.Detail.InnerXml, ex);
            }
            catch (Exception e)
            {
                Tracing.FrameworkError("CrmDataSourceView", "EndExecuteSelect", "Exception: {0}", e);

                // raise post-event with exception
                CrmDataSourceStatusEventArgs selectedExceptionArgs = new CrmDataSourceStatusEventArgs(0, e);
                OnSelected(selectedExceptionArgs);

                if (!selectedExceptionArgs.ExceptionHandled)
                {
                    throw;
                }

                return(selectResult);
            }
            finally
            {
                _client = null;
            }

            // raise post-event
            CrmDataSourceStatusEventArgs selectedArgs = new CrmDataSourceStatusEventArgs(rowsAffected, null);

            OnSelected(selectedArgs);

            Tracing.FrameworkInformation("CrmDataSourceView", "EndExecuteSelect", "End");

            return(string.IsNullOrEmpty(Owner.StaticEntityWrapperTypeName) ? selectResult : CreateEntities(selectResult));
        }