예제 #1
0
 public static void ProcessAsync(ShellTask taskInfo)
 {
     var task = new Task((taskInfoObject) =>
     {
         var shellTask = (ShellTask)taskInfoObject;
         ProcessAsyncShellTask(shellTask);
         return;
     }, taskInfo);
     task.Start();
 }
예제 #2
0
        private static void ProcessAsyncShellTask(ShellTask shellTask)
        {
            // Force reloading extensions if there were extensions installed
            // See http://orchard.codeplex.com/workitem/17465
            //if (shellTask.MessageName == "IRecipeSchedulerEventHandler.ExecuteWork")
            //{
            //    var ctx = _orchardHost().GetShellContext(entry.ShellSettings);
            //}

            var shellContext = shellTask.ShellContextFactory.CreateDescribedContext(shellTask.ShellSettings, shellTask.ShellDescriptor);
            using (shellContext.LifetimeScope)
            {
                using (var standaloneEnvironment = shellContext.LifetimeScope.CreateWorkContextScope())
                {

                    ILogger logger = NullLogger.Instance;
                    ITransactionManager transactionManager;
                    if (!standaloneEnvironment.TryResolve(out transactionManager))
                        transactionManager = null;

                    try
                    {


                        foreach (var interfaceType in shellTask.ShellAction.Method.DeclaringType.GetInterfaces())
                        {
                            string methodKey = String.Format("{0}_{1}_{2}", shellTask.ShellAction.GetType().FullName, shellTask.ShellAction.Method.DeclaringType, shellTask.ShellAction.Method.Name);
                            MethodInfo method;
                            if (!_interfaceMethodsCache.TryGetValue(methodKey, out method))
                            {
                                var m = (from o in interfaceType.GetMethods()
                                         where o.Name == shellTask.ShellAction.Method.Name
                                         && o.GetParameters().Length == 1
                                         && shellTask.ShellData.GetType().IsAssignableFrom(o.GetParameters()[0].ParameterType)
                                         select o).First();

                                method = _interfaceMethodsCache.GetOrAdd(methodKey, m);
                            }
                            if (method != null)
                            {

                                //DynamicMethod dynamic = new DynamicMethod(string.Empty,
                                //typeof(object),
                                //new Type[0],
                                //target.DeclaringType);
                                //ILGenerator il = dynamic.GetILGenerator();
                                //il.DeclareLocal(target.DeclaringType);
                                //il.Emit(OpCodes.Newobj, target);
                                //il.Emit(OpCodes.Stloc_0);
                                //il.Emit(OpCodes.Ldloc_0);
                                //il.Emit(OpCodes.Ret);
                                //var y = new { }.ActLike(interfaceType);

                                //Works
                                //IBlockModelService bms;
                                //if (standaloneEnvironment.TryResolve(out bms))
                                //    bms.ProcessModel(shellTask.ShellData);

                                //dynamic x = new ExpandoObject();
                                //x.i = Activator.CreateInstance(InterfaceHelper.ClassBuilder.Build(interfaceType));
                                //dynamic o  = x.i;


                                object obj;
                                if (Autofac.ResolutionExtensions.TryResolve(shellContext.LifetimeScope, interfaceType, out obj))
                                    method.Invoke(obj, new object[] { shellTask.ShellData });
                                return;
                            }


                        }

                        throw new NotSupportedException("Could not invoke (missing) long running concurrent method using IoC.");

                    }
                    catch
                    {
                        throw;
                    }
                    finally
                    {
                        // any database changes in this using(env) scope are invalidated
                        if (transactionManager != null)
                            transactionManager.Cancel();
                    }
                }
            }
        }