Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generic dependencies are not automatically resolved #382

Open
TonyTroeff opened this issue Jun 22, 2020 · 0 comments
Open

Generic dependencies are not automatically resolved #382

TonyTroeff opened this issue Jun 22, 2020 · 0 comments
Assignees
Labels
Milestone

Comments

@TonyTroeff
Copy link

Describe your issue
Following your tutorial, I am registering all the necessary services in StartUp and TestStartup as well. However, they are generic and I believe that there is some problem with their resolving because it simply does not work out for me.

Project files
This is the generic controller feature provider we have configured.

    public class GenericControllerFeatureProvider : IApplicationFeatureProvider<ControllerFeature>
    {
        private readonly string _entityIdType;

        public GenericControllerFeatureProvider(IConfiguration runningConfiguration)
        {
            if (runningConfiguration == null)
                throw new ArgumentNullException(nameof(runningConfiguration));

            this._entityIdType = runningConfiguration["EntityIdType"];

            if (string.IsNullOrWhiteSpace(this._entityIdType))
                throw new Exception(ExceptionMessages.YouMustSpecifyEntityIdTypeInAppsettings);
        }

        public void PopulateFeature(IEnumerable<ApplicationPart> applicationParts, ControllerFeature controllerFeature)
        {
            var publicTypes = typeof(GenericControllerFeatureProvider).Assembly.GetExportedTypes();
            var baseControllerType = typeof(IMyGenericController<>);

            var genericControllers =
                publicTypes.Where(
                        t => t.ContainsGenericParameters
                            && t.IsClass
                            && t.GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == baseControllerType))
                    .ToList();

            Type[] typeArgs = { Type.GetType(this._entityIdType) };

            foreach (var genericController in genericControllers)
            {
                var genericType = genericController.MakeGenericType(typeArgs).GetTypeInfo();
                controllerFeature.Controllers.Add(genericType);
            }
        }
    }

This controller model convention is used to beautify the controller name if it is displayed in Swagger dor example.

    public class GenericControllerAttribute : Attribute, IControllerModelConvention
    {
        public void Apply(ControllerModel controller)
        {
            if (controller is null)
                throw new ArgumentNullException(nameof(controller));

            if (string.IsNullOrWhiteSpace(controller.ControllerName))
                return;

            var controllerName = controller.ControllerName;
            var characterIndex = controllerName.IndexOf("Controller`", StringComparison.InvariantCultureIgnoreCase);

            if (characterIndex >= 1)
            {
                var controllerTypeName = controllerName.Substring(0, characterIndex);
                controller.ControllerName = controllerTypeName;
            }
        }
    }

Startup classes
This is the TestStartup class - nothing special.

    public class TestStartup : Quantum.DMS.API.Startup
    {
        public TestStartup(IConfiguration configuration)
            : base(configuration)
        {
        }

        public void ConfigureTestServices(IServiceCollection services)
        {
            base.ConfigureServices(services);
        }
    }

Expected behavior
Calling a controller action without .Withdependencies(...) should work appropriately - take services internally from ASP and pass them to the requested controller's constructor)

Environment:

  • OS: Windows 10
  • ASP.NET Core Version 3.1
@TonyTroeff TonyTroeff added the bug label Jun 22, 2020
@ivaylokenov ivaylokenov added this to the 5.0.0 milestone Jul 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants