简体   繁体   English

没有为此对象定义无参数构造函数。 当我尝试使用 Unity 依赖时

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

Here i wana to add Unit dependency soi had take 3-Projects as Univers(MVC),Moon(ClassLib),Service(ClassLib)its Responsible for Unit Dependecy在这里,我想添加单元依赖项,因此我将 3 个项目作为 Univers(MVC)、Moon(ClassLib)、Service(ClassLib) 负责单元依赖项

Moon Here i had Take 1-Interface 2-ClassFile月亮在这里我有 Take 1-Interface 2-ClassFile

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

Service.cs服务.cs

Here i had Take 1-IService 2-Service 3-ContainerBootstrap.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 ContainerBootstrap.cs

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

        }

Now i Come to Universe its my MVC File Here i had Take UnityDependencyResolver .cs file and wrote some code现在我来到 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**

Now i had taken a classfile as UnityConfig.cs in App_Start现在我在 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 Now I register that File in Global as Global.asax现在我在 Global 中将该文件注册为

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

Now I implement in HomeController.cs现在我在 HomeController.cs 中实现

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

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

I faced this Problem already And also i'm with Mr.j...Comment....Here you did not Register your HomeCOntroller Please Add this我已经遇到了这个问题而且我和j先生在一起......评论......在这里你没有注册你的家庭控制器请添加这个

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Unity:没有为此对象定义无参数构造函数。 - Unity : No parameterless constructor defined for this object. 获取“未为此对象定义无参数的构造函数。”发布视图模型时出错 - Getting “No parameterless constructor defined for this object.” Error while posting a viewmodel 没有为此对象定义无参数构造函数。 MVC 5 - No parameterless constructor defined for this object. MVC 5 依赖注入:没有为此定义无参数构造函数 object - Dependency Injection: No parameterless constructor defined for this object CBO.FillCollection抛出“没有为此对象定义的无参数构造函数。”错误 - CBO.FillCollection throwing “No parameterless constructor defined for this object.” error 错误“未为此对象定义无参数的构造函数。” ViewModel中的SelectList错误 - Error “No parameterless constructor defined for this object.” with SelectList from ViewModel System.MissingMethodException:没有为此对象定义的无参数构造函数。 MVC4 - System.MissingMethodException: No parameterless constructor defined for this object. MVC4 “没有为此对象定义无参数的构造函数。”对于使用结构映射的控制器中的IBus - “No parameterless constructor defined for this object.” for IBus in controller using StructureMap 没有为此对象定义无参数构造函数 - No parameterless constructor defined for this object 添加Owin后,依赖注入中断,没有为此对象定义的无参数构造函数 - Dependency Injection broken after adding Owin, no parameterless constructor defined for this object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM