2017-02-03 107 views
-2

在这里,我瓦纳添加单元相关SOI已经采取3项目为UNIVERS(MVC),月亮(CLASSLIB),服务(CLASSLIB)及其负责单位Dependecy没有为此对象定义的无参数构造函数。当我尝试团结依赖

月亮 在这里,我有取1-接口2-ClassFile的

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

Service.cs

这里我不得不采取1- IService -2-服务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>(); 

     } 

现在我来宇宙的我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** 

现在我已经采取了类文件作为Unity_Config.cs in App_Start

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中 现在我注册全球该文件作为

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(); 
     } 
+0

为什么减去在这里,我想趁自己错误的无参数化的发现 –

+1

错误是告诉你什么问题..听起来像你缺少一个空的构造函数在你的一个类.. – MethodMan

+0

但我认为每一件事情都很好,如果你做错了任何错误,你可以plz建议我在哪里做什么错误... –

回答

0

实现我面对这个问题已经同时,我敢与Mr.j. ..Comment ....在这里,您没有注册您的HomeController 请加入这个

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

oooo谢谢soo多为你的价值信息 –

相关问题