This repository has been archived by the owner on Dec 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Fixes #4014] Add overload to AddControllerAsServices that uses the d…
…efault controller discovery logic. * Added ControllerFeature and ControllerFeatureProvider to perform controller discovery. * Changed controller discovery to use application parts. * Changed ControllerActionDescriptorProvider to make use of Application parts. * Simplified AddControllerAsServices to not accept any parameter and perform controller discovery through the ApplicationPartManager in the IMvcBuilder and IMvcCoreBuilder. Assemblies should be added to the ApplicationPartManager in order to discover controllers in them.
- Loading branch information
Showing
24 changed files
with
918 additions
and
859 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/Microsoft.AspNetCore.Mvc.Core/ApplicationParts/IApplicationPartTypeProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System.Collections.Generic; | ||
using System.Reflection; | ||
|
||
namespace Microsoft.AspNetCore.Mvc.ApplicationParts | ||
{ | ||
/// <summary> | ||
/// Exposes a set of types from an <see cref="ApplicationPart"/>. | ||
/// </summary> | ||
public interface IApplicationPartTypeProvider | ||
{ | ||
/// <summary> | ||
/// Gets the list of available types in the <see cref="ApplicationPart"/>. | ||
/// </summary> | ||
IEnumerable<TypeInfo> Types { get; } | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/Microsoft.AspNetCore.Mvc.Core/Controllers/ControllerFeature.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System.Collections.Generic; | ||
using System.Reflection; | ||
using Microsoft.AspNetCore.Mvc.ApplicationParts; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Microsoft.AspNetCore.Mvc.Controllers | ||
{ | ||
/// <summary> | ||
/// The list of controllers types in an MVC application. The <see cref="ControllerFeature"/> can be populated | ||
/// using the <see cref="ApplicationPartManager"/> that is available during startup at <see cref="IMvcBuilder.PartManager"/> | ||
/// and <see cref="IMvcCoreBuilder.PartManager"/> or at a later stage by requiring the <see cref="ApplicationPartManager"/> | ||
/// as a dependency in a component. | ||
/// </summary> | ||
public class ControllerFeature | ||
{ | ||
/// <summary> | ||
/// Gets the list of controller types in an MVC application. | ||
/// </summary> | ||
public IList<TypeInfo> Controllers { get; } = new List<TypeInfo>(); | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
src/Microsoft.AspNetCore.Mvc.Core/Controllers/ControllerFeatureProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
using Microsoft.AspNetCore.Mvc.ApplicationParts; | ||
|
||
namespace Microsoft.AspNetCore.Mvc.Controllers | ||
{ | ||
/// <summary> | ||
/// Discovers controllers from a list of <see cref="ApplicationPart"/> instances. | ||
/// </summary> | ||
public class ControllerFeatureProvider : IApplicationFeatureProvider<ControllerFeature> | ||
{ | ||
private const string ControllerTypeNameSuffix = "Controller"; | ||
|
||
/// <inheritdoc /> | ||
public void PopulateFeature( | ||
IEnumerable<ApplicationPart> parts, | ||
ControllerFeature feature) | ||
{ | ||
foreach (var part in parts.OfType<IApplicationPartTypeProvider>()) | ||
{ | ||
foreach (var type in part.Types) | ||
{ | ||
if (IsController(type) && !feature.Controllers.Contains(type)) | ||
{ | ||
feature.Controllers.Add(type); | ||
} | ||
} | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Determines if a given <paramref name="typeInfo"/> is a controller. | ||
/// </summary> | ||
/// <param name="typeInfo">The <see cref="TypeInfo"/> candidate.</param> | ||
/// <returns><code>true</code> if the type is a controller; otherwise <code>false</code>.</returns> | ||
protected virtual bool IsController(TypeInfo typeInfo) | ||
{ | ||
if (!typeInfo.IsClass) | ||
{ | ||
return false; | ||
} | ||
|
||
if (typeInfo.IsAbstract) | ||
{ | ||
return false; | ||
} | ||
|
||
// We only consider public top-level classes as controllers. IsPublic returns false for nested | ||
// classes, regardless of visibility modifiers | ||
if (!typeInfo.IsPublic) | ||
{ | ||
return false; | ||
} | ||
|
||
if (typeInfo.ContainsGenericParameters) | ||
{ | ||
return false; | ||
} | ||
|
||
if (typeInfo.IsDefined(typeof(NonControllerAttribute))) | ||
{ | ||
return false; | ||
} | ||
|
||
if (!typeInfo.Name.EndsWith(ControllerTypeNameSuffix, StringComparison.OrdinalIgnoreCase) && | ||
!typeInfo.IsDefined(typeof(ControllerAttribute))) | ||
{ | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
} | ||
} |
91 changes: 0 additions & 91 deletions
91
src/Microsoft.AspNetCore.Mvc.Core/Controllers/DefaultControllerTypeProvider.cs
This file was deleted.
Oops, something went wrong.
46 changes: 0 additions & 46 deletions
46
src/Microsoft.AspNetCore.Mvc.Core/Controllers/StaticControllerTypeProvider.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.