private IContentLibraryMultimediaItem GetEclItem(string eclStubComponentId, out IContentLibraryContext eclContext)
        {
            _log.Debug("Retrieving ECL item for ECL Stub Component: " + eclStubComponentId);
            IEclUri eclUri = _eclSession.TryGetEclUriFromTcmUri(eclStubComponentId);

            if (eclUri == null)
            {
                throw new Exception("Unable to get ECL URI for ECL Stub Component: " + eclStubComponentId);
            }

            eclContext = _eclSession.GetContentLibrary(eclUri);
            // This is done this way to not have an exception thrown through GetItem, as stated in the ECL API doc.
            // The reason to do this, is because if there is an exception, the ServiceChannel is going into the aborted state.
            // GetItems allows up to 20 (depending on config) connections.
            IList <IContentLibraryItem>   eclItems = eclContext.GetItems(new[] { eclUri });
            IContentLibraryMultimediaItem eclItem  = (eclItems == null) ? null : eclItems.OfType <IContentLibraryMultimediaItem>().FirstOrDefault();

            if (eclItem == null)
            {
                eclContext.Dispose();
                throw new Exception(string.Format("ECL item '{0}' not found (TCM URI: '{1}')", eclUri, eclStubComponentId));
            }

            _log.Debug(string.Format("Retrieved ECL item for ECL Stub Component '{0}': {1}", eclStubComponentId, eclUri));
            return(eclItem);
        }
        protected virtual string GetECLUrl(string uri)
        {
            Component c = (Component)engine.GetObject(uri);

            if (c == null)
            {
                throw new Exception(string.Format("Error loading item with uri {0}", uri));
            }

            log.Debug(System.Threading.Thread.CurrentThread.ManagedThreadId + ": Instantiating a new ECL Session");
            using (IEclSession localSession = SessionFactory.CreateEclSession(engine.GetSession()))
            {
                IEclUri eclUri = localSession.TryGetEclUriFromTcmUri(uri);
                if (eclUri != null) // this is an ECL item
                {
                    log.Debug(System.Threading.Thread.CurrentThread.ManagedThreadId + ": Fetching IContentLibraryContext");
                    using (IContentLibraryContext context = localSession.GetContentLibrary(eclUri))
                    {
                        try
                        {
                            IContentLibraryMultimediaItem item = null;

                            // This is done this way to not have an exception thrown through GetItem, as stated
                            // in the API doc.
                            // The reason to do this, is because if there is an exception,
                            // the ServiceChannel is going into the aborted state...
                            log.Debug(System.Threading.Thread.CurrentThread.ManagedThreadId + ": Get Items");

                            // GetItems allows up to 20 (depending on config) connections.
                            // After that any new connection is aborted / not created.

                            var items = context.GetItems(new IEclUri[] { eclUri });

                            if (items != null && items.Count == 1)
                            {
                                item = (IContentLibraryMultimediaItem)items.First();
                                log.Debug(System.Threading.Thread.CurrentThread.ManagedThreadId + ": Item Fetched");
                            }

                            if (item == null)
                            {
                                log.Warning(System.Threading.Thread.CurrentThread.ManagedThreadId + ": Item with ECL URI: " + eclUri + " not found. This MM item is used in: " + c.Id);
                                throw new Exception(string.Format("ECL item not found (ecl uri = {0}, tcm uri = {1}", eclUri, c.Id));
                            }
                            string distributionUrl = item.GetDirectLinkToPublished(null);
                            string result          = distributionUrl.ToLower();
                            if (!string.IsNullOrEmpty(result))
                            {
                                log.Debug(System.Threading.Thread.CurrentThread.ManagedThreadId + ": Returning: " + result);
                                return(result);
                            }
                        }
                        finally
                        {
                            log.Debug(System.Threading.Thread.CurrentThread.ManagedThreadId + ": Going out of the context using block statement.");
                        }
                    }
                }
            }
            return(string.Empty);
        }