Merge pull request #1760 from abpframework/maliming/patch-4

Prevent the service from being injected again.
pull/1775/head
Halil İbrahim Kalkan 6 years ago committed by GitHub
commit 64b22df27a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -9,22 +9,36 @@ namespace Volo.Abp.AspNetCore.Mvc.DependencyInjection
{
public class AbpAspNetCoreMvcConventionalRegistrar : DefaultConventionalRegistrar
{
protected override ServiceLifetime? GetServiceLifetimeFromClassHierarcy(Type type)
public override void AddType(IServiceCollection services, Type type)
{
var lifeTime = base.GetServiceLifetimeFromClassHierarcy(type);
if (lifeTime != null)
if (!IsMvcService(type))
{
return lifeTime;
return;
}
if (IsController(type) ||
IsPageModel(type) ||
IsViewComponent(type))
var lifeTime = GetMvcServiceLifetime(type);
var serviceTypes = ExposedServiceExplorer.GetExposedServices(type);
TriggerServiceExposing(services, type, serviceTypes);
foreach (var serviceType in serviceTypes)
{
return ServiceLifetime.Transient;
var serviceDescriptor = ServiceDescriptor.Describe(serviceType, type, lifeTime);
services.Add(serviceDescriptor);
}
}
return null;
protected virtual bool IsMvcService(Type type)
{
return IsController(type) ||
IsPageModel(type) ||
IsViewComponent(type);
}
protected virtual ServiceLifetime GetMvcServiceLifetime(Type type)
{
return ServiceLifetime.Transient;
}
private static bool IsPageModel(Type type)

@ -0,0 +1,54 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.DependencyInjection;
using Shouldly;
using Volo.Abp.DependencyInjection;
using Xunit;
namespace Volo.Abp.AspNetCore.Mvc.DependencyInjection
{
public class AbpAspNetCoreMvcConventionalRegistrar_Tests
{
[Fact]
public void Should_Registered_Mvc_Service()
{
//Arrange
var services = new ServiceCollection();
//Act
services.AddConventionalRegistrar(new DefaultConventionalRegistrar());
services.AddConventionalRegistrar(new AbpAspNetCoreMvcConventionalRegistrar());
services.AddTypes(typeof(My_Test_PageModel), typeof(My_Test_Controller), typeof(My_Test_ViewComponent));
//Assert
services.ShouldContainTransient(typeof(My_Test_PageModel));
services.ShouldContainTransient(typeof(My_Test_Controller));
services.ShouldContainTransient(typeof(My_Test_ViewComponent));
var serviceProvider = services.BuildServiceProvider();
serviceProvider.GetServices<My_Test_PageModel>().Count().ShouldBe(1);
serviceProvider.GetServices<My_Test_Controller>().Count().ShouldBe(1);
serviceProvider.GetServices<My_Test_ViewComponent>().Count().ShouldBe(1);
}
}
public class My_Test_PageModel : PageModel
{
}
public class My_Test_Controller : Controller
{
}
public class My_Test_ViewComponent : ViewComponent
{
}
}
Loading…
Cancel
Save