I've been working with NInject in my Wep API2 project without problems so far, but I'm struggling with a weird situation now.
I have a controller that receives 3 parameters for setting it's dependencies. The constructor looks like this:
public UsersController(IUsersServices userServices, IConfigurationServices configServices, IUsersAPIProxy usersProxy)
{
this.configServices = configServices;
this.userServices = userServices;
this.usersProxy = usersProxy;
}
The dependency that can´t be resolved is the latest one: IUsersAPIProxy. There are no issues with the other 2. Off course, the bindings for the dependencies are defined, like this:
kernel.Bind<IUsersAPIProxy>().To<UsersAPIProxy>().InRequestScope();
kernel.Bind<IUsersServices>().To<UsersServices>().InRequestScope();
kernel.Bind<IConfigurationServices>().To<ConfigurationServices>().InRequestScope();
The IUsersAPIProxy interface is very simple:
public interface IUsersAPIProxy
{
UserModel ReadUser(string loginName);
IEnumerable<UserModel> ReadAllUsers();
}
The class UsersApiProxy doesn't have any dependencies that need to be injected:
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));
}
}
Its base class is nothing special either:
public class APIProxyBase
{
protected RestClient client = null;
public APIProxyBase()
{
client = new RestClient("http://myHost:MyPort/");
}
}
For a reason I don't know if I remove the IUsersAPIProxy parameter from the controller's constructor, it works as expected. The weird thing is that it only happens with this dependency. Changing the parameter to a different position of course that won't help. Also, the other interfaces and classes that actually work are much more complex. One thing worth to mention is that the types that work are defined at a different dll that the types that don't work. Needless to say that the web api is correctly referencing both dlls. The exception I get is as follows:
Message: An error occurred when trying to create a controller of type 'UsersController'. Make sure that the controller has a parameterless public constructor.
Stack Trace: en System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType) en System.Web.Http.Controllers.HttpControllerDescriptor.CreateController(HttpRequestMessage request) en System.Web.Http.Dispatcher.HttpControllerDispatcher.d__1.MoveNext()
Could someone point me to the right direction please?
Thanks!
Its base class is nothing special either:
public class APIProxyBase
{
protected RestClient client = null;
public APIProxyBase()
{
client = new RestClient("http://myHost:MyPort/");
}
}
I think you are mistaken about this - there is something special going on here. You are newing up a dependency during the construction phase of your application. This dependency may rely on runtime data in order to function (e.g. HttpContext), which isn't available at application startup.
To test out this theory, comment the line that instantiates RestClient to see if your dependency is injected.
public class APIProxyBase
{
protected RestClient client = null;
public APIProxyBase()
{
//client = new RestClient("http://myHost:MyPort/");
}
}
It's possible that the object fails to instantiate due to an error that is being swallowed by Ninject.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With