我收到错误"没有为此对象定义的无参数构造函数。对象类型' DependencyInjectionExample.Models.ProteinTrackingService"当我访问以下网址http://localhost:57213/ProteinTracker/时。我已经实现了依赖注入并使用Unity,但出于某种原因,当我尝试访问ProteinTracker控制器时会出现此错误。请找到以下代码
public class ProteinTrackerController : Controller
{
private IProteinTrackingService proteinTrackingService ;
public ProteinTrackerController(IProteinTrackingService proteinTrackingService)
{
this.proteinTrackingService = proteinTrackingService;
}
// GET: ProteinTracker
public ActionResult Index(ProteinTrackingService model)
{
model.Total = proteinTrackingService.Total;
return View(model);
}
// GET: ProteinTracker/Details/5
[HttpPost]
public ActionResult AddProtein(ProteinTrackingService model)
{
proteinTrackingService.AddProtein(model.Total);
model.Total = proteinTrackingService.Total;
return View("Index",model);
}
}
ProteinTrackingService
public class ProteinTrackingService : IProteinTrackingService
{
private IProteinRepository repository;
public ProteinTrackingService(IProteinRepository repository)
{
this.repository = repository;
}
public int Total
{
get { return repository.GetData(new DateTime().Date).Total; }
set { repository.SetTotal(new DateTime().Date, value); }
}
//public int Goal
//{
// get { return repository.GetData(new DateTime().Date).Goal; }
// set { repository.SetGoal(),value; }
//}
public void AddProtein(int amount)
{
Total += amount;
}
}
ProteinRepository
public class ProteinRepository : IProteinRepository
{
private static ProteinData data = new ProteinData();
public ProteinData GetData(DateTime date)
{
return data;
}
public void SetTotal(DateTime date, int value)
{
data.Total = value;
}
public void SetGoal(DateTime date, int value)
{
data.Goal = value;
}
}
UnityConfig
public class UnityConfig
{
#region Unity Container
private static Lazy<IUnityContainer> container = new Lazy<IUnityContainer>(() =>
{
var container = new UnityContainer();
RegisterTypes(container);
return container;
});
/// <summary>
/// Gets the configured Unity container.
/// </summary>
public static IUnityContainer GetConfiguredContainer()
{
return container.Value;
}
#endregion
public static void RegisterTypes(IUnityContainer container)
{
// NOTE: To load from web.config uncomment the line below. Make sure to add a Microsoft.Practices.Unity.Configuration to the using statements.
//container.LoadConfiguration();
// TODO: Register your types here
container.RegisterType<IProteinTrackingService, ProteinTrackingService>();
container.RegisterType<IProteinRepository, ProteinRepository>();
}
}
public static class UnityWebActivator
{
/// <summary>Integrates Unity when the application starts.</summary>
public static void Start()
{
var container = UnityConfig.GetConfiguredContainer();
FilterProviders.Providers.Remove(FilterProviders.Providers.OfType<FilterAttributeFilterProvider>().First());
FilterProviders.Providers.Add(new UnityFilterAttributeFilterProvider(container));
DependencyResolver.SetResolver(new UnityDependencyResolver(container));
// TODO: Uncomment if you want to use PerRequestLifetimeManager
// Microsoft.Web.Infrastructure.DynamicModuleHelper.DynamicModuleUtility.RegisterModule(typeof(UnityPerRequestHttpModule));
}
/// <summary>Disposes the Unity container when the application is shut down.</summary>
public static void Shutdown()
{
var container = UnityConfig.GetConfiguredContainer();
container.Dispose();
}
}