void MockRuntime(string runtimeType)
        {
            var runtime = new StubRuntimeEnvironment(runtimeType, _defaultPlatformService.Runtime);

            var customPlatformService = PlatformServices.Create(_defaultPlatformService, _defaultPlatformService.Application, runtime);

            PlatformServices.SetDefault(customPlatformService);
        }
Example #2
0
    public static void Initialize(IntPtr context, IntPtr exception)
    {
        try
        {
            DebugMessage("CoreCLREmbedding::Initialize (CLR) - Starting");

            RuntimeEnvironment = new EdgeRuntimeEnvironment(Marshal.PtrToStructure <EdgeBootstrapperContext>(context));

            Project project;
            Project.TryGetProject(RuntimeEnvironment.ApplicationDirectory, out project);

            ApplicationHostContext = new ApplicationHostContext
            {
                ProjectDirectory = RuntimeEnvironment.ApplicationDirectory,
                TargetFramework  = TargetFrameworkName,
                Project          = project
            };

            ApplicationEnvironment = new ApplicationEnvironment(ApplicationHostContext.Project, TargetFrameworkName, "Release", null);
            LoadContextAccessor    = new EdgeAssemblyLoadContextAccessor();

            EdgeServiceProvider serviceProvider = new EdgeServiceProvider();

            serviceProvider.Add(typeof(IRuntimeEnvironment), RuntimeEnvironment);
            serviceProvider.Add(typeof(IApplicationEnvironment), ApplicationEnvironment);
            serviceProvider.Add(typeof(IAssemblyLoadContextAccessor), LoadContextAccessor);

            CallContextServiceLocator.Locator = new EdgeServiceProviderLocator
            {
                ServiceProvider = serviceProvider
            };

            PlatformServices.SetDefault(PlatformServices.Create(null, ApplicationEnvironment, RuntimeEnvironment, null, LoadContextAccessor, null));

            DebugMessage("CoreCLREmbedding::Initialize (CLR) - Complete");
        }

        catch (Exception e)
        {
            DebugMessage("CoreCLREmbedding::Initialize (CLR) - Exception was thrown: {0}", e.Message);

            V8Type v8Type;
            Marshal.WriteIntPtr(exception, MarshalCLRToV8(e, out v8Type));
        }
    }
Example #3
0
        private void Initialize(RuntimeOptions options, IAssemblyLoadContextAccessor loadContextAccessor)
        {
            var applicationHostContext = new ApplicationHostContext
            {
                ProjectDirectory   = _projectDirectory,
                RuntimeIdentifiers = _runtimeEnvironment.GetAllRuntimeIdentifiers(),
                TargetFramework    = _targetFramework
            };

            var libraries = ApplicationHostContext.GetRuntimeLibraries(applicationHostContext, throwOnInvalidLockFile: true);

            Logger.TraceInformation("[{0}]: Project path: {1}", GetType().Name, applicationHostContext.ProjectDirectory);
            Logger.TraceInformation("[{0}]: Project root: {1}", GetType().Name, applicationHostContext.RootDirectory);
            Logger.TraceInformation("[{0}]: Project configuration: {1}", GetType().Name, options.Configuration);
            Logger.TraceInformation("[{0}]: Packages path: {1}", GetType().Name, applicationHostContext.PackagesDirectory);

            _applicationHostContext = applicationHostContext;

            _project = applicationHostContext.Project;

#if FEATURE_DNX_MIN_VERSION_CHECK
            ValidateMinRuntimeVersion(libraries);
#endif

            // Create a new Application Environment for running the app. It needs a reference to the Host's application environment
            // (if any), which we can get from the service provider we were given.
            // If this is null (i.e. there is no Host Application Environment), that's OK, the Application Environment we are creating
            // will just have it's own independent set of global data.
            var hostEnvironment        = PlatformServices.Default.Application;
            var applicationEnvironment = new ApplicationEnvironment(Project, _targetFramework, options.Configuration, hostEnvironment);

            var compilationContext = new CompilationEngineContext(
                applicationEnvironment,
                _runtimeEnvironment,
                loadContextAccessor.Default,
                new CompilationCache());

            // Compilation services available only for runtime compilation
            compilationContext.AddCompilationService(typeof(RuntimeOptions), options);
            compilationContext.AddCompilationService(typeof(IApplicationShutdown), _shutdown);

            var compilationEngine      = new CompilationEngine(compilationContext);
            var runtimeLibraryExporter = new RuntimeLibraryExporter(() => compilationEngine.CreateProjectExporter(Project, _targetFramework, options.Configuration));

            var runtimeLibraryManager = new RuntimeLibraryManager(applicationHostContext);

            // Default services
            _serviceProvider.Add(typeof(ILibraryExporter), runtimeLibraryExporter);
            _serviceProvider.Add(typeof(IApplicationShutdown), _shutdown);
            _serviceProvider.Add(typeof(IApplicationEnvironment), applicationEnvironment);
            _serviceProvider.Add(typeof(IRuntimeEnvironment), PlatformServices.Default.Runtime);
            _serviceProvider.Add(typeof(ILibraryManager), runtimeLibraryManager);
            _serviceProvider.Add(typeof(IAssemblyLoadContextAccessor), PlatformServices.Default.AssemblyLoadContextAccessor);
            _serviceProvider.Add(typeof(IAssemblyLoaderContainer), PlatformServices.Default.AssemblyLoaderContainer);


            PlatformServices.SetDefault(
                PlatformServices.Create(
                    PlatformServices.Default,
                    application: applicationEnvironment,
                    libraryManager: runtimeLibraryManager
                    ));

            if (options.CompilationServerPort.HasValue)
            {
                // Change the project reference provider
                Project.DefaultCompiler = Project.DefaultDesignTimeCompiler;
            }

            CallContextServiceLocator.Locator.ServiceProvider = _serviceProvider;

            // TODO: Dedupe this logic in the RuntimeLoadContext
            var projects = libraries.Where(p => p.Type == LibraryTypes.Project)
                           .ToDictionary(p => p.Identity.Name, p => (ProjectDescription)p);

            var assemblies = PackageDependencyProvider.ResolvePackageAssemblyPaths(libraries);

            // Configure Assembly loaders
            _loaders.Add(new ProjectAssemblyLoader(loadContextAccessor, compilationEngine, projects.Values));
            _loaders.Add(new PackageAssemblyLoader(loadContextAccessor, assemblies, libraries));

            var compilerOptionsProvider = new CompilerOptionsProvider(projects);

            _serviceProvider.Add(typeof(ICompilerOptionsProvider), compilerOptionsProvider);

            CompilationServices.SetDefault(
                CompilationServices.Create(
                    libraryExporter: runtimeLibraryExporter,
                    compilerOptionsProvider: compilerOptionsProvider
                    )
                );

#if DNX451
            PackageDependencyProvider.EnableLoadingNativeLibraries(libraries);
#endif
            AddBreadcrumbs(libraries);
        }
Example #4
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;
            }
        }