简体   繁体   中英

Error after updating Entity from Database: No parameterless constructor defined for this object

Has anyone ever seen something like this:

I've taken over coding an existing solution, which was running along just fine until I added a new table to the database, and then updated the entity from the DB. Everything seemed fine, until run-time, when all of a sudden I get this error on the home controller:

No parameterless constructor defined for this object.

From the stack trace...

[InvalidOperationException: An error occurred when trying to create a controller of type 'Response.Controllers.HomeController'. Make sure that the controller has a parameterless public constructor.]

The thing is, the home controller didn't change. It never did have a parameterless constructor. Further, all the references to it take parameters, so I'm not even sure where/why it thinks it needs a constructor that is parameterless.

Really confused.

Let me know if I need to add something more to help solve this. I'm kinda shooting in the dark 'cause I've never seen something like before.

EDIT:

Here's the constructor:

        public HomeController([Dependency("ImageUploadPath")] string imageUploadPath, 
            IPermissions permissions, 
            IResponseEntitiesFactory responseEntities, 
            IResponseWebConfiguration responseWebConfiguration,
            ILog log)
            : base(imageUploadPath, permissions, responseWebConfiguration)
        {
            _responseEntitiesFactory = responseEntities;
            _responseWebConfiguration = responseWebConfiguration;
            _log = log;
        }

EDIT #2:

The soluiton is using the Unity DI framework. Here's IUnityContainer ...

public class Bootstrapper
{

public static IUnityContainer BootstrapUnity()
    {
        IUnityContainer container = new UnityContainer();

        container.RegisterInstance(LogManager.GetLogger(typeof(Bootstrapper)));
        var log = container.Resolve<ILog>();

        container.RegisterType<IResponseQueueConfiguration, ResponseQueueConfiguration>();
        var configuration = container.Resolve<IResponseQueueConfiguration>();

        container.RegisterType<IMessageDispatcherManager, MessageDispatcherManager>();
        container.RegisterType<ResponseTimeHelper, ResponseTimeHelper>(new InjectionConstructor(log)); // hint for unity to default to the parameter less constructor (it tries to default to the IResonseEntities version!)
        container.RegisterType<IActivityQueue, MSMQActivityQueue>(new InjectionConstructor(configuration.QueueName));
        container.RegisterType<IActivityService, LeadActivityService>();
        container.RegisterType<IDispositionDiagnosticService, DispositionDiagnosticService>();
        container.RegisterType<IHttpPoxService, HttpPoxService>(new InjectionConstructor(configuration.UrbanScienceDispositionService, log));
        container.RegisterType<ICreateWorkflowTasksService, CreateWorkflowTasksService>();
        container.RegisterType<INewLeadNotifications, NewLeadNotifications>();


        container.RegisterType<IResponseEntities, ResponseEntities>(new PerThreadLifetimeManager());
        container.RegisterInstance<IResponseEntitiesFactory>(new SimpleResponseEntitiesFactory(() => new ResponseEntities()));
        container.RegisterInstance(container.Resolve<IResponseEntitiesFactory>().Create());

        container.RegisterType<ITokenReplacementService, TokenReplacementService>();
        container.RegisterType<IMessageProcessor, SendEmailMessageProcessor>("sendemail");
        container.RegisterType<IMessageProcessor, SendNewLeadNotificationsMessageProcessor>("sendnewleads");
        container.RegisterType<ITokenReplacementService, TokenReplacementService>();
        container.RegisterType<ISmtpConfiguration, SmtpConfiguration>();

        return container;
    }
}

Right now I don't see registrations for IPermissions , IResponseWebConfiguration and imageUploadPath . There's either another registration file used, or they are not registered.

You can add container.ResolveType<T> for each of the types I mentioned in the place where your registrations happen after the last registration and check, if any exception is thrown. Also you can manually check container in debug and see all the registrations for your project and determine what is missing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM