示例#1
0
        public Task<int> Main(string[] args)
        {
            if (args.Length < 1)
            {
                Console.WriteLine("{app} [arguments]");
                return Task.FromResult(-1);
            }

            var name = args[0];
            var programArgs = args.Skip(1).ToArray();

            var assembly = Assembly.Load(new AssemblyName(name));

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

            #if NET45
            // REVIEW: Need a way to set the application base on mono
            string applicationBaseDirectory = PlatformHelper.IsMono ?
                                              Directory.GetCurrentDirectory() :
                                              AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
            #else
            string applicationBaseDirectory = ApplicationContext.BaseDirectory;
            #endif

            var framework = Environment.GetEnvironmentVariable("TARGET_FRAMEWORK") ?? Environment.GetEnvironmentVariable("KRE_FRAMEWORK");
            var configuration = Environment.GetEnvironmentVariable("TARGET_CONFIGURATION") ?? Environment.GetEnvironmentVariable("KRE_CONFIGURATION") ?? "Debug";

            var targetFramework = FrameworkNameUtility.ParseFrameworkName(framework ?? (PlatformHelper.IsMono ? "net45" : "net451"));

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

            CallContextServiceLocator.Locator = new ServiceProviderLocator();

            var serviceProvider = new ServiceProvider();
            serviceProvider.Add(typeof(IAssemblyLoaderContainer), _container);
            serviceProvider.Add(typeof(IAssemblyLoaderEngine), _loaderEngine);
            serviceProvider.Add(typeof(IApplicationEnvironment), applicationEnvironment);

            CallContextServiceLocator.Locator.ServiceProvider = serviceProvider;

            return EntryPointExecutor.Execute(assembly, programArgs, serviceProvider);
        }
示例#2
0
        public void RegisteredServicesCanBeIEnumerableResolved()
        {
            var serviceProvider = new ServiceProvider();
            var service = new Service();

            serviceProvider.Add(typeof(IService), service);

            var serviceList = (IEnumerable<IService>)serviceProvider.GetService(typeof(IEnumerable<IService>));

            Assert.NotNull(serviceList);
            var enumerator = serviceList.GetEnumerator();
            Assert.True(enumerator.MoveNext(), "The serviceList should have 1 element");
            Assert.Same(service, enumerator.Current);
            Assert.False(enumerator.MoveNext(), "The serviceList should have 1 element");
        }
示例#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 ApplicationEnvironment(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;
            }
        }
示例#4
0
        public Task<int> RunAsync(List<string> args)
        {
            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 = Assembly.Load(new AssemblyName(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 framework = Environment.GetEnvironmentVariable("TARGET_FRAMEWORK") ?? Environment.GetEnvironmentVariable(EnvironmentNames.Framework);
                var configuration = Environment.GetEnvironmentVariable("TARGET_CONFIGURATION") ?? Environment.GetEnvironmentVariable(EnvironmentNames.Configuration) ?? "Debug";

                var targetFramework = FrameworkNameUtility.ParseFrameworkName(framework ?? FrameworkNames.ShortNames.Dnx451);

                var applicationEnvironment = new ApplicationEnvironment(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);

                CallContextServiceLocator.Locator.ServiceProvider = serviceProvider;

                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;
            }
        }
示例#5
0
        public Task<int> Main(string[] args)
        {
            if (args.Length < 1)
            {
                Console.WriteLine("{app} [arguments]");
                return Task.FromResult(-1);
            }

            var name = args[0];
            var programArgs = args.Skip(1).ToArray();

            var assembly = Assembly.Load(new AssemblyName(name));

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

            #if ASPNET50
            string applicationBaseDirectory;
            if (PlatformHelper.IsMono)
            {
                applicationBaseDirectory = Environment.GetEnvironmentVariable(EnvironmentNames.AppBase);

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

            var framework = Environment.GetEnvironmentVariable("TARGET_FRAMEWORK") ?? Environment.GetEnvironmentVariable(EnvironmentNames.Framework);
            var configuration = Environment.GetEnvironmentVariable("TARGET_CONFIGURATION") ?? Environment.GetEnvironmentVariable(EnvironmentNames.Configuration) ?? "Debug";

            // TODO: Support the highest installed version
            var targetFramework = FrameworkNameUtility.ParseFrameworkName(framework ?? "aspnet50");

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

            CallContextServiceLocator.Locator = new ServiceProviderLocator();

            var serviceProvider = new ServiceProvider();
            serviceProvider.Add(typeof(IAssemblyLoaderContainer), _container);
            serviceProvider.Add(typeof(IAssemblyLoadContextAccessor), LoadContextAccessor.Instance);
            serviceProvider.Add(typeof(IApplicationEnvironment), applicationEnvironment);

            CallContextServiceLocator.Locator.ServiceProvider = serviceProvider;

            return EntryPointExecutor.Execute(assembly, programArgs, serviceProvider);
        }