/// <summary> /// Returns the <see cref="IApplicationPartManager"/> for the provided context. /// </summary> /// <returns>The <see cref="IApplicationPartManager"/> belonging to the provided context.</returns> public static IApplicationPartManager GetApplicationPartManager(this IServiceCollection services) { var manager = GetServiceFromCollection <IApplicationPartManager>(services); if (manager is null) { manager = new ApplicationPartManager(); services.AddSingleton(manager); services.AddSingleton <IApplicationPartManager>(manager); manager.AddFromDependencyContext(); manager.AddFromAssemblyLoadContext(); if (GetServiceFromCollection <IHostEnvironment>(services)?.ApplicationName is string applicationName) { try { var entryAssembly = Assembly.Load(new AssemblyName(applicationName)); manager.AddApplicationPart(entryAssembly); } catch { // Ignore exceptions here. } } if (Assembly.GetEntryAssembly() is Assembly asm) { manager.AddApplicationPart(asm); } } return(manager); }
private static string GenerateSourceForAssembly(Assembly grainAssembly, LogLevel logLevel) { using (var loggerFactory = new LoggerFactory()) { var config = new ClusterConfiguration(); loggerFactory.AddConsole(logLevel); var serializationProviderOptions = Options.Create( new SerializationProviderOptions { SerializationProviders = config.Globals.SerializationProviders, FallbackSerializationProvider = config.Globals.FallbackSerializationProvider }); var applicationPartManager = new ApplicationPartManager(); applicationPartManager.AddFeatureProvider(new BuiltInTypesSerializationFeaturePopulator()); applicationPartManager.AddFeatureProvider(new AssemblyAttributeFeatureProvider <GrainInterfaceFeature>()); applicationPartManager.AddFeatureProvider(new AssemblyAttributeFeatureProvider <GrainClassFeature>()); applicationPartManager.AddFeatureProvider(new AssemblyAttributeFeatureProvider <SerializerFeature>()); applicationPartManager.AddApplicationPart(grainAssembly); applicationPartManager.AddApplicationPart(typeof(RuntimeVersion).Assembly); applicationPartManager.AddApplicationPartsFromReferences(grainAssembly); applicationPartManager.AddApplicationPartsFromReferences(typeof(RuntimeVersion).Assembly); var serializationManager = new SerializationManager(null, serializationProviderOptions, loggerFactory, new CachedTypeResolver()); serializationManager.RegisterSerializers(applicationPartManager); var codeGenerator = new RoslynCodeGenerator(serializationManager, applicationPartManager, loggerFactory); return(codeGenerator.GenerateSourceForAssembly(grainAssembly)); } }
/// <summary> /// Generates support code for the provided assembly and adds it to the builder. /// </summary> /// <param name="manager">The builder.</param> /// <param name="loggerFactory">The optional logger factory, for outputting code generation diagnostics.</param> /// <returns>A builder with support parts added.</returns> public static IApplicationPartManagerWithAssemblies WithCodeGeneration(this IApplicationPartManagerWithAssemblies manager, ILoggerFactory loggerFactory = null) { var stopWatch = Stopwatch.StartNew(); loggerFactory = loggerFactory ?? new NullLoggerFactory(); var tempPartManager = new ApplicationPartManager(); foreach (var provider in manager.FeatureProviders) { tempPartManager.AddFeatureProvider(provider); } foreach (var part in manager.ApplicationParts) { tempPartManager.AddApplicationPart(part); } tempPartManager.AddApplicationPart(new AssemblyPart(typeof(RuntimeVersion).Assembly)); tempPartManager.AddFeatureProvider(new AssemblyAttributeFeatureProvider <GrainInterfaceFeature>()); tempPartManager.AddFeatureProvider(new AssemblyAttributeFeatureProvider <GrainClassFeature>()); tempPartManager.AddFeatureProvider(new AssemblyAttributeFeatureProvider <SerializerFeature>()); tempPartManager.AddFeatureProvider(new BuiltInTypesSerializationFeaturePopulator()); var codeGenerator = new RoslynCodeGenerator(tempPartManager, loggerFactory); var generatedAssembly = codeGenerator.GenerateAndLoadForAssemblies(manager.Assemblies); stopWatch.Stop(); var logger = loggerFactory.CreateLogger("Orleans.CodeGenerator.RuntimeCodeGen"); logger?.LogInformation(0, $"Runtime code generation for assemblies {String.Join(",", manager.Assemblies.ToStrings())} took {stopWatch.ElapsedMilliseconds} milliseconds"); return(manager.AddApplicationPart(generatedAssembly)); }
/// <summary> /// Generates support code for the the provided assembly and adds it to the builder. /// </summary> /// <param name="manager">The builder.</param> /// <returns>A builder with support parts added.</returns> public static IApplicationPartManagerWithAssemblies WithCodeGeneration(this IApplicationPartManagerWithAssemblies manager) { var tempPartManager = new ApplicationPartManager(); foreach (var provider in manager.FeatureProviders) { tempPartManager.AddFeatureProvider(provider); } foreach (var part in manager.ApplicationParts) { tempPartManager.AddApplicationPart(part); } tempPartManager.AddApplicationPart(new AssemblyPart(typeof(RuntimeVersion).Assembly)); tempPartManager.AddFeatureProvider(new AssemblyAttributeFeatureProvider <GrainInterfaceFeature>()); tempPartManager.AddFeatureProvider(new AssemblyAttributeFeatureProvider <GrainClassFeature>()); tempPartManager.AddFeatureProvider(new AssemblyAttributeFeatureProvider <SerializerFeature>()); tempPartManager.AddFeatureProvider(new BuiltInTypesSerializationFeaturePopulator()); var codeGenerator = new RoslynCodeGenerator(tempPartManager, new NullLoggerFactory()); var generatedAssembly = codeGenerator.GenerateAndLoadForAssemblies(manager.Assemblies); return(manager.AddApplicationPart(generatedAssembly)); }
private static string GenerateSourceForAssembly(Assembly grainAssembly, LogLevel logLevel) { using (var loggerFactory = new LoggerFactory()) { loggerFactory.AddConsole(logLevel); var parts = new ApplicationPartManager() .AddFeatureProvider(new BuiltInTypesSerializationFeaturePopulator()) .AddFeatureProvider(new AssemblyAttributeFeatureProvider <GrainInterfaceFeature>()) .AddFeatureProvider(new AssemblyAttributeFeatureProvider <GrainClassFeature>()) .AddFeatureProvider(new AssemblyAttributeFeatureProvider <SerializerFeature>()); parts.AddApplicationPart(grainAssembly).WithReferences(); parts.AddApplicationPart(typeof(RuntimeVersion).Assembly).WithReferences(); var codeGenerator = new RoslynCodeGenerator(parts, loggerFactory); return(codeGenerator.GenerateSourceForAssembly(grainAssembly)); } }
/// <summary> /// Adds the provided <paramref name="assembly"/> as an application part. /// </summary> /// <param name="applicationPartManager">The application part manager.</param> /// <param name="assembly">The assembly.</param> public static void AddApplicationPart(this ApplicationPartManager applicationPartManager, Assembly assembly) { if (applicationPartManager == null) { throw new ArgumentNullException(nameof(applicationPartManager)); } if (assembly == null) { throw new ArgumentNullException(nameof(assembly)); } applicationPartManager.AddApplicationPart(new AssemblyPart(assembly)); }
public void RuntimeCodeGen_AddsSupportClasses() { var partManager = new ApplicationPartManager(); partManager.AddApplicationPart(typeof(IRuntimeCodeGenGrain).Assembly); partManager.AddFeatureProvider(new AssemblyAttributeFeatureProvider <GrainInterfaceFeature>()); partManager.AddFeatureProvider(new AssemblyAttributeFeatureProvider <GrainClassFeature>()); partManager.AddFeatureProvider(new AssemblyAttributeFeatureProvider <SerializerFeature>()); var interfaceFeature = new GrainInterfaceFeature(); partManager.PopulateFeature(interfaceFeature); Assert.DoesNotContain(interfaceFeature.Interfaces, i => i.InterfaceType == typeof(IRuntimeCodeGenGrain)); var classFeature = new GrainClassFeature(); partManager.PopulateFeature(classFeature); Assert.DoesNotContain(classFeature.Classes, c => c.ClassType == typeof(RuntimeCodeGenGrain)); var serializerFeature = new SerializerFeature(); partManager.PopulateFeature(serializerFeature); Assert.DoesNotContain(serializerFeature.SerializerTypes, s => s.Target == typeof(RuntimeCodeGenPoco)); partManager.AddApplicationPart(typeof(IRuntimeCodeGenGrain).Assembly).WithCodeGeneration(); interfaceFeature = new GrainInterfaceFeature(); partManager.PopulateFeature(interfaceFeature); Assert.Contains(interfaceFeature.Interfaces, i => i.InterfaceType == typeof(IRuntimeCodeGenGrain)); classFeature = new GrainClassFeature(); partManager.PopulateFeature(classFeature); Assert.Contains(classFeature.Classes, c => c.ClassType == typeof(RuntimeCodeGenGrain)); serializerFeature = new SerializerFeature(); partManager.PopulateFeature(serializerFeature); Assert.Contains(serializerFeature.SerializerTypes, s => s.Target == typeof(RuntimeCodeGenPoco)); }
private static string GenerateSourceForAssembly(Assembly grainAssembly, LogLevel logLevel) { var serviceCollection = new ServiceCollection() .AddLogging(logging => { logging .AddConsole() .SetMinimumLevel(logLevel); }); using (var services = serviceCollection.BuildServiceProvider()) { var loggerFactory = services.GetRequiredService <ILoggerFactory>(); var parts = new ApplicationPartManager() .AddFeatureProvider(new BuiltInTypesSerializationFeaturePopulator()) .AddFeatureProvider(new AssemblyAttributeFeatureProvider <GrainInterfaceFeature>()) .AddFeatureProvider(new AssemblyAttributeFeatureProvider <GrainClassFeature>()) .AddFeatureProvider(new AssemblyAttributeFeatureProvider <SerializerFeature>()); parts.AddApplicationPart(grainAssembly).WithReferences(); parts.AddApplicationPart(typeof(RuntimeVersion).Assembly).WithReferences(); var codeGenerator = new RoslynCodeGenerator(parts, loggerFactory); return(codeGenerator.GenerateSourceForAssembly(grainAssembly)); } }
/// <summary> /// Attempts to load and add assemblies from the specified directories as application parts. /// </summary> /// <param name="applicationPartManager">The application part manager.</param> /// <param name="directories">The directories to search.</param> private static void AddApplicationPartsFromProbingPath(this ApplicationPartManager applicationPartManager, params string[] directories) { if (directories == null) { throw new ArgumentNullException(nameof(directories)); } var dirs = new Dictionary <string, SearchOption>(); foreach (var dir in directories) { dirs[dir] = SearchOption.TopDirectoryOnly; } AssemblyLoaderPathNameCriterion[] excludeCriteria = { AssemblyLoaderCriteria.ExcludeResourceAssemblies }; AssemblyLoaderReflectionCriterion[] loadCriteria = { AssemblyLoaderReflectionCriterion.NewCriterion(ReferencesOrleansOrAbstractionsAssemblyPredicate) }; var loadedAssemblies = AssemblyLoader.LoadAssemblies(dirs, excludeCriteria, loadCriteria, new LoggerWrapper(nameof(ApplicationPartManagerExtensions), NullLoggerFactory.Instance)); foreach (var assembly in loadedAssemblies) { applicationPartManager.AddApplicationPart(assembly); } // Returns true if the provided assembly references the Orleans core or abstractions assembly. bool ReferencesOrleansOrAbstractionsAssemblyPredicate(Assembly assembly, out IEnumerable <string> complaints) { var referencesOrleans = assembly.GetReferencedAssemblies().Any(ReferencesOrleansOrAbstractions); complaints = referencesOrleans ? null : NoReferenceComplaint; return(referencesOrleans); bool ReferencesOrleansOrAbstractions(AssemblyName reference) { return(string.Equals(reference.Name, CoreAssemblyName, StringComparison.OrdinalIgnoreCase) || string.Equals(reference.Name, AbstractionsAssemblyName, StringComparison.OrdinalIgnoreCase)); } } }
/// <summary> /// Adds all assemblies referenced by the provided <paramref name="assembly"/> as application parts. /// </summary> /// <param name="applicationPartManager">The application part manager.</param> /// <param name="assembly">The assembly</param> public static void AddApplicationPartsFromReferences(this ApplicationPartManager applicationPartManager, Assembly assembly) { if (applicationPartManager == null) { throw new ArgumentNullException(nameof(applicationPartManager)); } if (assembly == null) { throw new ArgumentNullException(nameof(assembly)); } var processedAssemblies = new HashSet <Assembly>(); LoadReferencedAssemblies(assembly, processedAssemblies); foreach (var asm in processedAssemblies) { applicationPartManager.AddApplicationPart(asm); } }
/// <summary> /// Adds all assemblies in the current <see cref="AppDomain"/> as application parts. /// </summary> /// <param name="applicationPartManager">The application part manager.</param> /// <param name="loadReferencedAssemblies">Whether or not try to load all referenced assemblies.</param> public static void AddApplicationPartsFromAppDomain(this ApplicationPartManager applicationPartManager, bool loadReferencedAssemblies = true) { if (applicationPartManager == null) { throw new ArgumentNullException(nameof(applicationPartManager)); } var processedAssemblies = new HashSet <Assembly>(); foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies()) { if (processedAssemblies.Add(assembly) && loadReferencedAssemblies) { LoadReferencedAssemblies(assembly, processedAssemblies); } } foreach (var assembly in processedAssemblies) { applicationPartManager.AddApplicationPart(assembly); } }