/// <summary> /// 监听某个事件。 /// </summary> /// <param name="theEvent">事件契约。</param> /// <param name="appRuntimeName">部署的实例名。</param> /// <param name="listenerClassName">监听者的类名。</param> /// <param name="handleMethodName">响应方法名。</param> public void ListenEvent(EventDC theEvent, string appRuntimeName, string listenerClassName, string handleMethodName) { RemoteEventListenEntity listenEt = new RemoteEventListenEntity(); listenEt.AppRuntimeName = appRuntimeName; listenEt.EventName = theEvent.EventName; listenEt.HandleMethodName = handleMethodName; listenEt.ListenerClassName = listenerClassName; listenEt.SenderInterfaceName = theEvent.SenderInterfaceName; listenEt.SenderTypeName = theEvent.SenderTypeName; listenEt.ProxyMapConfigKey = string.Empty; lock (locker) { Action act = delegate() { if (!(dataPers.GetQueryable <RemoteEventListenEntity>().Where(ww => ww.EventListenKey == listenEt.EventListenKey).Count() > 0)) { dataPers.Add(listenEt); } }; dataPers.WorkUnitAct(act); } }
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); }
/// <summary> /// 按应用运行时名称取没被处理的监听的事件。 /// </summary> /// <param name="appRuntimeName">应用运行时名称。</param> /// <returns></returns> public List <EventDC> GetEventList(string appRuntimeName) { List <RemoteEventEntity> EventList = new List <RemoteEventEntity>(); lock (locker) { EventList = dataPers.GetQueryable <RemoteEventEntity>() .Where(ww => ww.AppRuntimeName == appRuntimeName && ww.HandleState == HandleState.Unstarted).ToList(); } List <EventDC> EventDcList = new List <EventDC>(); foreach (var Event in EventList) { EventDC aEDc = new EventDC(); aEDc.EventKey = Event.EventKey; aEDc.EventName = Event.EventName; ParameterDC message = new ParameterDC(); message.JsonValue = Event.MessageJsonValue; message.TypeQualifiedName = Event.MessageTypeQuaName; aEDc.Message = message; aEDc.SenderInterfaceName = Event.SenderInterfaceName; aEDc.SenderTypeName = Event.SenderTypeName; EventDcList.Add(aEDc); } return(EventDcList); }
/// <summary> /// 将事件向远程监听者播送。 /// </summary> /// <param name="pEvent">事件数据契约。</param> public void CastRemoteEvent(EventDC pEvent) { List <RemoteEventListenEntity> EventListenList = new List <RemoteEventListenEntity>(); lock (locker) { EventListenList = dataPers.GetQueryable <RemoteEventListenEntity>() .Where(ww => ww.SenderInterfaceName == pEvent.SenderInterfaceName && ww.SenderTypeName == pEvent.SenderTypeName && ww.EventName == pEvent.EventName).ToList(); Action saveForCast = delegate() { foreach (var EventListen in EventListenList) { RemoteEventEntity eventEt = new RemoteEventEntity(); eventEt.AppRuntimeName = EventListen.AppRuntimeName; eventEt.EventKey = pEvent.EventKey; eventEt.EventName = pEvent.EventName; eventEt.HandleState = HandleState.Unstarted; eventEt.MessageJsonValue = pEvent.Message.JsonValue; eventEt.MessageTypeQuaName = pEvent.Message.TypeQualifiedName; eventEt.SenderInterfaceName = pEvent.SenderInterfaceName; eventEt.SenderTypeName = pEvent.SenderTypeName; if (!dataPers.GetQueryable <RemoteEventEntity>().Contains(eventEt)) { dataPers.Add(eventEt); } } }; dataPers.WorkUnitAct(saveForCast); } }
/// <summary> /// 从契约中反序列化取参数列表。 /// </summary> /// <param name="theRemoteEven">方法数据契约。</param> /// <returns>参数列表。</returns> private static RemoteEventArgs <T> GetEventArgs <T>(EventDC theRemoteEven) where T : class, new() { RemoteEventArgs <T> Args = new RemoteEventArgs <T>(); Args.EventName = theRemoteEven.EventName; Type relType = Type.GetType(theRemoteEven.Message.TypeQualifiedName); object value = Serializer.Deserialize(theRemoteEven.Message.JsonValue, relType); return(Args); }
/// <summary> /// 取消某个事件的监听。 /// </summary> /// <param name="theEvent">事件契约。</param> /// <param name="appRuntimeName">部署的实例名。</param> /// <param name="listenerClassName">监听者的类名。</param> /// <param name="handleMethodName">响应方法名。</param> public void RepealListen(EventDC theEvent, string appRuntimeName, string listenerClassName, string handleMethodName) { RemoteEventListenEntity listenEt = new RemoteEventListenEntity(); listenEt.AppRuntimeName = appRuntimeName; listenEt.EventName = theEvent.EventName; listenEt.HandleMethodName = handleMethodName; listenEt.ListenerClassName = listenerClassName; listenEt.SenderInterfaceName = theEvent.SenderInterfaceName; listenEt.SenderTypeName = theEvent.SenderTypeName; lock (locker) { dataPers.Delete(listenEt); } }
/// <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); }