Ejemplo n.º 1
0
        public void CastEvent <T>(object sender, RemoteEventArgs <T> args) where T : class, new()
        {
            IEventOC        eventOC    = ServiceTaker.GetRemoteService <IEventOC>();
            IJsonSerializer serializer = ServiceTaker.GetService <IJsonSerializer>();

            EventDC evDc = new EventDC();

            evDc.EventKey  = Guid.NewGuid().ToString();
            evDc.EventName = args.EventName;
            evDc.Message.TypeQualifiedName = args.Message.GetType().AssemblyQualifiedName;
            evDc.Message.JsonValue         = serializer.Serialize(args.Message);

            Type senderType      = sender.GetType();
            Type senderInterface = null;

            Type[] theInterfaces = senderType.GetInterfaces();
            if (theInterfaces.Length == 1)
            {
                senderInterface = theInterfaces[0];
            }
            else
            {
                senderInterface = theInterfaces.Where(ww => ww.GetEvents().Select(ss => ss.Name).Contains(args.EventName)).FirstOrDefault();
            }

            evDc.SenderInterfaceName = senderInterface == null ? string.Empty : senderInterface.FullName;
            evDc.SenderTypeName      = senderType.FullName;

            eventOC.CastRemoteEvent(evDc);
        }
Ejemplo n.º 2
0
        public void RegisterTypeMapFromConfigDB(List <TypeMap> mapList)
        {
            var            config = ServiceTaker.GetService <IConfiger>();
            List <TypeMap> listTM = config.GetMulti <TypeMap>(typeof(TypeMap).FullName);

            foreach (var aMap in listTM)
            {
                lock (mapLoker)
                {
                    mapList.Remove(aMap);
                    mapList.Add(aMap);
                }
            }
        }
Ejemplo n.º 3
0
        public static DbProviderInfo GetProviderInfo(string DbContextName)
        {
            if (LocalConfig.providerInfoList == null || LocalConfig.providerInfoList.Count < 1)
            {
                LocalConfig.GetProviderFromFile();
            }
            DbProviderInfo dbProviderInfo = (
                from ww in LocalConfig.providerInfoList
                where ww.DbContextName == DbContextName
                select ww).FirstOrDefault <DbProviderInfo>();

            if (dbProviderInfo == null)
            {
                IConfiger service = ServiceTaker.GetService <IConfiger>(new object[0]);
                dbProviderInfo = service.GetFirst <DbProviderInfo>(DbContextName);
                LocalConfig.providerInfoList.Remove(dbProviderInfo);
                LocalConfig.providerInfoList.Add(dbProviderInfo);
            }
            return(dbProviderInfo);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 远程事件的播送方法。
        /// </summary>
        /// <param name="sender">发送者。这个参数用于计算响应者路由但不会发往远程响应端。</param>
        /// <param name="args">参数,参数将传弟到响应端。要发给远程响应端的数据必须放在这个参数中。</param>
        public void CastEvent <T>(object sender, RemoteEventArgs <T> args) where T : class, new()
        {
            Type    senderType = sender.GetType();
            EventDC evData     = new EventDC();

            evData.EventKey = Guid.NewGuid().ToString("N");

            if (senderType.GetEvent(args.EventName) != null || senderType.GetEvent(args.EventName).EventHandlerType.FullName == typeof(RemoteEventHandler <>).FullName)
            {
                evData.EventName = args.EventName;
            }
            else
            {
                throw new Exception("不存在RemoteEventHandler型事件" + evData.EventName);
            }

            Type[]      interfaces           = senderType.GetInterfaces();
            List <Type> inheritInterfaceList = interfaces.Where(ww => (ww.GetMembers().Select(ss => ss.Name)).Contains(args.EventName)).ToList();

            if (inheritInterfaceList.Count != 1)
            {
                throw new Exception("不存在或多于一个事件发送者所继承的接口中声名事件" + evData.EventName);
            }
            else
            {
                evData.SenderInterfaceName = inheritInterfaceList.First().FullName;
            }

            ParameterDC Message = new ParameterDC();

            Message.TypeQualifiedName = args.Message.GetType().AssemblyQualifiedName;
            Message.JsonValue         = ServiceTaker.GetService <IJsonSerializer>().Serialize(args.Message);

            evData.Message        = Message;
            evData.SenderTypeName = senderType.FullName;

            IEventOC evOperation = ServiceTaker.GetRemoteService <IEventOC>();

            evOperation.CastRemoteEvent(evData);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 运行方法
        /// </summary>
        /// <returns></returns>
        public InvokeResult Invoke()
        {
            object Result = null;

            object theApi = ServiceTaker.GetServiceByKey(theContract.InterfaceName, theContract.ApiConfigKey);

            List <object> paramList  = GetParamList(theContract);
            var           theType    = theApi.GetType();
            int           generic    = theContract.MethodName.IndexOf("<");
            var           methodName = generic >= 0 ?
                                       theContract.MethodName.Substring(0, generic) : theContract.MethodName;

            var methods = theType.GetMethods().Where(m => m.Name == methodName);


            //如果找不到方法
            if (methods.Count() == 0)
            {
                throw new Exception("找不到目标方法");
            }

            var method = GetMethod(methods, theContract.TypeArguments, paramList);

            if (method == null)
            {
                throw new Exception("未能找到方法名:" + methodName + "接口名称:" + theContract.InterfaceName);
            }
            if (!method.IsPublic)
            {
                throw new Exception("目标方法不是公有的");
            }

            if (method.IsAbstract) //如果是抽象方法。
            {
                throw new Exception("目标方法是抽象方法");
            }

            InvokeResult theRes = new InvokeResult();

            if (generic < 0)
            {
                Result            = method.Invoke(theApi, paramList.ToArray());
                theRes.ResultType = method.ReturnType;
            }
            else
            {
                MethodInfo methodInfo;
                Type[]     types = null;
                if (theContract.TypeArguments != null)
                {
                    types = new Type[theContract.TypeArguments.Length];
                    for (int i = 0; i < theContract.TypeArguments.Length; i++)
                    {
                        types[i] = Type.GetType(theContract.TypeArguments[i]);
                    }
                }
                if (types == null)
                {
                    methodInfo = method.MakeGenericMethod();
                }
                else
                {
                    methodInfo = method.MakeGenericMethod(types);
                }
                Result            = methodInfo.Invoke(theApi, paramList.ToArray());
                theRes.ResultType = methodInfo.ReturnType;
            }
            theRes.ResultValue = Result;
            return(theRes);
        }
Ejemplo n.º 6
0
 public ApiCore(MethodDC Contract)
 {
     Serializer  = ServiceTaker.GetService <IJsonSerializer>();
     theContract = Contract;
 }
Ejemplo n.º 7
0
 public WebRequseter()
 {
     Serializer = ServiceTaker.GetService <IJsonSerializer>();
 }
Ejemplo n.º 8
0
        private static T HttpRequest <T>(string url, string jsonData, string method)
        {
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);

            request.Method           = method;
            request.ContentType      = "application/json";
            request.Timeout          = 180000;
            request.ReadWriteTimeout = 180000;
            request.KeepAlive        = false;
            if (method.Equals("POST", StringComparison.OrdinalIgnoreCase))
            {
                if (!string.IsNullOrEmpty(jsonData))
                {
                    byte[] datas = Encoding.UTF8.GetBytes(jsonData);
                    if (datas.Length > 0)
                    {
                        request.ContentLength = datas.Length;
                        try
                        {
                            Stream requestStream = request.GetRequestStream();
                            requestStream.Write(datas, 0, datas.Length);
                            requestStream.Close();
                        }
                        catch (ProtocolViolationException)
                        {
                            request.Abort();
                            throw;
                        }
                        catch (WebException)
                        {
                            request.Abort();
                            throw;
                        }
                        catch (ObjectDisposedException)
                        {
                            request.Abort();
                            throw;
                        }
                        catch (InvalidOperationException)
                        {
                            request.Abort();
                            throw;
                        }
                        catch (NotSupportedException)
                        {
                            request.Abort();
                            throw;
                        }
                    }
                }
            }
            HttpWebResponse response      = null;
            string          responseDatas = string.Empty;

            try
            {
                response = (HttpWebResponse)request.GetResponse();
                Stream streamResponse = response.GetResponseStream();
                using (StreamReader sr = new StreamReader(streamResponse))
                {
                    responseDatas = sr.ReadToEnd();
                }
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (response != null)
                {
                    response.Close();
                    response = null;
                }
                request.Abort();
                request = null;
            }
            try
            {
                return(ServiceTaker.GetService <IJsonSerializer>().Deserialize <T>(responseDatas));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Ejemplo n.º 9
0
 /// <summary>
 /// 构造函数。
 /// </summary>
 public ApiHandler()
 {
     Serializer = ServiceTaker.GetService <IJsonSerializer>();
 }