private List <ServiceHandlerInfo> SetupDefaultServiceHandlers()
        {
            var l    = new List <ServiceHandlerInfo>();
            var srvs = ServiceLocator.GetAllInstances <IMessageHandlerServiceBase>();

            foreach (var srv in srvs)
            {
                var  st  = srv.GetType();
                Type sit = null;
                foreach (var it in st.GetInterfaces())
                {
                    if (it.IsGenericType && it.GetGenericTypeDefinition() == typeof(IMessageHandlerService <>))
                    {
                        Type   mtype = it.GetGenericArguments()[0];
                        string sname = mtype.Name;
                        if (l.Exists(x => x.ServiceName.Equals(sname, StringComparison.InvariantCultureIgnoreCase)))
                        {
                            sname = string.Format("{0}_{1}", st.Name, mtype.Name);
                        }
                        log.Info("Registering service handler {0} of message {1}. Service name: {2}", st.FullName, mtype.FullName, sname);
                        if (l.Exists(x => x.ServiceName.Equals(sname, StringComparison.InvariantCultureIgnoreCase)))
                        {
                            throw new Exception("Failed to register service handler because of duplicate service name. " + st.FullName);
                        }
                        var ht = typeof(IMessageHandlerService <>).MakeGenericType(mtype);
                        l.Add(new ServiceHandlerInfo {
                            ServiceName = sname,
                            RequestType = mtype,
                            MessageHandlerGenericType = ht,
                            HandleMethod = DelegateFactory.CreateServiceHandlerDelegate(ht.GetMethod("Handle"))
                        });
                    }
                }
                ServiceLocator.ReleaseInstance(srv);
            }
            return(l);
        }
Beispiel #2
0
 public virtual bool HandleRequest(IRequestContext ctx)
 {
     ctx.DispatchUrl = ctx.RawUrl;
     if (!string.IsNullOrEmpty(RootUrl))
     {
         var idx = ctx.RawUrl.IndexOf(RootUrl);
         if (idx < 0)
         {
             return(false);
         }
         ctx.DispatchUrl = ctx.RawUrl.Substring(idx);
     }
     foreach (IServlet srvlet in _resolver.GetAllInstances <IServlet>())
     {
         if (CanHandleRequest(ctx, srvlet.MatchUrl))
         {
             srvlet.HandleRequest(ctx);
             _resolver.ReleaseInstance(srvlet);
             return(true);
         }
         _resolver.ReleaseInstance(srvlet);
     }
     return(false);
 }