Make AddControllersAsServices not overwrite existing IControllerTypeProvider

This commit is contained in:
Ajay Bhargav Baaskaran 2016-02-12 12:49:17 -08:00
parent 910f0139f9
commit fd6d28d9eb
5 changed files with 87 additions and 1 deletions

View File

@ -26,7 +26,14 @@ namespace Microsoft.AspNetCore.Mvc.Internal
throw new ArgumentNullException(nameof(types));
}
var controllerTypeProvider = new StaticControllerTypeProvider();
StaticControllerTypeProvider controllerTypeProvider = null;
controllerTypeProvider = services
.Where(s => s.ServiceType == typeof(IControllerTypeProvider))
.Select(s => s.ImplementationInstance)
.OfType<StaticControllerTypeProvider>()
.FirstOrDefault()
?? new StaticControllerTypeProvider();
foreach (var type in types)
{

View File

@ -0,0 +1,48 @@
// 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 Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.Extensions.DependencyInjection;
using Xunit;
namespace Microsoft.AspNetCore.Mvc.Internal
{
public class ControllerAsServicesTest
{
[Fact]
public void AddControllerAsServices_MultipleCalls_RetainsPreviouslyAddedTypes()
{
// Arrange
var services = new ServiceCollection();
// Act 1
ControllersAsServices.AddControllersAsServices(services, new Type[] { typeof(ControllerOne) });
// Assert 1
var serviceDescriptor = Assert.Single(services, s => s.ServiceType == typeof(IControllerTypeProvider));
var controllerTypeProvider = Assert.IsType<StaticControllerTypeProvider>(serviceDescriptor.ImplementationInstance);
var expectedControllerType = Assert.Single(controllerTypeProvider.ControllerTypes);
// Act 2
ControllersAsServices.AddControllersAsServices(services, new Type[] { typeof(ControllerTwo) });
// Assert 2
serviceDescriptor = Assert.Single(services, s => s.ServiceType == typeof(IControllerTypeProvider));
controllerTypeProvider = Assert.IsType<StaticControllerTypeProvider>(serviceDescriptor.ImplementationInstance);
Assert.Equal(2, controllerTypeProvider.ControllerTypes.Count);
Assert.Same(expectedControllerType, controllerTypeProvider.ControllerTypes[0]);
Assert.Same(typeof(ControllerTwo), controllerTypeProvider.ControllerTypes[1]);
}
private class ControllerOne
{
}
private class ControllerTwo
{
}
}
}

View File

@ -104,5 +104,18 @@ namespace Microsoft.AspNetCore.Mvc.FunctionalTests
// Assert
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
}
[Fact]
public async Task AddControllersAsServices_MultipleCalls_DoesNotReplacePreviousProvider()
{
// Arrange
var expected = "1";
// Act
var response = await Client.GetStringAsync("http://localhost/another/");
// Assert
Assert.Equal(expected, response);
}
}
}

View File

@ -0,0 +1,17 @@
// 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 Microsoft.AspNetCore.Mvc;
namespace ControllersFromServicesWebSite
{
[Route("/[controller]")]
public class AnotherController : Controller
{
[HttpGet]
public int Get()
{
return 1;
}
}
}

View File

@ -17,6 +17,7 @@ namespace ControllersFromServicesWebSite
{
services
.AddMvc()
.AddControllersAsServices(typeof(AnotherController))
.AddControllersAsServices(new[]
{
typeof(TimeScheduleController).GetTypeInfo().Assembly