Ejemplo n.º 1
0
 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));
     }
 }
Ejemplo n.º 2
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.º 4
0
        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));
        }
        /// <summary>
        /// Constructors -- Registers Orleans system performance counters,
        /// plus any grain-specific activation counters that can be detected when this installer is run.
        /// </summary>
        public OrleansPerformanceCounterInstaller()
        {
            Trace.Listeners.Clear();
            var loggerFactory = CreateDefaultLoggerFactory($"{this.GetType()}.log");

            var parts = new ApplicationPartManager();

            parts.AddApplicationPartsFromAppDomain();
            parts.AddApplicationPartsFromBasePath();
            parts.AddFeatureProvider(new AssemblyAttributeFeatureProvider <GrainClassFeature>());
            var grainClassFeature = parts.CreateAndPopulateFeature <GrainClassFeature>();

            CrashUtils.GrainTypes = grainClassFeature.Classes.Select(metadata => TypeUtils.GetFullName(metadata.ClassType)).ToList();
            consumer    = new OrleansPerfCounterTelemetryConsumer(loggerFactory);
            this.logger = loggerFactory.CreateLogger <OrleansPerformanceCounterInstaller>();
        }
Ejemplo n.º 6
0
        public CounterControl(ILoggerFactory loggerFactory)
        {
            // Check user is Administrator and has granted UAC elevation permission to run this app
            var userIdent     = WindowsIdentity.GetCurrent();
            var userPrincipal = new WindowsPrincipal(userIdent);

            IsRunningAsAdministrator = userPrincipal.IsInRole(WindowsBuiltInRole.Administrator);

            var parts = new ApplicationPartManager();

            parts.AddApplicationPartsFromAppDomain();
            parts.AddApplicationPartsFromBasePath();
            parts.AddFeatureProvider(new AssemblyAttributeFeatureProvider <GrainClassFeature>());
            var grainClassFeature = parts.CreateAndPopulateFeature <GrainClassFeature>();

            CrashUtils.GrainTypes = grainClassFeature.Classes.Select(metadata => TypeUtils.GetFullName(metadata.ClassType)).ToList();

            perfCounterConsumer = new OrleansPerfCounterTelemetryConsumer(loggerFactory);
            this.logger         = loggerFactory.CreateLogger <CounterControl>();
        }