Reputation: 531
I have this services project that is used for some backend work. I'm trying to create a monitor that will obviously monitor the services and make sure they are working as they should. I'm using a web form for the monitor, and Ninject seems to not want to inject the properties at all(the object comes back null). So, here's the breakdown:
In the model that's being used for one method as a datacontract, here is the code for that:
[DataContract]
public class UserSummary
{
public UserSummary();
public UserSummary(User user);
[DataMember]
public AccountStatusValue AccountStatus { get; set; }
[DataMember]
public AudienceValue Audience { get; set; }
[DataMember]
public string EmailAddress { get; set; }
[DataMember]
public int ID { get; set; }
[DataMember]
public Guid Identifier { get; set; }
[DataMember]
public Name Name { get; set; }
[DataMember]
public string Username { get; set; }
}
So there are a number of properties there to fill up. Here is the Interface method that's being used to call this:
UserSummary GetUserSummaryInternal(string username);
I do have the other parts built up properly(everything is working), just trying to get a monitor going for it. Now, here is the web form code behind:
partial class ServicesMonitor : System.Web.UI.MasterPage
{
[Inject]
IUserManagementService Service { get; set; }
public ServicesMonitor(IUserManagementService service)
{
this.Service = service;
}
public ServicesMonitor()
{
}
protected void Page_Load(object sender, EventArgs e)
{
UserSummary summary = Service.GetUserSummaryInternal("testing993");
string message = "Up";
Response.Write(message);
Response.End();
return;
}
}
There's not much code in the code behind right now because I can't get Ninject working for it. I've tried various different ways, using the [Inject] tag, not using it, having constructors, etc. etc. I do also have the proper bindings for the interface i'm using in a module, which like i said before, works fine on the apps that are using this service library, I've also added a binding to the NinjectWebCommon file in the App_Start folder, but yet to no avail:
private static void RegisterServices(IKernel kernel)
{
kernel.Bind<ServiceLibrary.Service.IUserManagementService>().To<ServiceLibrary.Service.UserManagementService>();
kernel.Bind<ServiceLibrary.Service.IIntegrationService>().To<ServiceLibrary.Service.IntegrationService>();
kernel.Load(new List<string> { "ServiceLibrary.dll" });
}
. How can Ninject not work on a single aspx web form within a service library project? I'm baffled...
Upvotes: 0
Views: 406
Reputation: 531
Answering my own question, because well, occam's razor, often times the simplest solution is the correct one. So here's what I found, and I'm pretty sure other people are experiencing this as well. If you take a look at the code on the code behind file, where the object is "trying" to be injected:
[Inject]
IUserManagementService Service { get; set; }
This is what it looked like originally. Do you notice anything amiss? looked pretty normal to me, and there were no compile errors, or any error of any kind. Well, upon racking my brain for some time, and taking a break to clear my mind, I came to the conclusion that it makes perfect sense that Ninject obviously cannot see what it's trying to inject! The solution:
[Inject]
public IUserManagementService Service { get; set; }
Make the member public! Once it's public, Ninject can now see it, and properly inject it. All is well now. I have seen a couple other posts elsewhere that people were having this issue, and were not making the item to be injected public. My rookie mistake there.
Upvotes: 1