private static ScheduledTask NewTask <T>(ScheduledProducerSettings settings, object instance = null)
        {
            Type        type        = typeof(T);
            HandlerInfo handlerInfo = new HandlerInfo(type.Namespace, type.Name);

            if (instance != null)
            {
                handlerInfo.Instance = JsonConvert.SerializeObject(instance);
            }

            ScheduledTask scheduledTask = new ScheduledTask
            {
                Handler = JsonConvert.SerializeObject(handlerInfo)
            };

            settings.ProvisionTask(scheduledTask);
            return(scheduledTask);
        }
Exemple #2
0
        private Handler CreateOrGetHandler(ScheduledTask task)
        {
            HandlerInfo handlerInfo = JsonConvert.DeserializeObject <HandlerInfo>(task.Handler);
            Handler     handler;

            if (!HandlerCache.TryGetValue(handlerInfo, out handler))
            {
                string typeName = $"{handlerInfo.Namespace}.{handlerInfo.Entrypoint}";
                Type   type     = _settings.TypeResolver.FindTypeByName(typeName);
                if (type != null)
                {
                    object instance;
                    if (!string.IsNullOrWhiteSpace(handlerInfo.Instance) && !handlerInfo.Instance.Equals(NoHandlerState))
                    {
                        try
                        {
                            instance = JsonConvert.DeserializeObject(handlerInfo.Instance, type);
                        }
                        catch (Exception)
                        {
                            instance = null;
                        }
                    }
                    else
                    {
                        instance = Activator.CreateInstance(type);
                    }

                    if (instance != null)
                    {
                        handler = TryWrapHook <Handler>(instance);
                        if (handler != null)
                        {
                            HandlerCache.Add(handlerInfo, handler);
                        }
                    }
                }
            }
            return(handler);
        }