Skip to content

Commit

Permalink
Issue #278 DefaultLoader now prefilters attributes before instantiating
Browse files Browse the repository at this point in the history
  • Loading branch information
jinek authored and Tratcher committed Jun 26, 2019
1 parent 0f6dc4b commit 40de801
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/Owin.Loader/DefaultLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,17 +186,24 @@ private Tuple<Type, string> SearchForStartupAttribute(string friendlyName, IList
Assembly matchedAssembly = null;
foreach (var assembly in _referencedAssemblies)
{
object[] attributes;
Attribute[] attributes;
try
{
attributes = assembly.GetCustomAttributes(inherit: false);
// checking attribute's name first and only then instantiating it
// then we are filtering attributes by name second time as inheritors could be added by calling to GetCustomAttributes(type)
attributes = assembly.GetCustomAttributesData()
.Where(data => MatchesStartupAttribute(data.AttributeType))
.Select(data => data.AttributeType)
.SelectMany(type => assembly.GetCustomAttributes(type))
.Distinct()
.ToArray();
}
catch (CustomAttributeFormatException)
{
continue;
}

foreach (var owinStartupAttribute in attributes.Where(attribute => attribute.GetType().Name.Equals(Constants.OwinStartupAttribute, StringComparison.Ordinal)))
foreach (var owinStartupAttribute in attributes.Where(attribute => MatchesStartupAttribute(attribute.GetType())))
{
Type attributeType = owinStartupAttribute.GetType();
foundAnyInstances = true;
Expand Down Expand Up @@ -265,6 +272,11 @@ private Tuple<Type, string> SearchForStartupAttribute(string friendlyName, IList
}
return fullMatch;
}

private static bool MatchesStartupAttribute(Type type)
{
return type.Name.Equals(Constants.OwinStartupAttribute, StringComparison.Ordinal);
}

// Search for any assemblies with a Startup or [AssemblyName].Startup class.
private Tuple<Type, string> SearchForStartupConvention(IList<string> errors)
Expand Down

0 comments on commit 40de801

Please sign in to comment.