Exemplo n.º 1
0
        protected sealed override void Setup()
        {
            try
            {
                var sw = Stopwatch.StartNew();
                PhotonWireEngine.Initialize(HubTargetAssemlies, HubTargetTags, Serializer, IsDebugMode);
                sw.Stop();
                Logger.InitializeComplete(ApplicationName, sw.Elapsed.TotalMilliseconds);
            }
            catch (Exception ex)
            {
                var aex = ex as AggregateException;
                if (aex != null)
                {
                    ex = aex.InnerException;
                }

                Logger.SetupFailed(ApplicationName, ex.GetType().Name, ex.Message, ex.StackTrace);
                if (IsDebugMode)
                {
                    System.Diagnostics.Debug.Fail("PhotonWire Initialize Failed", ex.Message);
                }
            }
            PhotonWireEngine.Instance.RegisterApplication(this);
            SetupCore();
        }
Exemplo n.º 2
0
        public static void Initialize(Assembly[] targetAssemblies, string[] targetTags, IPhotonSerializer serializer, bool enableExceptionReturnDebugError)
        {
            if (Interlocked.Increment(ref registeredEngine) != 0)
            {
                return;
            }

            var assemblies = targetAssemblies;
            var types      = assemblies
                             .SelectMany(x =>
            {
                try
                {
                    return(x.GetTypes());
                }
                catch (ReflectionTypeLoadException ex)
                {
                    return(ex.Types.Where(t => t != null));
                }
            });

            var engine = new PhotonWireEngine()
            {
                serializer = serializer,
                enableExceptionReturnDebugError = enableExceptionReturnDebugError
            };

            Parallel.ForEach(types, type =>
            {
                var hub = HubDescriptor.CreateIfPossible(type);
                if (hub == null)
                {
                    return;
                }

                lock (engine.hubs)
                {
                    // check tag
                    if (targetTags.Length > 0)
                    {
                        if (!targetTags.Any(x => hub.HubTags.Contains(x)))
                        {
                            hub.CanExecute = false;
                        }
                    }

                    var key = Tuple.Create(hub.HubKind, hub.HubId);
                    if (engine.hubs.ContainsKey(key))
                    {
                        throw new InvalidOperationException(string.Format("same hubId is not allowed, class:{0} hubid:{1}", hub.HubName, hub.HubId));
                    }
                    engine.hubs.Add(key, hub);
                    engine.hubsByType.Add(hub.HubType, hub);
                }
            });

            Instance = engine;
        }