Ejemplo n.º 1
0
        public IAsyncResult InvokeBegin(object instance, object[] inputs, AsyncCallback callback, object state)
        {
            string cacheKey = this.CreateCacheKey(inputs);

            CachedOperationResult cachedItem       = CacheServiceHelper.Current.Get <CachedOperationResult>(cacheKey);
            CachingUserState      cachingUserState = new CachingUserState
            {
                CacheItem = cachedItem,
                CacheKey  = cacheKey,
                Callback  = callback,
                State     = state
            };

            IAsyncResult originalAsyncResult;

            if (cachedItem != null)
            {
                InvokerDelegate invoker = cachedItem.GetValue;

                object[] notUsed;
                originalAsyncResult = invoker.BeginInvoke(inputs, out notUsed, this.InvokerCallback, cachingUserState);
            }
            else
            {
                originalAsyncResult = _invoker.InvokeBegin(instance, inputs, this.InvokerCallback, cachingUserState);
            }

            return(new CachingAsyncResult(originalAsyncResult, cachingUserState));
        }
        public object InvokeEnd(object instance, out object[] outputs, IAsyncResult asyncResult)
        {
            CachingAsyncResult cachingAsyncResult = asyncResult as CachingAsyncResult;
            CachingUserState   cachingUserState   = cachingAsyncResult.CachingUserState;

            if (cachingUserState.CacheItem == null)
            {
                object result = this.originalInvoker.InvokeEnd(instance, out outputs, cachingAsyncResult.OriginalAsyncResult);
                cachingUserState.CacheItem = new CachedResult {
                    ReturnValue = result, Outputs = outputs
                };
#if Webhosted
                this.GetCache().Insert(cachingUserState.CacheKey, cachingUserState.CacheItem, null, Cache.NoAbsoluteExpiration, TimeSpan.FromSeconds(this.cacheDuration));
#else
                this.GetCache().Add(cachingUserState.CacheKey, cachingUserState.CacheItem, DateTimeOffset.UtcNow.Add(TimeSpan.FromSeconds(this.cacheDuration)));
#endif
                return(result);
            }
            else
            {
                InvokerDelegate invoker = ((System.Runtime.Remoting.Messaging.AsyncResult)cachingAsyncResult.OriginalAsyncResult).AsyncDelegate as InvokerDelegate;
                invoker.EndInvoke(out outputs, cachingAsyncResult.OriginalAsyncResult);
                return(cachingUserState.CacheItem.ReturnValue);
            }
        }
        public IAsyncResult InvokeBegin(object instance, object[] inputs, AsyncCallback callback, object state)
        {
#if Webhosted
            Cache cache = GetCache();
#else
            ObjectCache cache = this.GetCache();
#endif
            string           cacheKey         = this.CreateCacheKey(inputs);
            CachedResult     cacheItem        = cache[cacheKey] as CachedResult;
            CachingUserState cachingUserState = new CachingUserState
            {
                CacheItem            = cacheItem,
                CacheKey             = cacheKey,
                OriginalUserCallback = callback,
                OriginalUserState    = state
            };

            IAsyncResult originalAsyncResult;
            if (cacheItem != null)
            {
                InvokerDelegate invoker = cacheItem.GetValue;
                object[]        dummy;
                originalAsyncResult = invoker.BeginInvoke(inputs, out dummy, this.InvokerCallback, cachingUserState);
            }
            else
            {
                originalAsyncResult = this.originalInvoker.InvokeBegin(instance, inputs, this.InvokerCallback, cachingUserState);
            }

            return(new CachingAsyncResult(originalAsyncResult, cachingUserState));
        }
