[英]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.