diff --git a/src/OpenIddict.Mvc/OpenIddictExtensions.cs b/src/OpenIddict.Mvc/OpenIddictExtensions.cs index a254d30a..1e954ff1 100644 --- a/src/OpenIddict.Mvc/OpenIddictExtensions.cs +++ b/src/OpenIddict.Mvc/OpenIddictExtensions.cs @@ -5,13 +5,14 @@ */ using System; +using System.Collections.Generic; using System.Diagnostics; using System.Reflection; using JetBrains.Annotations; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc.ApplicationModels; -using Microsoft.AspNetCore.Mvc.Infrastructure; +using Microsoft.AspNetCore.Mvc.ApplicationParts; using Microsoft.AspNetCore.Mvc.Razor.Compilation; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.FileProviders; @@ -53,11 +54,15 @@ namespace Microsoft.AspNetCore.Builder { var configuration = app.ApplicationServices.GetRequiredService(); services.AddMvc() - // Register the OpenIddict controller. - .AddControllersAsServices(new[] { - typeof(OpenIddictController<,>).MakeGenericType(configuration.UserType, configuration.ApplicationType) + // Note: ConfigureApplicationPartManager() must be + // called before AddControllersAsServices(). + .ConfigureApplicationPartManager(manager => { + manager.ApplicationParts.Clear(); + manager.ApplicationParts.Add(new OpenIddictPart(configuration)); }) + .AddControllersAsServices() + // Add an OpenIddict-specific convention to ensure that the generic // OpenIddictController gets an appropriate controller name. .AddMvcOptions(options => options.Conventions.Add(new OpenIddictConvention())) @@ -110,16 +115,6 @@ namespace Microsoft.AspNetCore.Builder { return container.GetRequiredService(typeof(UserManager<>).MakeGenericType(configuration.UserType)); }); - // Register the assembly provider in the isolated container. - services.AddScoped(provider => { - var accessor = provider.GetRequiredService(); - var container = (IServiceProvider) accessor.HttpContext.Items[typeof(IServiceProvider)]; - Debug.Assert(container != null); - - // Resolve the assembly provider from the parent container. - return container.GetRequiredService(); - }); - // Register the compilation service in the isolated container. services.AddScoped(provider => { var accessor = provider.GetRequiredService(); @@ -147,5 +142,20 @@ namespace Microsoft.AspNetCore.Builder { controller.ControllerName = "OpenIddict"; } } + + private class OpenIddictPart : ApplicationPart, IApplicationPartTypeProvider { + public OpenIddictPart(OpenIddictConfiguration configuration) { + Types = new[] { + typeof(OpenIddictController<,>) + .MakeGenericType(configuration.UserType, + configuration.ApplicationType) + .GetTypeInfo() + }; + } + + public override string Name { get; } = "OpenIddict.Mvc"; + + public IEnumerable Types { get; } + } } } \ No newline at end of file