Ejemplo n.º 4
0
        private static T AdaptToHost <T>(AddInToken pipeline, IContract addInContract)
        {
            if (addInContract == null)
            {
                throw new ArgumentNullException("addInContract");
            }
            System.Diagnostics.Contracts.Contract.EndContractBlock();

            ContractComponent contract    = pipeline._contract;
            HostAdapter       hostAdapter = pipeline._hostAdapter;

            Type hostAdapterType;
            Type contractType;

            LoadContractAndHostAdapter(contract, hostAdapter, out contractType, out hostAdapterType);

            int?temporaryToken = null;

            try
            {
                temporaryToken = addInContract.AcquireLifetimeToken();

                // This assembly resolve event exists to:
                //    1) To upgrade the contract assembly from the LoadFrom context to
                //       the default loader context
                ResolverHelper      resolver         = new ResolverHelper(pipeline);
                ResolveEventHandler assemblyResolver = new ResolveEventHandler(resolver.ResolveAssemblyForHostAdapter);
                AppDomain.CurrentDomain.AssemblyResolve += assemblyResolver;

                // Create the Host Adapter, which is a subclass of the Host Add-In View.
                // Pass the contract implementation to this constructor, using a
                // transparent proxy to the real type.  Detect common errors here.
                InvokerDelegate myInvokerDelegate = CreateConsInvoker(hostAdapterType, contractType);

                T hav;
                try
                {
                    hav = (T)myInvokerDelegate(addInContract);
                }
                catch (ArgumentException e)
                {
                    CheckForLoaderContextProblems(contract, pipeline._addinAdapter, e);
                    throw;
                }
                finally
                {
                    AppDomain.CurrentDomain.AssemblyResolve -= assemblyResolver;
                }

                return(hav);
            }
            finally
            {
                if (temporaryToken != null && addInContract != null)
                {
                    addInContract.RevokeLifetimeToken((int)temporaryToken);
                }
            }
        }
Ejemplo n.º 5
0
        public void BeginInvoke()
        {
            this.invoker = PrintTime;
            invoker += Statistics;

            while (true)
            {
                invoker();
                this.calls++;
                Thread.Sleep(this.T*1000);
            }
        }
Ejemplo n.º 6
0
        internal static T ActivateHostAdapter <T>(PartialToken pipeline, IContract addIn)
        {
            if (pipeline == null)
            {
                throw new ArgumentNullException("pipeline");
            }
            if (addIn == null)
            {
                throw new ArgumentNullException("addIn");
            }
            System.Diagnostics.Contracts.Contract.EndContractBlock();

            ContractComponent contract    = pipeline._contract;
            HostAdapter       hostAdapter = pipeline._hostAdapter;

            Type hostAdapterType;
            Type contractType;

            LoadContractAndHostAdapter(contract, hostAdapter, out contractType, out hostAdapterType);

            // This assembly resolve event exists to:
            //    1) To upgrade the contract assembly from the LoadFrom context to
            //       the default loader context
            ResolverHelper      resolver         = new ResolverHelper(contract);
            ResolveEventHandler assemblyResolver = new ResolveEventHandler(resolver.ResolveAssemblyForHostAdapter);

            AppDomain.CurrentDomain.AssemblyResolve += assemblyResolver;

            T hav;

            InvokerDelegate myInvokerDelegate = CreateConsInvoker(hostAdapterType, contractType);

            try
            {
                hav = (T)myInvokerDelegate(addIn);
            }
            catch (ArgumentException e)
            {
                CheckForLoaderContextProblems(contract, pipeline._addinAdapter, e);
                throw;
            }
            finally
            {
                AppDomain.CurrentDomain.AssemblyResolve -= assemblyResolver;
            }

            return(hav);
        }
Ejemplo n.º 7
0
        public object InvokeEnd(object instance, out object[] outputs, IAsyncResult asyncResult)
        {
            CachingAsyncResult result = asyncResult as CachingAsyncResult;
            CachingUserState   state  = result.CachingUserState;

            if (state.CacheItem == null)
            {
                object data = _invoker.InvokeEnd(instance, out outputs, result.OriginalAsyncResult);
                state.CacheItem = new CachedOperationResult {
                    Data = data, Outputs = outputs
                };

                CacheServiceHelper.Current.Add(state.CacheKey, state.CacheItem, TimeSpan.FromSeconds(_cacheDuration));

                return(data);
            }
            else
            {
                InvokerDelegate invoker = ((AsyncResult)result.OriginalAsyncResult).AsyncDelegate as InvokerDelegate;
                invoker.EndInvoke(out outputs, result.OriginalAsyncResult);

                return(state.CacheItem.Data);
            }
        }