コード例 #1
0
ファイル: ConcurrentTaskService.cs プロジェクト: dioptre/nkd
 public static void ProcessAsync(ShellTask taskInfo)
 {
     var task = new Task((taskInfoObject) =>
     {
         var shellTask = (ShellTask)taskInfoObject;
         ProcessAsyncShellTask(shellTask);
         return;
     }, taskInfo);
     task.Start();
 }
コード例 #2
0
ファイル: ConcurrentTaskService.cs プロジェクト: dioptre/nkd
        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))
                            {
                                MethodInfo m = null;
                                if (shellTask.ShellData != null)
                                {
                                    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();                                    
                                }
                                else
                                {
                                    m = (from o in interfaceType.GetMethods()
                                         where o.Name == shellTask.ShellAction.Method.Name
                                         && o.GetParameters().Length == 1                                         
                                         select o).First();
                                    logger.Information("WARNING: Concurrent Service Began Without Assignment Check. Dangerous!");
                                }
                                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
                    {
                        try
                        {
                            // any database changes in this using(env) scope are invalidated
                            if (transactionManager != null)
                            {
                                try
                                {
                                    transactionManager.Cancel();
                                }
                                catch { }
                            }
                        }
                        catch { }
                    }
                }
            }
        }