/// <summary>
 /// This method starts a scope to resolve and release all objects automatically.
 /// You can use the <c>scope</c> in <see cref="action"/>.
 /// </summary>
 /// <param name="iocResolver">IResolver object</param>
 /// <param name="action">An action that can use the resolved object</param>
 public static void UsingScope(this IResolver iocResolver, Action <IScopedIocResolver> action)
 {
     using (var scope = iocResolver.CreateScope())
     {
         action(scope);
     }
 }
Ejemplo n.º 2
0
        public async ValueTask <object[]> RunAsync(object[] initial_imports)
        {
            var scope = _resolver?.CreateScope();

            try
            {
                foreach (var idx in _meta.ExecuteList)
                {
                    var entry      = _meta.Entrypoints[idx];
                    var outputArgs = _meta.ExportIndex[idx];
                    var inputArgs  = _meta.ImportIndex[idx];
                    var input      = _meta.InitialEntrypointIdx == idx ? initial_imports : _inputArgs[idx];
                    int loaded     = CheckConstraints(idx) ? LoadInput(entry, inputArgs, input) : -1;
                    if (loaded < inputArgs.Length)
                    {
                        SaveOutputAsNone(outputArgs);
                        continue;
                    }

                    var instance = GetOrCreateInstance(idx);

                    if (entry.IsAsync)
                    {
                        await entry.ExecuteAsync(instance, input, _output).ConfigureAwait(false);
                    }
                    else
                    {
                        entry.Execute(instance, input, _output);
                    }

                    SaveOutput(entry, outputArgs, _output);
                }
            }
            finally
            {
                try { DisposeScopedInstances(); }
                finally
                {
                    Array.Clear(_exportOptionInstances, 0, _exportOptionInstances.Length);
                    scope?.Dispose();
                }
            }

            var res = _meta.ResultEntrypointIdx == -1 ? Array.Empty <object>() : (object[])_inputArgs[_meta.ResultEntrypointIdx].Clone();

            return(res);
        }