Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 没有为此对象定义无参数构造函数。当我尝试统一依赖时_C# - Fatal编程技术网

C# 没有为此对象定义无参数构造函数。当我尝试统一依赖时

C# 没有为此对象定义无参数构造函数。当我尝试统一依赖时,c#,C#,在这里,我想添加单元依赖性,soi已经接受了3个项目,如Univers(MVC)、Moon(ClassLib)、Service(ClassLib),它们负责单元依赖性 月亮 这里我有一个1接口2类文件 public interface IMoon { string Gotomoon(); } public class MoonImp : IMoon { public string Gotomoon() {

在这里,我想添加单元依赖性,soi已经接受了3个项目,如Univers(MVC)、Moon(ClassLib)、Service(ClassLib),它们负责单元依赖性

月亮 这里我有一个1接口2类文件

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

在这里,我使用了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();
        }
 public static void RegisterTypes(IUnityContainer container)
        {
            container
                     .RegisterType<IMoon, MoonImp>()
                     .RegisterType<IService, ServiceImp>();

        }
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();
        }
 public static void RegisterTypes(IUnityContainer container)
        {
            container
                     .RegisterType<IMoon, MoonImp>()
                     .RegisterType<IService, ServiceImp>();

        }
现在我在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>();
        }
公共静态无效注册表类型(IUnityContainer容器)
{
ContainerBootstrap.RegisterTypes(容器);
container.RegisterType();
}

为什么减号在这里我遇到了一个错误,因为没有参数化的结果。这个错误告诉你问题出在哪里。。听起来你在你的一个类中缺少了一个空构造函数。但是我认为如果你犯了错误,一切都很好。你能告诉我我犯了什么错误吗?当然你至少可以指出错误所在的那一行吗?为什么这看起来不重要?在这里,我完全挂起了执行错误命中的位置,为什么?在这里,我完全支持这种依赖注入
 public static void RegisterType(IUnityContainer container)
        {
            ContainerBootstrap.RegisterTypes(container);
            container.RegisterType<HomeController>();
        }