public void Start(bool strict = true) { // loading application assemblies now occurs in four phases. // 1. We scan the file system for assemblies meeting pre-determined criteria, specified in SiloAssemblyLoader.LoadApplicationAssemblies (called by the constructor). // 2. We load those assemblies into memory. In the official distribution of Orleans, this is usually 4 assemblies. var loader = new SiloAssemblyLoader(); // Generate code for newly loaded assemblies. CodeGeneratorManager.GenerateAndCacheCodeForAllAssemblies(); // (no more assemblies should be loaded into memory, so now is a good time to log all types registered with the serialization manager) SerializationManager.LogRegisteredTypes(); // 3. We scan types in memory for GrainTypeData objects that describe grain classes and their corresponding grain state classes. InitializeGrainClassData(loader, strict); // 4. We scan types in memory for grain method invoker objects. InitializeInvokerMap(loader, strict); InitializeInterfaceMap(); StreamingInitialize(); }
public Type GetGrainReferenceType(Type interfaceType) { var typeInfo = interfaceType.GetTypeInfo(); CodeGeneratorManager.GenerateAndCacheCodeForAssembly(typeInfo.Assembly); var genericInterfaceType = interfaceType.IsConstructedGenericType ? typeInfo.GetGenericTypeDefinition() : interfaceType; if (!typeof(IAddressable).IsAssignableFrom(interfaceType)) { throw new InvalidCastException( $"Target interface must be derived from {typeof(IAddressable).FullName} - cannot handle {interfaceType}"); } // Try to find the correct GrainReference type for this interface. Type grainReferenceType; if (!this.grainToReferenceMapping.TryGetValue(genericInterfaceType, out grainReferenceType)) { throw new InvalidOperationException( $"Cannot find generated {nameof(GrainReference)} class for interface '{interfaceType}'"); } if (interfaceType.IsConstructedGenericType) { grainReferenceType = grainReferenceType.MakeGenericType(typeInfo.GenericTypeArguments); } if (!typeof(IAddressable).IsAssignableFrom(grainReferenceType)) { // This represents an internal programming error. throw new InvalidCastException( $"Target reference type must be derived from {typeof(IAddressable).FullName}- cannot handle {grainReferenceType}"); } return(grainReferenceType); }
private static IGrainMethodInvoker MakeInvoker(Type interfaceType) { var typeInfo = interfaceType.GetTypeInfo(); CodeGeneratorManager.GenerateAndCacheCodeForAssembly(typeInfo.Assembly); var genericInterfaceType = interfaceType.IsConstructedGenericType ? typeInfo.GetGenericTypeDefinition() : interfaceType; // Try to find the correct IGrainMethodInvoker type for this interface. Type invokerType; if (!GrainToInvokerMapping.TryGetValue(genericInterfaceType, out invokerType)) { return(null); } if (interfaceType.IsConstructedGenericType) { invokerType = invokerType.MakeGenericType(typeInfo.GenericTypeArguments); } return((IGrainMethodInvoker)Activator.CreateInstance(invokerType)); }
public Task AddCachedAssembly(string targetAssemblyName, GeneratedAssembly cachedAssembly) { CodeGeneratorManager.AddGeneratedAssembly(targetAssemblyName, cachedAssembly); return(Task.CompletedTask); }
/// <summary>Adds a cached assembly to the code generator.</summary> /// <param name="targetAssemblyName">The assembly which the cached assembly was generated for.</param> /// <param name="cachedAssembly">The generated assembly.</param> public void AddCachedAssembly(string targetAssemblyName, GeneratedAssembly cachedAssembly) { CodeGeneratorManager.AddGeneratedAssembly(targetAssemblyName, cachedAssembly); }
private static GrainReferenceCaster MakeCaster(Type interfaceType) { var typeInfo = interfaceType.GetTypeInfo(); CodeGeneratorManager.GenerateAndCacheCodeForAssembly(typeInfo.Assembly); var genericInterfaceType = interfaceType.IsConstructedGenericType ? typeInfo.GetGenericTypeDefinition() : interfaceType; // Try to find the correct GrainReference type for this interface. Type grainReferenceType; if (!GrainToReferenceMapping.TryGetValue(genericInterfaceType, out grainReferenceType)) { throw new InvalidOperationException( string.Format("Cannot find generated GrainReference class for interface '{0}'", interfaceType)); } if (interfaceType.IsConstructedGenericType) { grainReferenceType = grainReferenceType.MakeGenericType(typeInfo.GenericTypeArguments); } // Get the grain reference constructor. var constructor = grainReferenceType.GetConstructors(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance) .Where( _ => { var parameters = _.GetParameters(); return(parameters.Length == 1 && parameters[0].ParameterType == typeof(GrainReference)); }).FirstOrDefault(); if (constructor == null) { throw new InvalidOperationException( string.Format( "Cannot find suitable constructor on generated reference type for interface '{0}'", interfaceType)); } // Construct an expression to construct a new instance of this grain reference when given another grain // reference. var createLambdaParameter = Expression.Parameter(typeof(GrainReference), "gr"); var createLambda = Expression.Lambda <Func <GrainReference, IAddressable> >( Expression.New(constructor, createLambdaParameter), createLambdaParameter); var grainRefParameter = Expression.Parameter(typeof(IAddressable), "grainRef"); var body = Expression.Call( GrainReferenceCastInternalMethodInfo, Expression.Constant(interfaceType), createLambda, grainRefParameter, Expression.Constant(GrainInterfaceUtils.GetGrainInterfaceId(interfaceType))); // Compile and return the reference casting lambda. var lambda = Expression.Lambda <GrainReferenceCaster>(body, grainRefParameter); return(lambda.Compile()); }
/// <summary> /// Processes the provided assembly. /// </summary> /// <param name="assembly">The assembly to process.</param> private static void ProcessAssembly(Assembly assembly) { string assemblyName = assembly.GetName().Name; if (Logger.IsVerbose3) { Logger.Verbose3("Processing assembly {0}", assemblyName); } // If the assembly is loaded for reflection only avoid processing it. if (assembly.ReflectionOnly) { return; } // Don't bother re-processing an assembly we've already scanned lock (ProcessedAssemblies) { if (!ProcessedAssemblies.Add(assembly)) { return; } } // If the assembly does not reference Orleans, avoid generating code for it. if (TypeUtils.IsOrleansOrReferencesOrleans(assembly)) { // Code generation occurs in a self-contained assembly, so invoke it separately. CodeGeneratorManager.GenerateAndCacheCodeForAssembly(assembly); } // Process each type in the assembly. var shouldProcessSerialization = SerializationManager.ShouldFindSerializationInfo(assembly); var assemblyTypes = TypeUtils.GetDefinedTypes(assembly, Logger).ToArray(); // Process each type in the assembly. List <Type> typesToFind = new List <Type>(assemblyTypes.Length); foreach (TypeInfo typeInfo in assemblyTypes) { try { var type = typeInfo.AsType(); string typeName = typeInfo.FullName; if (Logger.IsVerbose3) { Logger.Verbose3("Processing type {0}", typeName); } if (shouldProcessSerialization) { typesToFind.Add(type); } GrainFactory.FindSupportClasses(type); } catch (Exception exception) { Logger.Error(ErrorCode.SerMgr_TypeRegistrationFailure, "Failed to load type " + typeInfo.FullName + " in assembly " + assembly.FullName + ".", exception); } } SerializationManager.FindSerializationInfo(typesToFind); }
public void Setup() { var _ = typeof(Microsoft.CodeAnalysis.CSharp.Formatting.CSharpFormattingOptions); _codeGeneratorManager = CodeGeneratorManagerFactory.Create(new Dictionary <string, IReadOnlyList <string> >(), false); }
/// <summary> /// Processes the provided assembly. /// </summary> /// <param name="assembly">The assembly to process.</param> private static void ProcessAssembly(Assembly assembly) { string assemblyName = assembly.GetName().Name; if (Logger.IsVerbose3) { Logger.Verbose3("Processing assembly {0}", assemblyName); } // If the assembly is loaded for reflection only avoid processing it. if (assembly.ReflectionOnly) { return; } // Don't bother re-processing an assembly we've already scanned lock (ProcessedAssemblies) { if (!ProcessedAssemblies.Add(assembly)) { return; } } // If the assembly does not reference Orleans, avoid generating code for it. if (TypeUtils.IsOrleansOrReferencesOrleans(assembly)) { // Code generation occurs in a self-contained assembly, so invoke it separately. CodeGeneratorManager.GenerateAndCacheCodeForAssembly(assembly); } // Process each type in the assembly. var shouldProcessSerialization = SerializationManager.ShouldFindSerializationInfo(assembly); TypeInfo[] assemblyTypes; try { assemblyTypes = assembly.DefinedTypes.ToArray(); } catch (Exception exception) { if (Logger.IsWarning) { var message = string.Format( "AssemblyLoader encountered an exception loading types from assembly '{0}': {1}", assembly.FullName, exception); Logger.Warn(ErrorCode.Loader_TypeLoadError_5, message, exception); } return; } // Process each type in the assembly. foreach (TypeInfo type in assemblyTypes) { string typeName = type.FullName; if (Logger.IsVerbose3) { Logger.Verbose3("Processing type {0}", typeName); } if (shouldProcessSerialization) { SerializationManager.FindSerializationInfo(type); } GrainFactory.FindSupportClasses(type); } }
public void Setup() { var _ = typeof(Microsoft.CodeAnalysis.CSharp.Formatting.CSharpFormattingOptions); _codeGeneratorManager = CodeGeneratorManagerFactory.Create(null, false); }
/// <summary> /// Initializes a new instance of the <see cref="AssemblyProcessor"/> class. /// </summary> /// <param name="typeCache"> /// The type cache. /// </param> /// <param name="codeGeneratorManager"> /// The code generator. /// </param> public AssemblyProcessor(TypeMetadataCache typeCache, CodeGeneratorManager codeGeneratorManager) { this.logger = LogManager.GetLogger("AssemblyProcessor"); this.typeCache = typeCache; this.codeGeneratorManager = codeGeneratorManager; }
public TypeMetadataCache(CodeGeneratorManager codeGeneratorManager) { this.codeGeneratorManager = codeGeneratorManager; }
/// <summary> /// Initializes a new instance of the <see cref="AssemblyProcessor"/> class. /// </summary> /// <param name="typeCache"> /// The type cache. /// </param> /// <param name="serializationManager"> /// The serialization manager. /// </param> /// <param name="codeGeneratorManager"> /// The code generator. /// </param> /// <param name="logger"> the logger to use</param> public AssemblyProcessor(TypeMetadataCache typeCache, SerializationManager serializationManager, CodeGeneratorManager codeGeneratorManager, LoggerWrapper <AssemblyProcessor> logger) { this.logger = logger; this.typeCache = typeCache; this.serializationManager = serializationManager; this.codeGeneratorManager = codeGeneratorManager; }
/// <summary> /// Processes the provided assembly. /// </summary> /// <param name="assembly">The assembly to process.</param> private void ProcessAssembly(Assembly assembly) { if (assembly == null) { return; } string assemblyName = assembly.GetName().Name; if (this.logger.IsVerbose3) { this.logger.Verbose3("Processing assembly {0}", assemblyName); } #if !NETSTANDARD // If the assembly is loaded for reflection only avoid processing it. if (assembly.ReflectionOnly) { return; } #endif // Don't bother re-processing an assembly we've already scanned lock (this.processedAssemblies) { if (!this.processedAssemblies.Add(assembly)) { return; } } // If the assembly does not reference Orleans, avoid generating code for it. if (TypeUtils.IsOrleansOrReferencesOrleans(assembly)) { // Code generation occurs in a self-contained assembly, so invoke it separately. var generated = CodeGeneratorManager.GenerateAndCacheCodeForAssembly(assembly); this.ProcessAssembly(generated?.Assembly); } // Process each type in the assembly. var assemblyTypes = TypeUtils.GetDefinedTypes(assembly, this.logger).ToArray(); // Process each type in the assembly. foreach (TypeInfo typeInfo in assemblyTypes) { try { var type = typeInfo.AsType(); string typeName = typeInfo.FullName; if (this.logger.IsVerbose3) { this.logger.Verbose3("Processing type {0}", typeName); } SerializationManager.FindSerializationInfo(type); this.typeCache.FindSupportClasses(type); } catch (Exception exception) { this.logger.Error(ErrorCode.SerMgr_TypeRegistrationFailure, "Failed to load type " + typeInfo.FullName + " in assembly " + assembly.FullName + ".", exception); } } }