本文介绍了Mvc 5统一依赖注入:我的代码中有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 您好我第一次在示例项目中使用依赖注入和工作单元。 IUnitOfWork.cs Hi I am first time using dependency injection and unit of work in sample project.IUnitOfWork.csusing System;using WDCS.BusinessLogic.Infrastructure.Interfaces.UserRepo;namespace WDCS.BusinessLogic.Infrastructure.Interfaces{ public interface IUnitOfWork : IDisposable { IUserRepository UesrRepository { get; } void SaveChages(); }} UnitOfWork.cs UnitOfWork.csusing System;using WDCS.BusinessLogic.Infrastructure.Interfaces;using WDCS.BusinessLogic.Infrastructure.Interfaces.UserRepo;using WDCS.Data;using WDCS.SqlRepository.UserRepo;namespace WDCS.SqlRepository{ public class UnitOfWork : IUnitOfWork { public WDCSContext db = null; public UnitOfWork() { db = new WDCSContext(); } public IUserRepository _uesrRepository; public IUserRepository UesrRepository { get { if (_uesrRepository == null) { _uesrRepository = new UserRepository(db); } return _uesrRepository; } } public void SaveChages() { db.SaveChanges(); } private bool disposed = false; protected virtual void Dispose(bool disposing) { if (!this.disposed) { if (disposing) { db.Dispose(); } } this.disposed = true; } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } }} UnityConfig.cs UnityConfig.csusing System.Web.Mvc;using Microsoft.Practices.Unity;using Unity.Mvc5;using WDCS.BusinessLogic.Infrastructure.Interfaces;using WDCS.SqlRepository;namespace WDCS.WebApi{ public static class UnityConfig { public static void RegisterComponents() {var container = new UnityContainer(); // register all your components with the container here // it is NOT necessary to register your controllers // e.g. container.RegisterType<ITestService, TestService>(); container.RegisterType<IUnitOfWork, UnitOfWork>(); DependencyResolver.SetResolver(new UnityDependencyResolver(container)); } }} LoginController.cs LoginController.csusing WDCS.BusinessLogic.Infrastructure.Interfaces;using WDCS.Data;namespace WDCS.WebApi.Controllers{ public class LoginController : ApiController { private IUnitOfWork _uow = null; public LoginController(IUnitOfWork uow) { _uow = uow; } [HttpPost] public IHttpActionResult Login([FromBody] User user) { var res = _uow.UesrRepository.Login(user.userName, user.password); if (res != null) return Ok(res); else return BadRequest("Invalid Username or password"); } [HttpGet] public IHttpActionResult Ping() { return Ok(_uow.UesrRepository.GetAll()); } }} 但是当我运行应用程序并试图点击 http:// localhost / WDCS / api / Login / ping [ ^ ]收到以下错误。 but when I am running the app and trying to hit http://localhost/WDCS/api/Login/ping[^] getting following error.{"Message":"An error has occurred.","ExceptionMessage":"An error occurred when trying to create a controller of type 'LoginController'. Make sure that the controller has a parameterless public constructor.","ExceptionType":"System.InvalidOperationException","StackTrace":" at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)\r\n at System.Web.Http.Controllers.HttpControllerDescriptor.CreateController(HttpRequestMessage request)\r\n at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()","InnerException":{"Message":"An error has occurred.","ExceptionMessage":"Type 'WDCS.WebApi.Controllers.LoginController' does not have a default constructor","ExceptionType":"System.ArgumentException","StackTrace":" at System.Linq.Expressions.Expression.New(Type type)\r\n at System.Web.Http.Internal.TypeActivator.Create[TBase](Type instanceType)\r\n at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage request, Type controllerType, Func`1& activator)\r\n at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)"}} 请帮我解决这个问题。 我尝试了什么: 我尝试过这些链接上的任何内容 http://sarangasl.blogspot.in/2015/04/mvc-5 -with-unity-for-dependency.html http://www.dotnet-tricks.com/Tutorial/dependencyinjection/632V140413-Dependency-Injection-in-ASP.NET-MVC-4-使用-Unity-IoC-Container.html 我遵循相同的但无法弄清楚究竟是什么问题。Please help me to solve this issue.What I have tried:I have tried whatever I got on these linkshttp://sarangasl.blogspot.in/2015/04/mvc-5-with-unity-for-dependency.htmlhttp://www.dotnet-tricks.com/Tutorial/dependencyinjection/632V140413-Dependency-Injection-in-ASP.NET-MVC-4-using-Unity-IoC-Container.htmlI followed the same but not able to figure out what is the exact issue.推荐答案 经过大量的搜索后得到了解决方案。上面也是正确的但这对于简单的MVC我需要MVC WebAPI。这里是解决方案的链接 Dep ASP.NET Web API 2中的依赖注入ASP.NET站点 [ ^ ] After doing lot of search got the solution. Above is also right but this for simple MVC and I needed for MVC WebAPI. here is link for solutionDependency Injection in ASP.NET Web API 2 | The ASP.NET Site[^] 您无需更改代码中的任何内容,只需使用安装包Unity.WebApi。希望它有所帮助!You no need to change anything in your code but just use install-package Unity.WebApi instead. Hope it helps! 这篇关于Mvc 5统一依赖注入:我的代码中有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-25 18:51