private static IAssembly CreateAssembly(string sourceCode) { var compiledAssembly = Compile(sourceCode); using (var asm = new MemoryStream(compiledAssembly)) { var assemblyLoadContextFactory = new AssemblyLoadContextFactory(); var assemblyLoadContext = assemblyLoadContextFactory.Create("Plugin", true); //var assemblyLoadContext = new AssemblyLoadContext("Plugin", true); var assembly = assemblyLoadContext.LoadFromStream(asm); return(assembly); } }
public IAssemblyLoadContext Create(IServiceProvider serviceProvider) { var hostContextKey = Tuple.Create("RuntimeLoadContext", _project.Name, _appEnv.Configuration, _appEnv.RuntimeFramework); var appHostContext = _compilationFactory.CompilationCache.Cache.Get <ApplicationHostContext>(hostContextKey, ctx => { var applicationHostContext = new ApplicationHostContext(serviceProvider, _project.ProjectDirectory, packagesDirectory: null, configuration: _appEnv.Configuration, targetFramework: _appEnv.RuntimeFramework); applicationHostContext.DependencyWalker.Walk(_project.Name, _project.Version, _appEnv.RuntimeFramework); // Watch all projects for project.json changes foreach (var library in applicationHostContext.DependencyWalker.Libraries) { if (string.Equals(library.Type, "Project")) { ctx.Monitor(new FileWriteTimeCacheDependency(library.Path)); } } // Add a cache dependency on restore complete to reevaluate dependencies ctx.Monitor(_compilationFactory.CompilationCache.NamedCacheDependencyProvider.GetNamedDependency(_project.Name + "_Dependencies")); // Create compilation engine and add it to the services var compilationEngine = _compilationFactory.CreateEngine(new CompilationEngineContext( applicationHostContext.LibraryManager, applicationHostContext.ProjectGraphProvider, NoopWatcher.Instance, applicationHostContext.ServiceProvider, _appEnv.RuntimeFramework, _appEnv.Configuration)); applicationHostContext.AddService(typeof(ICompilationEngine), compilationEngine); return(applicationHostContext); }); var factory = new AssemblyLoadContextFactory(appHostContext.ServiceProvider); return(factory.Create(appHostContext.ServiceProvider)); }
public IAssemblyLoadContext Create(IServiceProvider serviceProvider) { var hostContextKey = Tuple.Create("RuntimeLoadContext", _project.Name, _appEnv.Configuration, _appEnv.RuntimeFramework); var appHostContext = _cache.Get <ApplicationHostContext>(hostContextKey, ctx => { var applicationHostContext = new ApplicationHostContext(serviceProvider, _project.ProjectDirectory, packagesDirectory: null, configuration: _appEnv.Configuration, targetFramework: _appEnv.RuntimeFramework, cache: _cache, cacheContextAccessor: _cacheContextAccessor, namedCacheDependencyProvider: _namedDependencyProvider); applicationHostContext.DependencyWalker.Walk(_project.Name, _project.Version, _appEnv.RuntimeFramework); // Watch all projects for project.json changes foreach (var library in applicationHostContext.DependencyWalker.Libraries) { if (string.Equals(library.Type, "Project")) { ctx.Monitor(new FileWriteTimeCacheDependency(library.Path)); } } // Add a cache dependency on restore complete to reevaluate dependencies ctx.Monitor(_namedDependencyProvider.GetNamedDependency(_project.Name + "_Dependencies")); return(applicationHostContext); }); var factory = new AssemblyLoadContextFactory(appHostContext.ServiceProvider); return(factory.Create(appHostContext.ServiceProvider)); }