Example #1
0
        /// <summary>
        /// 加载插件信息
        /// </summary>
        private void LoadExtendConfig()
        {
            if (!File.Exists("Services.config"))
            {
                return;
            }

            try
            {
                XDocument doc     = XDocument.Load("Services.config");
                XElement  element = doc.Root.Element("ExtendAssemblys");
                if (element != null)
                {
                    foreach (var ae in element.Elements("Assembly"))
                    {
                        ExtendAssembly assembly = new ExtendAssembly();
                        ExtendAssemblys.Add(assembly);
                        assembly.Name      = ae.Attribute("Name").Value;
                        assembly.FileName  = ae.Attribute("FileName").Value;
                        assembly.IsEnabled = bool.Parse(ae.Attribute("IsEnabled").Value);
                        int type = int.Parse(ae.Attribute("Type").Value);
                        assembly.Type = (AssemblyType)type;
                    }
                }
            }
            catch (Exception e)
            {
                logger.Fatal("读取外部程序集异常!", e);
            }
        }
Example #2
0
 /// <summary>
 /// 停止插件运行
 /// </summary>
 /// <param name="assembly">要移除插件</param>
 private void StopAssembly(ExtendAssembly assembly)
 {
     try
     {
         //关闭服务
         foreach (ThreadServiceHost host in assembly.ServiceHosts)
         {
             host.Stop();
         }
     }
     catch (Exception ex)
     {
         logger.Fatal(ex);
     }
 }
Example #3
0
        /// <summary>
        /// 运行插件
        /// </summary>
        /// <param name="assembly">插件</param>
        private void StartAssembly(ExtendAssembly assembly)
        {
            try
            {
                if (assembly.Type == AssemblyType.ServiceAssembly)
                {
                    //将外部程序集载入
                    assembly.Assembly = System.Reflection.Assembly.LoadFrom(assembly.FileName);
                    //服务库初始化
                    foreach (Type type in assembly.Assembly.GetTypes())
                    {
                        System.Reflection.MethodInfo isCallbackServiceMethod = null;
                        System.Reflection.MethodInfo getName = null;
                        Object obj = null;
                        try
                        {
                            getName = type.GetMethod("GetName");
                            isCallbackServiceMethod = type.GetMethod("IsCallbackService");
                            if (getName != null)
                            {
                                obj = Activator.CreateInstance(type);
                            }
                            else
                            {
                                continue;
                            }
                        }
                        catch (Exception ex)
                        {
                            logger.Fatal("加载服务插件", ex);
                            continue;
                        }

                        string      httpUri   = "http://" + _ServerIP + ":" + _basicHttpPort.ToString() + "/" + type.Name;
                        string      netTcpUri = "net.tcp://" + _ServerIP + ":" + _netTcpPort.ToString() + "/" + type.Name;
                        ServiceHost host      = GetHost(type, new Uri[] { new Uri(httpUri), new Uri(netTcpUri) });

                        XmlDictionaryReaderQuotas quotas = new XmlDictionaryReaderQuotas()
                        {
                            MaxDepth = int.MaxValue,
                            MaxStringContentLength = int.MaxValue,
                            MaxArrayLength         = int.MaxValue,
                            MaxBytesPerRead        = int.MaxValue,
                            MaxNameTableCharCount  = int.MaxValue
                        };

                        BasicHttpBinding basicHttpBinding = new BasicHttpBinding()
                        {
                            ReaderQuotas           = quotas,
                            MaxReceivedMessageSize = int.MaxValue
                        };

                        NetTcpBinding netTcpBinding = new NetTcpBinding()
                        {
                            ReaderQuotas           = quotas,
                            MaxBufferSize          = int.MaxValue,
                            MaxBufferPoolSize      = int.MaxValue,
                            MaxReceivedMessageSize = int.MaxValue,
                            PortSharingEnabled     = true,
                            Security = new NetTcpSecurity {
                                Mode = SecurityMode.None
                            }
                        };

                        string name = getName.Invoke(obj, null) as string;
                        bool   isCallbackService = (bool)isCallbackServiceMethod.Invoke(obj, null);

                        if (type.GetCustomAttributes(typeof(ServiceContractAttribute), true).Length > 0)
                        {
                            if (!isCallbackService)
                            {
                                host.AddServiceEndpoint(type, basicHttpBinding, "");
                            }
                            if (!_isBasicHttpOnly)
                            {
                                host.AddServiceEndpoint(type, netTcpBinding, "");
                            }
                        }

                        if (!assembly.ServiceHosts.Any(a => a.ServiceName == name))
                        {
                            assembly.ServiceHosts.Add(new ThreadServiceHost(name));
                        }

                        var threadServiceHost = assembly.ServiceHosts.Single(a => a.ServiceName == name);

                        if (!threadServiceHost.IsRunning)
                        {
                            threadServiceHost.Start(host);
                        }

                        if (isCallbackService)
                        {
                            type.GetMethod("StartListenClients").Invoke(null, null);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Fatal(assembly.Name + " 启动异常\r\n", ex);

                System.Windows.MessageBox.Show(assembly.Name + " 启动异常," + ex.Message, "提示");
            }
        }