예제 #1
0
        public Task<int> RunAsync(List<string> args, IRuntimeEnvironment env, string appBase, FrameworkName targetFramework)
        {
            var accessor = LoadContextAccessor.Instance;
            var container = new LoaderContainer();
            LoadContext.InitializeDefaultContext(new DefaultLoadContext(container));

            var disposable = container.AddLoader(new PathBasedAssemblyLoader(accessor, _searchPaths));

            try
            {
                var name = args[0];
                var programArgs = new string[args.Count - 1];
                args.CopyTo(1, programArgs, 0, programArgs.Length);

                var assembly = accessor.Default.Load(name);

                if (assembly == null)
                {
                    return Task.FromResult(1);
                }

#if DNX451
                string applicationBaseDirectory = appBase;

                // Set the app domain variable so that AppContext.BaseDirectory works on .NET Framework (and hopefully mono)
                AppDomain.CurrentDomain.SetData("APP_CONTEXT_BASE_DIRECTORY", applicationBaseDirectory);
#else
                var applicationBaseDirectory = AppContext.BaseDirectory;
#endif

                var configuration = Environment.GetEnvironmentVariable("TARGET_CONFIGURATION") ?? Environment.GetEnvironmentVariable(EnvironmentNames.Configuration) ?? "Debug";
                Logger.TraceInformation($"[{nameof(Bootstrapper)}] Runtime Framework: {targetFramework}");

                var applicationEnvironment = new HostApplicationEnvironment(applicationBaseDirectory,
                                                                        targetFramework,
                                                                        configuration,
                                                                        assembly);

                CallContextServiceLocator.Locator = new ServiceProviderLocator();

                var serviceProvider = new ServiceProvider();
                serviceProvider.Add(typeof(IAssemblyLoaderContainer), container);
                serviceProvider.Add(typeof(IAssemblyLoadContextAccessor), accessor);
                serviceProvider.Add(typeof(IApplicationEnvironment), applicationEnvironment);
                serviceProvider.Add(typeof(IRuntimeEnvironment), env);

                CallContextServiceLocator.Locator.ServiceProvider = serviceProvider;

                PlatformServices.SetDefault(
                    PlatformServices.Create(
                        basePlatformServices: null,
                        application: applicationEnvironment,
                        runtime: env,
                        container: container,
                        accessor: accessor));

#if DNX451
                if (RuntimeEnvironmentHelper.IsMono)
                {
                    // Setting this value because of a Execution Context bug in older versions of Mono
                    AppDomain.CurrentDomain.SetData("DNX_SERVICEPROVIDER", serviceProvider);
                }
#endif

                var task = EntryPointExecutor.Execute(assembly, programArgs, serviceProvider);

                return task.ContinueWith((t, state) =>
                {
                    // Dispose the host
                    ((IDisposable)state).Dispose();

                    return t.GetAwaiter().GetResult();
                }, disposable);
            }
            catch
            {
                disposable.Dispose();

                throw;
            }
        }
