Example #1
0
        public Assembly Load(AssemblyName assemblyName)
        {
            Project project;
            string  name = assemblyName.FullName;

            if (!Project.TryGetProject(_virtualPathProvider.Combine(_path, name), out project))
            {
                return(null);
            }

            var cache = (ICache)_serviceProvider.GetService(typeof(ICache));

            var target = new CompilationTarget(
                name,
                project.GetTargetFramework(_applicationEnvironment.RuntimeFramework).FrameworkName,
                _applicationEnvironment.Configuration,
                null);

            ModuleLoaderContext moduleContext = new ModuleLoaderContext(
                _serviceProvider,
                project.ProjectDirectory);

            moduleContext.DependencyWalker.Walk(name, project.Version, target.TargetFramework);

            var cacheContextAccessor = (ICacheContextAccessor)_serviceProvider.GetService(typeof(ICacheContextAccessor));
            var loadContextFactory   = (IAssemblyLoadContextFactory)_serviceProvider.GetService(typeof(IAssemblyLoadContextFactory)) ?? new AssemblyLoadContextFactory(_serviceProvider);

            var compiler = new InternalRoslynCompiler(
                cache,
                cacheContextAccessor,
                new NamedCacheDependencyProvider(),
                loadContextFactory,
                _fileWatcher,
                _applicationEnvironment,
                _serviceProvider);

            _orchardLibraryManager.AddAdditionalRegistrations(moduleContext.DependencyWalker.Libraries);

            var exports = ProjectExportProviderHelper.GetExportsRecursive(
                _orchardLibraryManager,
                moduleContext.LibraryExportProvider,
                new CompilationTarget(name, target.TargetFramework, target.Configuration, target.Aspect),
                true);

            _orchardLibraryManager.AddAdditionalLibraryExportRegistrations(name, exports);

            foreach (var dependency in project.Dependencies)
            {
                if (!_orchardLibraryManager.MetadataReferences.ContainsKey(dependency.Name))
                {
                    continue;
                }

                exports.MetadataReferences.Add(_orchardLibraryManager.MetadataReferences[dependency.Name]);
            }

            var compliationContext = compiler.CompileProject(
                project.ToCompilationContext(target),
                exports.MetadataReferences,
                exports.SourceReferences,
                () => CompositeResourceProvider.Default.GetResources(project));

            var roslynProjectReference = new RoslynProjectReference(compliationContext);

            _orchardLibraryManager.AddMetadataReference(name, roslynProjectReference);

            var loadContext = _assemblyLoadContextAccessor.Default;

            return(roslynProjectReference.Load(loadContext));
        }
        public Assembly Load(string name) {
            Project project;

            if (!Project.TryGetProject(_virtualPathProvider.Combine(_path, name), out project)) {
                return null;
            }

            var cache = (ICache)_serviceProvider.GetService(typeof(ICache));

            var target = new LibraryKey {
                Name = name,
                Configuration = _applicationEnvironment.Configuration,
                TargetFramework = project.GetTargetFramework(_applicationEnvironment.RuntimeFramework).FrameworkName
            };

            ModuleLoaderContext moduleContext = new ModuleLoaderContext(
                _serviceProvider,
                project.ProjectDirectory);

            moduleContext.DependencyWalker.Walk(name, project.Version, target.TargetFramework);

            var cacheContextAccessor = (ICacheContextAccessor)_serviceProvider.GetService(typeof(ICacheContextAccessor));
            var loadContextFactory = (IAssemblyLoadContextFactory)_serviceProvider.GetService(typeof(IAssemblyLoadContextFactory)) ?? new AssemblyLoadContextFactory(_serviceProvider);

            var compiler = new InternalRoslynCompiler(
               cache,
               cacheContextAccessor,
               new NamedCacheDependencyProvider(),
               loadContextFactory,
               _fileWatcher,
               _applicationEnvironment,
               _serviceProvider);

            _orchardLibraryManager.AddAdditionalRegistrations(moduleContext.DependencyWalker.Libraries);

            var exports = ProjectExportProviderHelper.GetExportsRecursive(
                cache,
                _orchardLibraryManager,
                moduleContext.LibraryExportProvider,
                target,
                true);

            _orchardLibraryManager.AddAdditionalLibraryExportRegistrations(name, exports);

            foreach (var dependency in project.Dependencies) {
                if (!_orchardLibraryManager.MetadataReferences.ContainsKey(dependency.Name))
                    continue;

                exports.MetadataReferences.Add(_orchardLibraryManager.MetadataReferences[dependency.Name]);
            }

            var compliationContext = compiler.CompileProject(project,
                target,
                exports.MetadataReferences,
                exports.SourceReferences);
            
            var roslynProjectReference = new RoslynProjectReference(compliationContext);

            _orchardLibraryManager.AddMetadataReference(name, roslynProjectReference);

            var loadContext = _assemblyLoadContextAccessor.Default;
            return roslynProjectReference.Load(loadContext);
        }