我正在asp.net Web API项目中工作,并且我已经使用unity容器进行依赖项注入,但是注册似乎无法正常工作。
我已经在API控制器中创建了Get API,并将服务类注入了API控制器。当我用邮递员测试我的API时,会出现此错误。
“消息”:“发生错误。”,
“ Exceptionmessage”:“尝试创建类型为'AvailableVehicleListForWebController'的控制器时发生错误。请确保该控制器具有无参数的公共构造函数。”,
“ ExceptionType”:“ System.InvalidOperationException”
这是我的API控制器的代码。
using System;
using System.Collections.Generic;
using System.Linq;
using system.web;
using system.web.Http;
using Eyepax.MABI.Core.Entities;
using PriceEngine.WebAPI.BusinessServices;
namespace PriceEngine.WebAPI.Controllers.Controller
{
public class AvailableVehicleListForWebController : ApiController
{
private readonly IAvailableVehicleListForWebService _vehicleService;
public AvailableVehicleListForWebController(IAvailableVehicleListForWebService vehicleService)
{
_vehicleService = vehicleService;
}
[HttpGet,Route("api/controller/availablevehicleforweb")]
public int Getnumber()
{
return 5;
}
}
}
但是当我从构造函数中删除注入时,API可以正常工作。所以我认为问题出在统一容器上。
这是我的统一容器。
using Eyepax.MABI.Public.Web.Repository;
using Eyepax.MABI.Public.Web.Repository.Contracts;
using PriceEngine.WebAPI.BusinessServices;
using PriceEngine.WebAPI.Repositories;
using system.web.Http;
using Unity;
using Unity.WebApi;
namespace PriceEngine.WebAPI
{
public static class UnityConfig
{
public static void RegisterComponents()
{
var container = new UnityContainer();
container.RegisterType<IStationListDataService,StationListDataService>();
container.RegisterType<IStationListService,StationListService>();
container.RegisterType<IAvailableVehicleListForWebService,AvailableVehicleListForWebService>();
container.RegisterType<IVehicleRepository,VehicleRepository>();
GlobalConfiguration.Configuration.DependencyResolver = new UnityDependencyResolver(container);
}
}
}
有人可以帮我解决这个问题吗?