Ejemplo n.º 1
0
        /// <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));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Adds all assemblies referenced by the assemblies in the builder's <see cref="IApplicationPartManagerWithAssemblies.Assemblies"/> property.
        /// </summary>
        /// <param name="manager">The builder.</param>
        /// <returns>The builder with the additionally included assemblies.</returns>
        public static IApplicationPartManagerWithAssemblies WithReferences(this IApplicationPartManagerWithAssemblies manager)
        {
            var referencedAssemblies = new HashSet <Assembly>(manager.Assemblies);

            foreach (var scopedAssembly in manager.Assemblies)
            {
                LoadReferencedAssemblies(scopedAssembly, referencedAssemblies);
            }

            foreach (var includedAsm in referencedAssemblies)
            {
                manager.AddApplicationPart(new AssemblyPart(includedAsm));
            }

            return(new ApplicationPartManagerWithAssemblies(manager, referencedAssemblies));

            void LoadReferencedAssemblies(Assembly asm, HashSet <Assembly> includedAssemblies)
            {
                if (asm == null)
                {
                    throw new ArgumentNullException(nameof(asm));
                }

                if (includedAssemblies == null)
                {
                    throw new ArgumentNullException(nameof(includedAssemblies));
                }

                var referenced = asm.GetReferencedAssemblies();

                foreach (var asmName in referenced)
                {
                    try
                    {
                        var refAsm = Assembly.Load(asmName);
                        if (includedAssemblies.Add(refAsm))
                        {
                            LoadReferencedAssemblies(refAsm, includedAssemblies);
                        }
                    }
                    catch
                    {
                        // Ignore loading exceptions.
                    }
                }
            }
        }