gpt4 book ai didi

c# - Unity IoC 不会将依赖项注入(inject) Web API Controller

转载 作者:IT王子 更新时间:2023-10-29 04:34:41 27 4
gpt4 key购买 nike

我刚开始使用 Unity,但我的问题是每当我调用我的 Web 服务时,我都会收到一个异常说明

“确保 Controller 有一个无参数的公共(public)构造函数”

我已经学习了多个教程,但仍然遇到同样的问题。

在我的 WebApiConfig 类的 Register 函数中,我有

var container = new UnityContainer();
container.RegisterType<IValidator, Validator>(new HierarchicalLifetimeManager());
config.DependencyResolver = new UnityResolver(container);

这是我的 UnityResolver 类

using Microsoft.Practices.Unity;
using System;
using System.Collections.Generic;
using System.Web.Http.Dependencies;

public class UnityResolver : IDependencyResolver
{
protected IUnityContainer container;

public UnityResolver(IUnityContainer container)
{
if (container == null)
{
throw new ArgumentNullException("container");
}
this.container = container;
}

public object GetService(Type serviceType)
{
try
{
return container.Resolve(serviceType);
}
catch (ResolutionFailedException)
{
return null;
}
}

public IEnumerable<object> GetServices(Type serviceType)
{
try
{
return container.ResolveAll(serviceType);
}
catch (ResolutionFailedException)
{
return new List<object>();
}
}

public IDependencyScope BeginScope()
{
var child = container.CreateChildContainer();
return new UnityResolver(child);
}

public void Dispose()
{
container.Dispose();
}
}

我没有注册任何 Controller ,因为每个教程都声称我不需要这样做。这是我的实际控制人

public class Controller: ApiController
{
private IValidator _validator;

public Controller(IValidator validator)
{
this._validator = validator;
}

[HttpPost]
public void ReceiveIPN()
{

}
}

有人知道我可能做错了什么吗?谢谢!

编辑 1:这是 Validator 类的“实现”。它很空,因为在解决 Unity 问题之前我不想在这里引入错误。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;


public class Validator: IValidator
{
public bool ValidateIPN(string body)
{
throw new NotImplementedException();
}
}

编辑 2:这是我尝试使用 Fiddler 调用 web api 路由时得到的完整错误响应

{"message":"An error has occurred.","exceptionMessage":"An error occurred when trying to create a controller of type 'Controller'. 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) at System.Web.Http.Controllers.HttpControllerDescriptor.CreateController(HttpRequestMessage request) at System.Web.Http.Dispatcher.HttpControllerDispatcher.d__1.MoveNext()","innerException":{"message":"An error has occurred.","exceptionMessage":"Type 'Project.Controller' does not have a default constructor","exceptionType":"System.ArgumentException","stackTrace":" at System.Linq.Expressions.Expression.New(Type type) at System.Web.Http.Internal.TypeActivator.Create[TBase](Type instanceType) at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage request, Type controllerType, Func`1& activator) at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)"}}

最佳答案

因此,在用头撞墙几个小时后,我发现这不起作用,因为我的项目中有一个 OWIN/Katana Startup.cs 类。现在我不知道这里到底发生了什么,所以如果能提供更多信息就更好了。

基本上,因为我使用的是 OWIN/Katana,所以我有一个 Startup.cs 文件,该文件创建了一个新的 HttpConfiguration 对象并对其进行了配置,类似于在 WebApiConfig.cs 类中完成的方式。

private void ConfigureWebApi(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
var container = new UnityContainer();
container.RegisterType<IValidator, Validator>();
config.DependencyResolver = new UnityDependencyResolver(container);

var jsonFormatter = config.Formatters.OfType<JsonMediaTypeFormatter().First();
jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
}

看起来代码首先运行 WebApiConfig 的 Register 函数,然后用 Startup.cs 文件中生成的对象覆盖 HttpConfiguration 对象。我必须将我的容器设置内容移到这里才能使其正常工作。

抱歉我之前没有提到 OWIN 的东西。这对我来说是相当新的,我没有意识到它是相关的。希望这可以避免其他人遭受我刚刚经历的痛苦。

关于c# - Unity IoC 不会将依赖项注入(inject) Web API Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29611083/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com