gpt4 book ai didi

c# - NInject 不解决特定的依赖关系

转载 作者:行者123 更新时间:2023-11-30 15:17:35 26 4
gpt4 key购买 nike

到目前为止,我一直在我的 Wep API2 项目中使用 NInject,没有遇到任何问题,但我现在遇到了一个奇怪的情况。

我有一个 Controller ,它接收 3 个参数来设置它的依赖项。构造函数如下所示:

public UsersController(IUsersServices userServices, IConfigurationServices configServices, IUsersAPIProxy usersProxy) 
{
this.configServices = configServices;
this.userServices = userServices;
this.usersProxy = usersProxy;
}

无法解决的依赖是最新的:IUsersAPIProxy。其他2个没有问题。当然,依赖项的绑定(bind)是这样定义的:

kernel.Bind<IUsersAPIProxy>().To<UsersAPIProxy>().InRequestScope();
kernel.Bind<IUsersServices>().To<UsersServices>().InRequestScope();
kernel.Bind<IConfigurationServices>().To<ConfigurationServices>().InRequestScope();

IUsersAPIProxy 接口(interface)非常简单:

public interface IUsersAPIProxy
{
UserModel ReadUser(string loginName);
IEnumerable<UserModel> ReadAllUsers();
}

UsersApiProxy 类没有任何需要注入(inject)的依赖:

public class UsersAPIProxy : APIProxyBase, IUsersAPIProxy
{
public IEnumerable<UserModel> ReadAllUsers()
{
var request = new RestRequest("sec/users2/");
IRestResponse<List<UserModel>> response = client.Execute<List<UserModel>>(request);
if (response.StatusCode == System.Net.HttpStatusCode.OK)
return response.Data;

throw new ApplicationException(String.Format("There was an error when trying to retrieve the users. Response content: {0}"));
}

public UserModel ReadUser(string loginName)
{
var request = new RestRequest("sec/users2/{loginName}");
request.AddUrlSegment("loginName", loginName);
IRestResponse<UserModel> response = client.Execute<UserModel>(request);

if (response.StatusCode == System.Net.HttpStatusCode.OK)
return response.Data;

throw new ApplicationException(String.Format("There was an error when trying to retrieve data for user {0}. Response content: {1}", loginName, response.Content));
}
}

它的基类也没什么特别的:

public class APIProxyBase
{
protected RestClient client = null;

public APIProxyBase()
{
client = new RestClient("http://myHost:MyPort/");
}
}

出于某种原因,我不知道我是否从 Controller 的构造函数中删除了 IUsersAPIProxy 参数,它按预期工作。奇怪的是,它只发生在这种依赖性下。将参数更改为其他位置当然无济于事。此外,实际工作的其他接口(interface)和类要复杂得多。值得一提的是,有效的类型定义在与无效的类型不同的 dll 中。不用说 web api 正确引用了两个 dll。我得到的异常如下:

消息:尝试创建“UsersController”类型的 Controller 时出错。确保 Controller 具有无参数公共(public)构造函数。

堆栈跟踪: zh System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage 请求,HttpControllerDescriptor controllerDescriptor,Type controllerType) zh System.Web.Http.Controllers.HttpControllerDescriptor.CreateController(HttpRequestMessage 请求) zh System.Web.Http.Dispatcher.HttpControllerDispatcher.d__1.MoveNext()

有人能给我指出正确的方向吗?

谢谢!

最佳答案

Its base class is nothing special either:

public class APIProxyBase
{
protected RestClient client = null;

public APIProxyBase()
{
client = new RestClient("http://myHost:MyPort/");
}
}

我认为您对此有误 - 这里发生了一些特别的事情。您在应用程序的构建 阶段新建依赖项。此依赖项可能依赖于运行时 数据才能运行(例如 HttpContext),这在应用程序启动时不可用。

要验证这个理论,请注释实例化 RestClient 的行以查看是否注入(inject)了您的依赖项。

public class APIProxyBase
{
protected RestClient client = null;

public APIProxyBase()
{
//client = new RestClient("http://myHost:MyPort/");
}
}

可能由于 Ninject 吞噬了一个错误,对象无法实例化。

关于c# - NInject 不解决特定的依赖关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45946606/

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