簡體   English   中英

從數據庫更新實體后發生錯誤:未為此對象定義無參數構造函數

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

有沒有人看過這樣的東西:

我已經接管了現有解決方案的編碼,該解決方案一直運行良好,直到我向數據庫添加了新表,然后從數據庫更新了實體。 一切似乎都很好,直到運行時,突然我在家庭控制器上收到了這個錯誤:

No parameterless constructor defined for this object.

從堆棧跟蹤...

[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.]

問題是,家庭控制器沒有改變。 它從來沒有沒有參數的構造函數。 此外,對其的所有引用都帶有參數,所以我什至不知道它為什么/為什么認為需要無參數的構造函數。

真的很困惑。

讓我知道是否需要添加更多幫助解決此問題的方法。 我有點在黑暗中射擊,因為我從未見過像以前這樣的東西。

編輯:

這是構造函數:

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

編輯#2:

解決方案正在使用Unity DI框架。 這是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;
    }
}

現在我看不到IPermissionsIResponseWebConfigurationimageUploadPath 使用了另一個注冊文件,或者未注冊。

您可以為我在上次注冊后進行注冊的地方提到的每種類型添加container.ResolveType<T> ,並檢查是否拋出任何異常。 您也可以在調試中手動檢查容器,查看項目的所有注冊並確定缺少的內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM