Exemplo n.º 1
0
 /// <summary>
 /// 异步事件订阅
 /// </summary>
 /// <param name="handler">处理回调</param>
 /// <param name="mode">匹配过滤模式</param>
 /// <param name="descriptions">匹配事件描述</param>
 /// <returns>异步任务</returns>
 public Task <Guid> SubscribeAsync <TEvent>(BaseEventHandler <TEvent> handler, string descriptions, FilterMode mode = FilterMode.StartsWith) where TEvent : IEvent, new()
 {
     return(Task.Factory.StartNew(() =>
     {
         if (handler == null)
         {
             throw new DomianEventException("事件处理回调不能为空!");
         }
         if (descriptions == null || descriptions.Length == 0)
         {
             throw new DomianEventException("订阅事件至少需要一条描述!");
         }
         //if (mode == FilterMode.FullLocalEvent && !(handler is BaseEventHandler<EventJsonWrapper>))
         //{
         //    throw new DomianEventException("订阅本地全部事件时,请实现BaseEventHandler<EventJsonWrapper>!");
         //}
         var id = Guid.NewGuid();
         lock (subscribes)
         {
             subscribes.Add(new LocalSubscribeItem()
             {
                 Handler = handler,
                 Descriptions = descriptions,
                 FilterMode = mode,
                 Id = id
             });
         }
         //  if (FilterMode.FullLocalEvent != mode)
         RemoteServiceAgent?.Subscribe(new SubscribeItem()
         {
             Descriptions = descriptions, FilterMode = mode, PublisherId = PublisherId
         });
         return id;
     }));
 }
Exemplo n.º 2
0
 private void RemoteSubscribe(object sender, EventArgs e)
 {
     lock (subscribes)
     {
         foreach (var item in subscribes)
         {
             RemoteServiceAgent = sender as IRemoteServiceAgent;
             RemoteServiceAgent?.Subscribe(new SubscribeItem()
             {
                 Descriptions = item.Descriptions, FilterMode = item.FilterMode, PublisherId = PublisherId
             });
         }
     }
 }