我创建了一个带有单个控制器的基本Web API项目,并尝试使用NuGet的Unity MVC Bootstrapper软件包。不幸的是,当我尝试访问URL http://localhost/api/runtypes时,它会抛出错误
{“消息”:“发生了错误。”,“ExceptionMessage”:“类型 'SE.FlexApplication.WebApi.Controllers.RunTypeController'没有 有默认值 构造函数 “ ”ExceptionType“: ”System.ArgumentException“, ”堆栈跟踪“:” 在System.Linq.Expressions.Expression.New(Type type)\ r \ n at System.Web.Http.Internal.TypeActivator.Create [TBASE](类型 instanceType)\ r \ n at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage 请求,输入controllerType,Func`1&激活者)\ r \ n at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage 请求,HttpControllerDescriptor controllerDescriptor,Type controllerType)“}
我的控制器类
public class RunTypeController : ApiController
{
private readonly IStaticTypesRepository _staticTypesRepository;
private readonly IDataModelConverter _dataModelConverter;
private readonly IBusinessModelConverter _businessModelConverter;
public RunTypeController(IStaticTypesRepository staticTypesRepository, IDataModelConverter dataModelConverter, IBusinessModelConverter businessModelConverter)
{
_staticTypesRepository = staticTypesRepository;
_dataModelConverter = dataModelConverter;
_businessModelConverter = businessModelConverter;
}
public RunType[] Get()
{
return _staticTypesRepository.GetRunTypes().Select(r => _dataModelConverter.Convert(r)).ToArray();
}
public HttpResponseMessage Post(RunType type)
{
_staticTypesRepository.AddRunType(_businessModelConverter.Convert(type));
_staticTypesRepository.Save();
var response = Request.CreateResponse(System.Net.HttpStatusCode.Created, type);
return response;
}
}
Unity激活器
[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(SE.FlexApplication.WebApi.App_Start.UnityWebActivator), "Start")]
[assembly: WebActivatorEx.ApplicationShutdownMethod(typeof(SE.FlexApplication.WebApi.App_Start.UnityWebActivator), "Shutdown")]
namespace SE.FlexApplication.WebApi.App_Start
{
/// <summary>Provides the bootstrapping for integrating Unity with ASP.NET MVC.</summary>
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));
}
/// <summary>Disposes the Unity container when the application is shut down.</summary>
public static void Shutdown()
{
var container = UnityConfig.GetConfiguredContainer();
container.Dispose();
}
}
}
Unity Config类
public class UnityConfig
{
#region Unity Container
private static readonly 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
/// <summary>Registers the type mappings with the Unity container.</summary>
/// <param name="container">The unity container to configure.</param>
/// <remarks>There is no need to register concrete types such as controllers or API controllers (unless you want to
/// change the defaults), as Unity allows resolving a concrete type even if it was not previously registered.</remarks>
public static void RegisterTypes(IUnityContainer container)
{
try
{
//container.RegisterInstance<IUnitOfWork>(new ProdUnitOfWork());
container.RegisterType<IEventRepository, EventRepository>();
container.RegisterType<IModelRepository, ModelRepository>();
container.RegisterType<ISecurityRepository, SecurityRepository>();
container.RegisterType<IStaticTypesRepository, StaticTypesRepository>();
container.RegisterType<IDataModelConverter, DataToModelConverter>();
container.RegisterType<IBusinessModelConverter, ModelToDataConverter>();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
似乎Controller没有意识到它应该使用Unity Dependency解析器进行构造函数注入。我已经按照在线教程的所有步骤进行了操作。我错过了一些基本的东西吗?