private void RegisterByConvention(IEnumerable <Assembly> assemblies) { foreach (var assembly in assemblies.ToList()) { if (RegisteredAssemblies.Contains(assembly)) { continue; } var interfaces = (from type in assembly.GetTypes() where type.IsInterface select type).ToList(); var tempRegistrations = (from @interface in interfaces from type in assembly.GetTypes() where !type.IsAbstract && type.IsClass && @interface.IsAssignableFrom(type) && !type.GetCustomAttributes(typeof(ExcludeFromRegistrationAttribute), true).Any() select new { Interface = @interface, Implementation = type }).ToList(); // get interfaces with only one implementation var registrations = (from r in tempRegistrations group r by r.Interface into grp where grp.Count() == 1 select new { Interface = grp.Key, grp.First().Implementation }).ToList(); registrations.ForEach(f => ServiceCollection.AddTransient(f.Interface, f.Implementation)); RegisterHandlers(assembly); RegisterOneWayHandlers(assembly); RegisterPostRegistrationTasks(assembly); RegisterCleanupTasks(assembly); } }
private void PrepareContainer() { if (ContainerPrepared) { return; } var builder = new ContainerBuilder(); foreach (var registeredFile in GetAssembliesDetail()) { try { builder.RegisterAssemblyModules(Assembly.LoadFrom(registeredFile.Location)); RegisteredAssemblies.Add(registeredFile); } catch (Exception ex) { //Could not register assembly System.Diagnostics.Trace.WriteLine( $"{nameof(Resolver)}: Faild to load Assembly - {registeredFile.Name} from {registeredFile.Location} - Exception message: {ex.Message}"); } } _container = builder.Build(); ContainerPrepared = true; }
internal void Reset() { ContainerPrepared = false; _container = null; RootAssembliesNames.Clear(); RegisteredAssemblies.Clear(); }
static Assembly ResolveAssemblyHandler(object sender, ResolveEventArgs e) { AssemblyName an = new AssemblyName(e.Name); string dynamic_base = AppDomain.CurrentDomain.SetupInformation.DynamicBase; string compiled = Path.Combine(dynamic_base, an.Name + ".compiled"); string asmPath; if (!File.Exists(compiled)) { string fn = an.FullName; if (!RegisteredAssemblies.Find((uint)fn.GetHashCode(), fn, out asmPath)) { return(null); } } else { PreservationFile pf; try { pf = new PreservationFile(compiled); } catch (Exception ex) { throw new HttpException( String.Format("Failed to read preservation file {0}", an.Name + ".compiled"), ex); } asmPath = Path.Combine(dynamic_base, pf.Assembly + ".dll"); } if (String.IsNullOrEmpty(asmPath)) { return(null); } Assembly ret = null; try { ret = Assembly.LoadFrom(asmPath); } catch (Exception) { // ignore } return(ret); }
internal string GetContextDetail() { var builder = new StringBuilder(); builder.AppendLine($"{nameof(ContainerPrepared)}: {ContainerPrepared}"); builder.AppendLine($"{nameof(RootAssembliesNames)}: {string.Join(",", RootAssembliesNames)}"); if (RegisteredAssemblies.Any()) { builder.AppendLine("Registered Assemblies"); foreach (var registeredFile in RegisteredAssemblies) { builder.AppendLine($"FileName:{registeredFile.Name} - Location: {registeredFile.Location}"); } } else { builder.AppendLine("No Assembly is Registered"); } return(builder.ToString()); }