簡體   English   中英

沒有為此對象定義無參數構造函數。 當我嘗試使用 Unity 依賴時

[英]No parameterless constructor defined for this object. while i try for Unity Dependency

在這里,我想添加單元依賴項,因此我將 3 個項目作為 Univers(MVC)、Moon(ClassLib)、Service(ClassLib) 負責單元依賴項

月亮在這里我有 Take 1-Interface 2-ClassFile

 public interface IMoon
    {
        string Gotomoon();
    }
  public class MoonImp : IMoon
    {
        public string Gotomoon()
        {
           return"Hello moon how r u.....";
        }
    }

服務.cs

在這里我有 Take 1-IService 2-Service 3-ContainerBootstrap.cs

public interface IService
    {
        string Gotomoon();
    }
 public class ServiceImp : IService
    {
        private IMoon Moon;


        public ServiceImp(IMoon moons)
        {
            this.Moon = moons; 
        }
        public string Gotomoon()
        {
            return Moon.Gotomoon();
        }

ContainerBootstrap.cs

 public static void RegisterTypes(IUnityContainer container)
        {
            container
                     .RegisterType<IMoon, MoonImp>()
                     .RegisterType<IService, ServiceImp>();

        }

現在我來到 Universe,它是我的 MVC 文件,在這里我使用 UnityDependencyResolver .cs 文件並編寫了一些代碼

 public class UnityDependencyResolver : IDependencyResolver
    {
        private readonly IUnityContainer container;


        public UnityDependencyResolver(IUnityContainer container)
        {
            this.container = container;
        }
        public object GetService(Type serviceType)
        {
            if (!this.container.IsRegistered(serviceType))
            {
                return null;
            }
            else
            {
               return this.container.Resolve(serviceType);
            }
        }

        public IEnumerable<object> GetServices(Type serviceType)
        {
            if (!this.container.IsRegistered(serviceType))
            {
                return new List<object>();
            }
            else
                return this.container.ResolveAll(serviceType);
        }
 **UnityConfig.cs**

現在我在 App_Start 中取了一個類文件作為 UnityConfig.cs

public class UnityConfig
    {
        private static Lazy<IUnityContainer> container = new Lazy<IUnityContainer>(()=> 
        {
            IUnityContainer container = new UnityContainer();
            RegisterType(container);
            return container;
        });


        public static IUnityContainer Getconfiguration()
        {
            return container.Value;
        }

        public static void RegisterType(IUnityContainer container)
        {
            ContainerBootstrap.RegisterTypes(container);
        }
    }

Global.asax現在我在 Global 中將該文件注冊為

  DependencyResolver.SetResolver(new UnityDependencyResolver(UnityConfig.Getconfiguration()));

現在我在 HomeController.cs 中實現

private  IService serviced;
        public HomeController(IService service)
        {
            this.serviced = service;
        }
        public ActionResult Index()
        {

            ViewBag.msg = serviced.Gotomoon();
            return View();
        }

我已經遇到了這個問題而且我和j先生在一起......評論......在這里你沒有注冊你的家庭控制器請添加這個

 public static void RegisterType(IUnityContainer container)
        {
            ContainerBootstrap.RegisterTypes(container);
            container.RegisterType<HomeController>();
        }

暫無
暫無

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

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