C# 难以使用WebAPI和OWIN实现统一依赖注入
当我尝试在我的应用程序上调用控制器API端点时,出现以下错误:C# 难以使用WebAPI和OWIN实现统一依赖注入,c#,asp.net-web-api2,unity-container,owin,C#,Asp.net Web Api2,Unity Container,Owin,当我尝试在我的应用程序上调用控制器API端点时,出现以下错误: { "Message": "An error has occurred.", "ExceptionMessage": "An error occurred when trying to create a controller of type 'ProviderController'. Make sure that the controller has a parameterless public constructor
{
"Message": "An error has occurred.",
"ExceptionMessage": "An error occurred when trying to create a controller of type 'ProviderController'. 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 'mynamespace.api.controllers.ProviderController' does not have a default constructor",
"ExceptionType": "System.ArgumentException",
"StackTrace": " at System.Linq.Expressions.Expression.New(Type type)\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)"
}
}
UnityConfig.cs:
public static class UnityConfig
{
#region Unity Container
/// <summary>
/// Load types in Unity Container
/// </summary>
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
/// <summary>
/// Register the types into Unity Container.
/// </summary>
/// <param name="container"></param>
public static void RegisterTypes(UnityContainer container)
{
container.RegisterType<IProviderService, ProviderService();
}
}
ProviderService.cs:
public ProviderService()
{
this.serviceClient = new ServiceClientAdapter();
}
我花了一上午的时间通读各种各样的文章,但我不知道哪里出了问题。如果我删除Unity并直接在控制器中实例化一个新的ProviderService,一切都会正常工作。我的问题是一个小疏忽。Startup.cs中对UseWebApi的调用没有将HttpConfiguration配置对象作为参数,而是声明它自己的。这意味着WebAPI和Unity配置了不同的HTTPConfiguration对象,因此WebAPI没有意识到Unity。答案已经存在!只是为了简单或简短的代码 您可以编写单一方法
public static void RegisterComponents()
{
var container = new UnityContainer();
// register all your components with the container here
// it is NOT necessary to register your controllers
container.RegisterType<IProviderService, ProviderService>();
GlobalConfiguration.Configuration.DependencyResolver = new UnityDependencyResolver(container);
}
然后添加UnityConfig.RegisterComponents();进入全局文件我想我可能有类似的问题。我尝试从启动类将配置对象注入DependencyResolver,但没有成功!您是如何解决这个问题的?如果您看看我上面的示例,对于Startup.cs,第一行的config对象用于配置Unity。然后,当调用app.UseWebApi时,将创建一个新的配置对象并将其传递给它。这就是问题所在,应该传入第一行声明的配置。谢谢兄弟!这有帮助。
public ProviderService()
{
this.serviceClient = new ServiceClientAdapter();
}
public static void RegisterComponents()
{
var container = new UnityContainer();
// register all your components with the container here
// it is NOT necessary to register your controllers
container.RegisterType<IProviderService, ProviderService>();
GlobalConfiguration.Configuration.DependencyResolver = new UnityDependencyResolver(container);
}
public void Configuration(IAppBuilder app)
{
ConfigureOAuth(app);
HttpConfiguration config = GlobalConfiguration.Configuration;
app.UseWebApi(config);
}