예제 #2
0
        public Task <int> RunAsync(List <string> args, IRuntimeEnvironment env, string appBase, FrameworkName targetFramework)
        {
            var accessor  = LoadContextAccessor.Instance;
            var container = new LoaderContainer();

            LoadContext.InitializeDefaultContext(new DefaultLoadContext(container));

            var disposable = container.AddLoader(new PathBasedAssemblyLoader(accessor, _searchPaths));

            try
            {
                var name        = args[0];
                var programArgs = new string[args.Count - 1];
                args.CopyTo(1, programArgs, 0, programArgs.Length);

                var assembly = accessor.Default.Load(name);

                if (assembly == null)
                {
                    return(Task.FromResult(1));
                }

#if DNX451
                string applicationBaseDirectory = appBase;

                // Set the app domain variable so that AppContext.BaseDirectory works on .NET Framework (and hopefully mono)
                AppDomain.CurrentDomain.SetData("APP_CONTEXT_BASE_DIRECTORY", applicationBaseDirectory);
#else
                var applicationBaseDirectory = AppContext.BaseDirectory;
#endif

                var configuration = Environment.GetEnvironmentVariable("TARGET_CONFIGURATION") ?? Environment.GetEnvironmentVariable(EnvironmentNames.Configuration) ?? "Debug";
                Logger.TraceInformation($"[{nameof(Bootstrapper)}] Runtime Framework: {targetFramework}");

                var applicationEnvironment = new HostApplicationEnvironment(applicationBaseDirectory,
                                                                            targetFramework,
                                                                            configuration,
                                                                            assembly);

                var serviceProvider = new ServiceProvider();
                serviceProvider.Add(typeof(IAssemblyLoaderContainer), container);
                serviceProvider.Add(typeof(IAssemblyLoadContextAccessor), accessor);
                serviceProvider.Add(typeof(IApplicationEnvironment), applicationEnvironment);
                serviceProvider.Add(typeof(IRuntimeEnvironment), env);

                PlatformServices.SetDefault(new DnxHostPlatformServices(applicationEnvironment, env, container, accessor));

#if DNX451
                if (RuntimeEnvironmentHelper.IsMono)
                {
                    // Setting this value because of a Execution Context bug in older versions of Mono
                    AppDomain.CurrentDomain.SetData("DNX_SERVICEPROVIDER", serviceProvider);
                }
#endif

                var task = EntryPointExecutor.Execute(assembly, programArgs, serviceProvider);

                return(task.ContinueWith((t, state) =>
                {
                    // Dispose the host
                    ((IDisposable)state).Dispose();

                    return t.GetAwaiter().GetResult();
                }, disposable));
            }
            catch
            {
                disposable.Dispose();

                throw;
            }
        }
예제 #3
0
        public Task <int> RunAsync(List <string> args, IRuntimeEnvironment env, FrameworkName targetFramework)
        {
            var accessor  = LoadContextAccessor.Instance;
            var container = new LoaderContainer();

            LoadContext.InitializeDefaultContext(new DefaultLoadContext(container));

            var disposable = container.AddLoader(new PathBasedAssemblyLoader(accessor, _searchPaths));

            try
            {
                var name        = args[0];
                var programArgs = new string[args.Count - 1];
                args.CopyTo(1, programArgs, 0, programArgs.Length);

                var assembly = accessor.Default.Load(name);

                if (assembly == null)
                {
                    return(Task.FromResult(-1));
                }

#if DNX451
                string applicationBaseDirectory = Environment.GetEnvironmentVariable(EnvironmentNames.AppBase);

                if (string.IsNullOrEmpty(applicationBaseDirectory))
                {
                    applicationBaseDirectory = Directory.GetCurrentDirectory();
                }
#else
                string applicationBaseDirectory = AppContext.BaseDirectory;
#endif

                var configuration = Environment.GetEnvironmentVariable("TARGET_CONFIGURATION") ?? Environment.GetEnvironmentVariable(EnvironmentNames.Configuration) ?? "Debug";
                Logger.TraceInformation($"[{nameof(Bootstrapper)}] Runtime Framework: {targetFramework}");

                var applicationEnvironment = new HostApplicationEnvironment(applicationBaseDirectory,
                                                                            targetFramework,
                                                                            configuration,
                                                                            assembly);

                CallContextServiceLocator.Locator = new ServiceProviderLocator();

                var serviceProvider = new ServiceProvider();
                serviceProvider.Add(typeof(IAssemblyLoaderContainer), container);
                serviceProvider.Add(typeof(IAssemblyLoadContextAccessor), accessor);
                serviceProvider.Add(typeof(IApplicationEnvironment), applicationEnvironment);
                serviceProvider.Add(typeof(IRuntimeEnvironment), env);

                CallContextServiceLocator.Locator.ServiceProvider = serviceProvider;
#if DNX451
                if (RuntimeEnvironmentHelper.IsMono)
                {
                    // Setting this value because of a Execution Context bug in older versions of Mono
                    AppDomain.CurrentDomain.SetData("DNX_SERVICEPROVIDER", serviceProvider);
                }
#endif

                var task = EntryPointExecutor.Execute(assembly, programArgs, serviceProvider);

                return(task.ContinueWith(async(t, state) =>
                {
                    // Dispose the host
                    ((IDisposable)state).Dispose();

                    return await t;
                }, disposable).Unwrap());
            }
            catch
            {
                disposable.Dispose();

                throw;
            }
        }