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

Could not find a parameterless constructor. #591

Closed
LFDCC opened this issue Jul 1, 2021 · 4 comments
Closed

Could not find a parameterless constructor. #591

LFDCC opened this issue Jul 1, 2021 · 4 comments

Comments

@LFDCC
Copy link

LFDCC commented Jul 1, 2021

I want to add a proxy to Controller ,And then I implemented this interface, IControllerActivator ,
Because my Controller does not have a parameterless constructor, the call CreateClassProxyWithTarget throws the exception. I want to know how to solve this problem

//ControllerActivator
 public class CustomControllerActivator : IControllerActivator
    {
        public object Create(ControllerContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            Type serviceType = context.ActionDescriptor.ControllerTypeInfo.AsType();

            var target = context.HttpContext.RequestServices.GetRequiredService(serviceType);

            var proxyGenerator = context.HttpContext.RequestServices.GetRequiredService<ProxyGenerator>();
            var customInterceptors = context.HttpContext.RequestServices.GetRequiredService<IEnumerable<ICustomInterceptor>>();
            var interceptors = GetInterceptors(customInterceptors);

            var proxy = proxyGenerator.CreateClassProxyWithTarget(serviceType,target,interceptors.ToInterceptors());
            //throw execption "Could not find a parameterless constructor."
            return proxy;

        }
}
//Controller
 [ApiController]
    [Route("[controller]")]
    public class WeatherForecastController : ControllerBase
    {
        private static readonly string[] Summaries = new[]
        {
            "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
        };

        private readonly ILogger<WeatherForecastController> _logger;

        ITestUser testUser;        

        public WeatherForecastController(ILogger<WeatherForecastController> logger, ITestUser testUser)
        {
            _logger = logger;
            this.testUser = testUser;
        }
}
@jonorossi
Copy link
Member

ASP.NET Core has heaps of extensibility points, I don't understand why you'd use DynamicProxy.

@LFDCC
Copy link
Author

LFDCC commented Jul 2, 2021

ASP.NET Core has heaps of extensibility points, I don't understand why you'd use DynamicProxy.

I knew that using Filter would be a better choice
I want to know how to solve this problem

@LFDCC
Copy link
Author

LFDCC commented Jul 2, 2021

ASP.NET Core has heaps of extensibility points, I don't understand why you'd use DynamicProxy.

I don't think any dynamic proxy is as good as the Filter that comes with ASP.NET Core, but I'm just wondering how to implement a proxy in Controller using castle. Core

@jonorossi
Copy link
Member

The CreateClassProxyWithTarget overload you are using expects a default constructor as documented in the XML documentation:

/// <exception cref = "ArgumentException">Thrown when no parameterless constructor exists on type
///     <paramref name = "classToProxy" />.</exception>

You'll need to use an overload that accepts object[] constructorArguments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants