示例#1
0
 private void ResolveAndValidateAssembly(string bundlePath, List <IAssemblyMetadata> assemblyMetadata)
 {
     assemblyMetadata.RemoveAll(delegate(IAssemblyMetadata item) {
         try
         {
             AssemblyName name3;
             string assemblyFile       = BundleUtility.FindAssemblyFullPath(bundlePath, item.Path, true);
             AssemblyName assemblyName = AssemblyName.GetAssemblyName(assemblyFile);
             if (assemblyName == null)
             {
                 assemblyName = new AssemblyName {
                     Name        = Path.GetFileNameWithoutExtension(assemblyFile),
                     Version     = FrameworkConstants.DEFAULT_VERSION,
                     CultureInfo = FrameworkConstants.DEFAULT_CULTURE
                 };
             }
             item.Path         = assemblyFile;
             item.Version      = assemblyName.Version;
             item.AssemblyName = assemblyName;
             if (this.TryToReplaceWithGlobalAssembly(item, out name3))
             {
                 FileLogUtility.Warn(string.Format(Messages.LocalAssemblyReplacedByGlobal, new object[] { item.AssemblyName, item.Owner.SymbolicName, item.Owner.Version, name3 }));
                 item.IsDuplicatedWithGlobalAssembly = true;
                 item.AssemblyName = name3;
             }
             return(false);
         }
         catch (Exception exception)
         {
             item.Owner.AssembliesFailedToLoad.Add(item);
             FileLogUtility.Error(string.Format(Messages.BundleAssemblyLoadFailed, new object[] { item.Path, item.Owner.SymbolicName, item.Owner.Version, exception.Message }));
             return(true);
         }
     });
 }
示例#2
0
 /// <summary>
 /// Restart the website if the bundle runtime state is still in specified state.
 /// </summary>
 /// <param name="expectedState">Specified state.</param>
 private void StateTimeoutToRestart(BundleRuntimeState expectedState)
 {
     _appStartingTimeout = new Timer(delegate(object state)
     {
         _appStartingTimeout.Dispose();
         if (BundleRuntime.State != (BundleRuntimeState)state)
         {
             FileLogUtility.Warn(string.Format("Fail to start/stop framework. BundleRuntime state is not in '{0}'.", state));
             RestartAppDomain();
             return;
         }
         FileLogUtility.Inform(string.Format("Dectect that the framework is in '{0}' state.", state));
     },
                                     expectedState,
                                     90000,
                                     -1);
 }
示例#3
0
        public object Resolve(Type type)
        {
            //Try to resolve controller by Autofac, which support non-parameter constructor.
            var container = BundleRuntime.Instance.GetFirstOrDefaultService <IContainer>();

            if (container != null)
            {
                try
                {
                    return(container.Resolve(type));
                }
                catch (Exception)
                {
                    FileLogUtility.Warn(string.Format("IOC conatiner can't resolve controller type {0}.", type));
                }
            }

            return(null);
        }
示例#4
0
        public TController Inject <TController>(TController instance)
        { //Try to resolve controller by Autofac, which support non-parameter constructor.
            var container = BundleRuntime.Instance.GetFirstOrDefaultService <IContainer>();

            if (container != null)
            {
                try
                {
                    return(container.InjectProperties(instance));//.Resolve(type);
                }
                catch (Exception ex)
                {
                    FileLogUtility.Warn(string.Format("IOC conatiner can't inject controller type {0}.", instance));
                    FileLogUtility.Warn(ex);
                }
            }

            return(instance);
        }
        /// <summary>
        /// 获取插件的一个布尔值。
        /// </summary>
        /// <param name="bundle">插件。</param>
        /// <param name="key">字符串的Key。</param>
        /// <param name="defaultValue">默认值。</param>
        /// <returns>如果存在且是合法日期,返回配置值,否则,返回默认值。</returns>
        public bool Get(IBundle bundle, string key, bool defaultValue)
        {
            var config = Get(bundle);

            if (config.AppSettings.Settings[key] == null || string.IsNullOrEmpty(config.AppSettings.Settings[key].Value))
            {
                return(defaultValue);
            }

            bool intValue;

            if (bool.TryParse(config.AppSettings.Settings[key].Value, out intValue))
            {
                return(intValue);
            }
            else
            {
                FileLogUtility.Warn(string.Format("The setting '{0}' with value '{1}' in the Bundle '{2}' is not a int value.", key, config.AppSettings.Settings[key].Value, bundle.SymbolicName));
                return(defaultValue);
            }
        }
示例#6
0
 /// <summary>
 /// Restart the website if the bundle runtime state is still in specified state.
 /// </summary>
 /// <param name="expectedState">Specified state.</param>
 private void StateTimeoutToRestart(BundleRuntimeState expectedState)
 {
     _appStartingTimeout = new Timer(
         state =>
     {
         _appStartingTimeout.Dispose();
         if (BundleRuntime.State != (BundleRuntimeState)state)
         {
             // If website is not started within 90 seconds, it will be restarted immediately.
             FileLogUtility.Warn(string.Format("Fail to start/stop framework. BundleRuntime state is not in '{0}'.", state));
             RestartAppDomain();
         }
         else
         {
             // If website is not started within 90 seconds, it will be restarted immediately.
             FileLogUtility.Inform(string.Format("Dectect that the framework is in '{0}' state.", state));
         }
     },
         expectedState,
         StateTimeOut * 1000,
         -1);
 }
示例#7
0
        public bool InstallBundles()
        {
            if (File.Exists(GlobalPersistentFile))
            {
                Persistenter.Load(GlobalPersistentFile);
            }
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();
            List <string> pluginDirectoryList = new List <string>();
            string        path = string.Empty;

            foreach (string str in PluginsDirectoryList)
            {
                path = str;
                if (!Directory.Exists(path))
                {
                    path = PathUtility.GetFullPath(path);
                }
                if (Directory.Exists(path))
                {
                    foreach (string str3 in Directory.GetFiles(path, "Manifest.xml", SearchOption.AllDirectories))
                    {
                        pluginDirectoryList.Add(Directory.GetParent(Path.GetFullPath(str3)).FullName);
                    }
                }
                else
                {
                    FileLogUtility.Warn(string.Format(Messages.PluginsDirectoryNotExist, path, str));
                }
            }
            stopwatch.Stop();
            FileLogUtility.Verbose(string.Format(Messages.TimeSpentForBundlesFound, stopwatch.ElapsedMilliseconds, pluginDirectoryList.Count));
            stopwatch.Start();
            InstallBundles(pluginDirectoryList);
            stopwatch.Stop();
            FileLogUtility.Verbose(string.Format(Messages.TimeSpentForBundleManifestParsed, stopwatch.ElapsedMilliseconds, pluginDirectoryList.Count));
            return(true);
        }
示例#8
0
        public override void OnBegin()
        {
            var data = digester.Peek() as ServiceData;

            if (data == null)
            {
                throw new NotSupportedException("the top element must be an instance of ServiceData,but it is " + base.digester.Peek().GetType().ToString());
            }
            var symbolicName = string.Empty;

            if (digester.Root is BundleData)
            {
                symbolicName = (digester.Root as BundleData).SymbolicName;
            }
            foreach (string str2 in digester.Attributes.Keys)
            {
                string str3 = digester.Attributes[str2];
                string str4 = str2.ToLower();
                if (str4 != null)
                {
                    if (str4 != "interface")
                    {
                        if (str4 != "type")
                        {
                            goto Label_00F2;
                        }
                        data.Type = str3;
                    }
                    else
                    {
                        data.Interfaces = str3.Split(new char[] { ';' });
                    }
                    continue;
                }
Label_00F2:
                FileLogUtility.Warn(string.Format(Messages.ManifestSectionNotRecognized, str2, base.digester.ElementName, symbolicName));
            }
        }
        /// <summary>
        /// 处理异常。
        /// </summary>
        /// <param name="context">HttpContext。</param>
        /// <param name="requestType">请求类型。</param>
        /// <param name="e">异常。</param>
        /// <returns>IHttpHandler实例,或者直接抛出异常。</returns>
        private IHttpHandler HandleException(HttpContext context, string requestType, Exception e)
        {
            FileLogUtility.Error(string.Format("Log the exception when compiling the request page '{0}'.", context.Request.Path));
            FileLogUtility.Error(e);
            context.Application["ErrorMessage"] = e.Message;
            IHttpHandler result;

            try
            {
                IHttpHandler handler = base.GetHandler(context, requestType, ErrorHandlerPage, "");
                FileLogUtility.Warn(string.Format("Return the Error Handler page when failed to get the request page '{0}'.", context.Request.Path));
                result = handler;
            }
            catch (Exception ex)
            {
                FileLogUtility.Error(
                    string.Format("Failed to get Error Handler page '{0}' when getting the request page '{1}' failed.", ErrorHandlerPage,
                                  context.Request.Path));
                FileLogUtility.Error(ex);
                throw e;
            }
            return(result);
        }
示例#10
0
        /// <summary>
        /// 将插件的一个ASP.NET页面编译并构建成一个IHttpHandler实例。
        /// </summary>
        /// <param name="context">HttpContext。</param>
        /// <param name="requestType">请求类型。</param>
        /// <param name="virtualPath">页面虚拟路径。</param>
        /// <param name="path">页面物理路径。</param>
        /// <returns>IHttpHandler实例。</returns>
        public override IHttpHandler GetHandler(HttpContext context, string requestType, string virtualPath, string path)
        {
            BundleRuntime instance = BundleRuntime.Instance;

            if (instance.State != BundleRuntimeState.Started)
            {
                try
                {
                    FileLogUtility.Debug(string.Format("Framework is not in 'Started' state when access page '{0}'.", path));
                    return(base.GetHandler(context, requestType, FrameworkBusyHandlerPage, ""));
                }
                catch (Exception ex)
                {
                    FileLogUtility.Warn("Failed to redirect framework Busy Handler page when Framework is not in 'Started'.");
                    FileLogUtility.Warn(ex);
                }
                return(null);
            }
            string value = string.Empty;
            IBundleRuntimeHttpHost bundleRuntimeHttpHost = (IBundleRuntimeHttpHost)context.ApplicationInstance;
            BundleData             bundleData            = bundleRuntimeHttpHost.BundleRuntime.GetFirstOrDefaultService <IBundleInstallerService>()
                                                           .FindBundleContainPath(Directory.GetParent(path).FullName);

            if (bundleData != null)
            {
                value = bundleData.SymbolicName;
            }
            if (string.IsNullOrEmpty(value))
            {
                FileLogUtility.Debug(string.Format(
                                         "Failed to get the bundle contains requested page '{0}' and just compile this page into IHttpHandler. Just compile the page directly.",
                                         path));
                return(SafelyGetHandler(context, requestType, virtualPath, path));
            }
            IBundle bundle = bundleRuntimeHttpHost.BundleRuntime.Framework.GetBundle(bundleData.Path);

            if (bundle == null)
            {
                return(SafelyGetHandler(context, requestType, virtualPath, path));
            }
            FileLogUtility.Debug(string.Format("The bundle state of requested page '{0}' is '{1}'.", path, bundle.State));
            switch (bundle.State)
            {
            case BundleState.Installed:
            case BundleState.Resolved:
            {
                object syncObject;
                Monitor.Enter(syncObject = _syncObject);
                try
                {
                    bundle.Start(BundleStartOptions.General);
                    bundleRuntimeHttpHost.AddReferencedAssemblies(bundleData.SymbolicName);
                }
                finally
                {
                    Monitor.Exit(syncObject);
                }
                return(SafelyGetHandler(context, requestType, virtualPath, path));
            }

            case BundleState.Starting:
            {
                object syncObject2;
                Monitor.Enter(syncObject2 = _syncObject);
                try
                {
                    bundleRuntimeHttpHost.AddReferencedAssemblies(bundleData.SymbolicName);
                }
                finally
                {
                    Monitor.Exit(syncObject2);
                }
                return(SafelyGetHandler(context, requestType, virtualPath, path));
            }

            case BundleState.Active:
                return(SafelyGetHandler(context, requestType, virtualPath, path));

            case BundleState.Stopping:
                return(HandleException(context, requestType, new HttpException("Access denied, for the bundle is stopping.")));

            case BundleState.Uninstalled:
                return(HandleException(context, requestType, new HttpException("Access denied, for the bundle is uninstalled.")));

            default:
                throw new NotSupportedException();
            }
        }
示例#11
0
        private void EventManager_FrameworkStateChanged(object sender, FrameworkEventArgs e)
        {
            var target = e.Target as IBundleContext;

            if (target == null)
            {
                var bundle = e.Target as IBundle;
                if (bundle != null)
                {
                    target = bundle.Context;
                }
            }
            switch (e.EventType)
            {
            case FrameworkEventType.Starting:
                OnFrameworkStarting();
                return;

            case FrameworkEventType.Started:
                OnFrameworkStarted();
                return;

            case FrameworkEventType.Error:
                if (e.Data != null)
                {
                    if (!(e.Data is Exception))
                    {
                        break;
                    }
                    FileLogUtility.Error(e.Data as Exception);
                    return;
                }
                if (target != null)
                {
                    FileLogUtility.Error(string.Format(Messages.BundleThrownFrameworkError, target.Bundle.SymbolicName, target.Bundle.Version));
                    return;
                }
                FileLogUtility.Error(string.Format(Messages.ObjectThrownFrameworkError, e.Target));
                return;

            case FrameworkEventType.Warning:
                if (e.Data != null)
                {
                    if (e.Data is Exception)
                    {
                        FileLogUtility.Warn(e.Data as Exception);
                        return;
                    }
                    break;
                }
                if (target != null)
                {
                    FileLogUtility.Warn(string.Format(Messages.BundleThrownFrameworkWarning, target.Bundle.SymbolicName, target.Bundle.Version));
                    return;
                }
                FileLogUtility.Warn(string.Format(Messages.ObjectThrownFrameworkWarning, e.Target));
                return;

            case FrameworkEventType.Info:
            case FrameworkEventType.Stopping:
                break;

            case FrameworkEventType.Stopped:
                OnFrameworkStop();
                break;

            default:
                return;
            }
        }
示例#12
0
        public override void OnBegin()
        {
            object obj2         = digester.Peek();
            string symbolicName = string.Empty;

            if (obj2 is BundleData)
            {
                symbolicName = base.digester.Attributes["SymbolicName"];
            }
            else if (base.digester.Root is BundleData)
            {
                symbolicName = (base.digester.Root as BundleData).SymbolicName;
            }
            using (IEnumerator enumerator = base.digester.Attributes.Keys.GetEnumerator())
            {
Label_0071:
                if (!enumerator.MoveNext())
                {
                    return;
                }
                string       current  = (string)enumerator.Current;
                string       str3     = base.digester.Attributes[current];
                PropertyInfo property = obj2.GetType().GetProperty(current, BindingFlags.Public | BindingFlags.Instance);
                if (property == null)
                {
                    string name = char.ToUpper(current[0]) + current.Substring(1);
                    property = obj2.GetType().GetProperty(name, BindingFlags.Public | BindingFlags.Instance);
                }
                if (property == null)
                {
                    goto Label_0191;
                }
                object obj3 = null;
                if (property.PropertyType.IsEnum)
                {
                    try
                    {
                        obj3 = Enum.Parse(property.PropertyType, str3, true);
                    }
                    catch (Exception exception)
                    {
                        FileLogUtility.Error(exception);
                    }
                }
                else
                {
                    try
                    {
                        obj3 = TypeConverterUtility.ConvertTo(str3, property.PropertyType);
                    }
                    catch (Exception exception2)
                    {
                        FileLogUtility.Error(exception2);
                    }
                }
                goto Label_01B2;
Label_013B:
                if (!string.IsNullOrEmpty(str3))
                {
                    FileLogUtility.Warn(string.Format(Messages.ManifestSectionInvalid, new object[] { str3, current, base.digester.ElementName, symbolicName }));
                    goto Label_0071;
                }
Label_0181:
                property.SetValue(obj2, obj3, null);
                goto Label_0071;
Label_0191:
                FileLogUtility.Warn(string.Format(Messages.ManifestSectionNotRecognized, current, base.digester.ElementName, symbolicName));
                goto Label_0071;
Label_01B2:
                if (obj3 != null)
                {
                    goto Label_0181;
                }
                goto Label_013B;
            }
        }
示例#13
0
        /// <summary>
        /// 将Xml节点转换成导航节点对象。
        /// </summary>
        /// <param name="nav">所属Navigation。</param>
        /// <param name="node">Xml节点。</param>
        /// <param name="Parent">父导航节点。</param>
        /// <param name="children">子导航节点列表。</param>
        private void ConvertToNode(NavigationNode parent, XmlNode node, IBundle bundle)
        {
            // 获取Id、Name、Value、Order、Permisson、Icon和Tooltip属性。
            string id   = GetAttribute(node, "Id");
            string name = GetAttribute(node, "Name");

            if (string.IsNullOrEmpty(name))
            {
                FileLogUtility.Warn("The Name attribute can not be empty for UIShell.NavigationService extension.");
                return;
            }
            string value      = GetAttribute(node, "Value");
            string order      = GetAttribute(node, "Order");
            float  orderFloat = 0;

            float.TryParse(order, out orderFloat);
            string permission = GetAttribute(node, "Permission");
            string icon       = GetAttribute(node, "Icon");
            string toolTip    = GetAttribute(node, "Tooltip");
            string parentId   = GetAttribute(node, "ParentId");
            // 创建导航节点。
            var navNode = new NavigationNode {
                ParentId   = string.IsNullOrEmpty(parentId) ? Guid.NewGuid().ToString() : parentId,
                Id         = string.IsNullOrEmpty(id) ? Guid.NewGuid().ToString() : id,
                Name       = name,
                Order      = orderFloat,
                Value      = value,
                Permission = permission,
                Icon       = icon,
                ToolTip    = toolTip,
                Bundle     = bundle
            };

            // 设置父节点,并添加到子节点列表。

            if (!string.IsNullOrEmpty(parentId))
            {
                PendingAppendedNodes.Add(navNode);
            }
            else if (string.IsNullOrEmpty(parentId))
            {
                if (parent != null)
                {
                    navNode.Parent = parent;
                    parent.Children.Add(navNode);
                }
                else
                {
                    navNode.Parent   = null;
                    navNode.ParentId = Guid.NewGuid().ToString();
                    NavigationNodeList.Add(navNode);
                }
            }

            // 将XML节点其它的属性保存到Attributes字典。
            foreach (XmlAttribute attr in node.Attributes)
            {
                if (attr.Name.Equals("Id") || attr.Name.Equals("Name") || attr.Name.Equals("Value") || attr.Name.Equals("Order") || attr.Name.Equals("Permission") || attr.Name.Equals("Icon") || attr.Name.Equals("ToolTip"))
                {
                    continue;
                }
                navNode.Attributes[attr.Name] = attr.Value;
            }
            // 遍历Xml子节点,并递归转换成导航节点。
            foreach (XmlNode childnode in node.ChildNodes)
            {
                if (childnode is XmlComment)
                {
                    continue;
                }
                ConvertToNode(navNode, childnode, bundle);
            }
        }