none
請問None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' ...的問題癥結為何? RRS feed

  • 問題

  • 請問各位大大 本程式使用語言為C# Mvc,發生以下問題:

    '/SONY/FUN2012' 應用程式中發生伺服器錯誤。
    None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'FUN2012.Controllers.SYMController' can be invoked with the available services and parameters:
    Cannot resolve parameter 'Library.Interface.ISYM SYM' of constructor 'Void .ctor(Library.Interface.ISYM)'.
    描述: 在執行目前 Web 要求的過程中發生未處理的例外狀況。請檢閱堆疊追蹤以取得錯誤的詳細資訊,以及在程式碼中產生的位置。 

    例外狀況詳細資訊: Autofac.Core.DependencyResolutionException: None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'FUN2012.Controllers.SYMController' can be invoked with the available services and parameters:
    Cannot resolve parameter 'Library.Interface.ISYM SYM' of constructor 'Void .ctor(Library.Interface.ISYM)'.

    原始程式錯誤: 

    在執行目前 Web 要求期間,產生未處理的例外狀況。如需有關例外狀況來源與位置的資訊,可以使用下列的例外狀況堆疊追蹤取得。

    堆疊追蹤: 


    [DependencyResolutionException: None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'FUN2012.Controllers.SYMController' can be invoked with the available services and parameters:
    Cannot resolve parameter 'Library.Interface.ISYM SYM' of constructor 'Void .ctor(Library.Interface.ISYM)'.]
       Autofac.Core.Activators.Reflection.ReflectionActivator.ActivateInstance(IComponentContext context, IEnumerable`1 parameters) +594
       Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable`1 parameters) +86
       Autofac.Core.Resolving.InstanceLookup.Execute() +63
       Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, IComponentRegistration registration, IEnumerable`1 parameters) +171
       Autofac.Core.Resolving.ResolveOperation.Execute(IComponentRegistration registration, IEnumerable`1 parameters) +108
       Autofac.ResolutionExtensions.TryResolveService(IComponentContext context, Service service, IEnumerable`1 parameters, Object& instance) +122
       Autofac.ResolutionExtensions.ResolveOptionalService(IComponentContext context, Service service, IEnumerable`1 parameters) +70
       Autofac.Integration.Mvc.AutofacDependencyResolver.GetService(Type serviceType) +95
       System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +61

    [InvalidOperationException: 嘗試建立型別 'FUN2012.Controllers.SYMController' 的控制器時發生錯誤。請確認此控制器是否有無參數公用建構函式。]
       System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +242
       System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName) +103
       System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory) +263
       System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +77
       System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +1122
       System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step) +213
       System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +131

    相關檔案ISYM檔案如下:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Library.Interface
    {
        public interface ISYM
        {
            int id { get; set; }
            string ass_name { get; set; }
            string unitname { get; set; }
            string jobtitle { get; set; }
            string phone { get; set; }
            string faxphone { get; set; }
            string email { get; set; }
            string gender { get; set; }
            string servicestate { get; set; }
            string highestedu { get; set; }
            string mainexp { get; set; }
            string address { get; set; }
            string joinyears { get; set; }
            string picfile { get; set; }
            string memo { get; set; }
            bool enable { get; set; }
            string creuser { get; set; }
            System.DateTime credate { get; set; }
            string edituser { get; set; }
            System.DateTime editdate { get; set; }
            string tel { get; set; }
            void add();
            void edit();
            void delete();
            //IQueryable<ISYM> GetAll();
            IList<ISYM> getDataList();
           ISYM getSYMId(int? RoleId);
        }
    }

    相關檔案SYMController內容如下:

    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Entity;
    using System.Linq;
    using System.Net;
    using System.Web;
    using System.Web.Mvc;
    using FUN2012.Models;
    using Library.EFClass;
    using Library.Interface;
    using Model;
    
    namespace FUN2012.Controllers
    {
        public class SYMController : Controller
        {
            ISYM _ap;
    
            public SYMController(ISYM SYM)
            {
    
                _ap = SYM;
            }
            // GET: SYM
            public ActionResult Index()
            {
                SYMViewModel SYM = new SYMViewModel();
                IList<ISYM> list = _ap.getDataList();
    
                var query = (from item in list
                             select new SYMViewModel
                             {
                編號 = item.id,
                委員姓名 = item.ass_name,
                性別 = item.gender,
                機關名稱 = item.unitname,
                職稱 = item.jobtitle,
                在職情形 = item.servicestate,
                連絡電話 = item.phone
            }).ToList();
    
                return View(query);
            }
    
            [HttpPost]
            public ActionResult Index(string search)
            {
               IList<ISYM> list = _ap.getDataList();
    
    
                var query = (from item in list
                             select new SYMViewModel
                             {
                                 編號 = item.id,
                                 委員姓名 = item.ass_name,
                                 性別 = item.gender,
                                 機關名稱 = item.unitname,
                                 職稱 = item.jobtitle,
                                 在職情形 = item.servicestate,
                                 連絡電話 = item.phone
                             });
    
                query = query.Where(a => a.委員姓名.Contains(search)).ToList();
                return View(query);
            }
    
          
            public ActionResult Details(int id)
            {
    
                var IAP = _ap.getDataList();
                SYMViewModel APVM = new SYMViewModel();
                APVM.編號 = _ap.id;
                APVM.委員姓名 = _ap.ass_name;
                APVM.機關名稱 = _ap.unitname;
    
                APVM.性別 = _ap.gender;
                APVM.職稱 = _ap.jobtitle;
                APVM.在職情形 = _ap.servicestate;
                APVM.連絡電話 = _ap.phone;
                return View(APVM);
            }
           
            private void checkNull(SYM item)
            {
                if (string.IsNullOrEmpty(item.ass_name)) this.ModelState.AddModelError("", "請輸入委員姓名");
                if (string.IsNullOrEmpty(item.phone) && string.IsNullOrEmpty(item.tel)) this.ModelState.AddModelError("", "請輸入手機或市話");
                if (!string.IsNullOrEmpty(item.phone)) { if (item.phone.Length > 50) this.ModelState.AddModelError("", "手機輸入文字過長,請小於50個字"); }
                if (!string.IsNullOrEmpty(item.tel)) { if (item.tel.Length > 50) this.ModelState.AddModelError("", "市話輸入文字過長,請小於50個字"); }
            }
    
            public ActionResult returnIndex()
            {
                return RedirectToAction("Index");
            }
    
            public ActionResult Create()
            {
                return View();
            }
            [HttpPost]
    
            public ActionResult Create([Bind(Include = "編號,委員姓名,機關名稱,性別,職稱,在職情形,連絡電話")] SYMViewModel apvm)
            {
                _ap.id = apvm.編號;
                _ap.ass_name = apvm.委員姓名;
                _ap.unitname = apvm.機關名稱;
                _ap.gender = apvm.性別;
                _ap.jobtitle = apvm.職稱;
                _ap.servicestate = apvm.在職情形;
                _ap.phone = apvm.連絡電話;
                _ap.add();
                return RedirectToAction("Index");
            }
    ...

    請問各位大大如何解決,謝謝各位大大!


    • 已編輯 Eden Knight 2018年8月20日 上午 02:53 修正
    2018年8月20日 上午 02:35

解答

  • 為SYMController加入預設建構函式再試看看:

    public class SYMController : Controller
    {
        public SYMController()
        {
        }
       ...
    }
    

    • 已標示為解答 Eden Knight 2018年9月13日 上午 06:01
    2018年8月20日 上午 02:49

所有回覆

  • 為SYMController加入預設建構函式再試看看:

    public class SYMController : Controller
    {
        public SYMController()
        {
        }
       ...
    }
    

    • 已標示為解答 Eden Knight 2018年9月13日 上午 06:01
    2018年8月20日 上午 02:49
  • 已解決,謝謝 tihs大大。

    結果如下:

    namespace FUN2012.Controllers
    {    public class SYNController : Controller
        {
            ISYM _sm;
            ISYD _sd;
            public SYMController()
            {
                _ap = new LISYM();
                _ad = new LISYD();
            }
            public int pageSize = 15;
            public ActionResult Index(int? page, string name)
            {
                IList<ISYM(> list = _sm.getDataList();
                var query = (from item in list
                             select newSYMViewModel
                             {
                                 id = item.id,
                                 ass_name = item.ass_name,
                                 gender = item.gender,
                                 unitname = item.unitname,
                                 jobtitle = item.jobtitle,
                                 servicestate = item.servicestate,
                                 phone = item.phone,
                                 enable=item.enable
                             }).ToList();
                foreach (var item in query)
                {
                    item.ass_name = item.ass_name == null ? "" : item.ass_name;
                }
                if (!string.IsNullOrEmpty(name))
                {
                    query = query.Where(a => a.ass_name.Contains(name)).ToList();
                }
                query = query.Where(a => a.enable==true).ToList();
                var result = query
                    .OrderByDescending(x => x.ass_name)
                    .ThenBy(x => x.ass_name).ToPagedList(page ?? 1, pageSize);
                var num = query.Count();
                ViewBag.pageSize = pageSize;
                ViewBag.page = page;
                ViewBag.num = num;
                ViewBag.count = query.Count();
                ViewBag.name = name;
                return View(result);
            }

    2018年9月13日 上午 06:07