Example #1
0
        static TypeDefine checkRemotingName(string remoteName)
        {
            TypeDefine pageDefine = null;

            if (ExistTypes.ContainsKey(remoteName))
            {
                pageDefine = ExistTypes[remoteName];
            }
            else
            {
                Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();

                for (int i = 0; i < assemblies.Length; i++)
                {
                    var type = assemblies[i].GetType(remoteName);
                    if (type != null)
                    {
                        pageDefine = new ScriptRemoting.TypeDefine();
                        pageDefine.ControllerType = type;
                        break;
                    }
                }
                if (pageDefine == null)
                {
                    throw new Exception("无法找到" + remoteName + "类定义");
                }
                if (pageDefine.ControllerType != RemotingPageType && pageDefine.ControllerType.GetTypeInfo().IsSubclassOf(RemotingPageType) == false)
                {
                    throw new Exception(remoteName + "不是RemotingPage的子类");
                }
                if (pageDefine.ControllerType.GetTypeInfo().IsPublic == false)
                {
                    throw new Exception($"{pageDefine.ControllerType.FullName}不是public");
                }
                MethodInfo[] methods = pageDefine.ControllerType.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic);
                foreach (MethodInfo m in methods)
                {
                    if (m.GetCustomAttribute <RemotingMethodAttribute>() != null)
                    {
                        pageDefine.Methods.Add(m);
                    }
                }
                try
                {
                    ExistTypes[remoteName] = pageDefine;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            return(pageDefine);
        }
Example #2
0
        void handleInit(MessageBag msgBag)
        {
            try
            {
                bool       outputRSAKey = false;
                string     remoteName   = msgBag.ClassFullName;
                TypeDefine pageDefine   = checkRemotingName(remoteName);

                List <MethodDefineForJs> methodDefineForOutput = new List <MethodDefineForJs>();

                foreach (MethodInfo method in pageDefine.Methods)
                {
                    var parameters = method.GetParameters();
                    RemotingMethodAttribute methodAttr = (RemotingMethodAttribute)method.GetCustomAttribute(typeof(RemotingMethodAttribute));
                    var mItem = new MethodDefineForJs()
                    {
                        Method            = method.Name,
                        ParameterLength   = parameters.Length,
                        EncryptParameters = methodAttr.UseRSA.HasFlag(RSAApplyScene.EncryptParameters),
                        EncryptResult     = methodAttr.UseRSA.HasFlag(RSAApplyScene.EncryptResult),
                    };
                    if (mItem.EncryptResult || mItem.EncryptParameters)
                    {
                        outputRSAKey = true;
                    }
                    methodDefineForOutput.Add(mItem);
                }

                //RemotingController currentPage = (RemotingController)Activator.CreateInstance(pageDefine.ControllerType);
                //currentPage.Session = this.Session;
                //currentPage.onLoad();
                if (outputRSAKey)
                {
                    CreateRSAKey(this.Session);
                }
                mSendDataFunc(Newtonsoft.Json.JsonConvert.SerializeObject(new
                {
                    methods   = methodDefineForOutput,
                    SessionID = this.Session.SessionID,
                    rsa       = outputRSAKey ? new { Exponent = this.Session["$$_rsa_PublicKeyExponent"], Modulus = this.Session["$$_rsa_PublicKeyModulus"] } : null,
                }));
            }
            catch (Exception ex)
            {
                mSendDataFunc(Newtonsoft.Json.JsonConvert.SerializeObject(new
                {
                    err = ex.ToString(),
                }));
            }
        }