I have a Web service and a Web site (both C#) in the same solution (For now); I also have a class library in the solution. Both the web service and the web site reference this class library.
The web service has a WebMethod that creates an object from the library and returns it. The website invokes this and attempts to put it into a Trainer object (once again, from the same library)
ProFitWebService.Service serviceConn = new ProFitWebService.Service();
ProFitLibrary.Trainer authenticatedTrainer = (ProFitLibrary.Trainer)serviceConn.GetAuthenticatedTrainer(_TrainerLogin.UserName);
however the following occurs: "Cannot convert type ProFitWebService.Trainer to ProFitLibrary.Trainer"
Here is the WebMethod:
[WebMethod]
public ProFitLibrary.Trainer GetAuthenticatedTrainer(string email)
{
ProFitLibrary.Trainer returnTrainer = new ProFitLibrary.Trainer();
SqlCommand cmd = new SqlCommand("SELECT * FROM Trainers WHERE EmailAddress = '" + email + "'", conn);
conn.Open();
SqlDataReader reader;
reader = cmd.ExecuteReader();
while (reader.Read())
{
returnTrainer.TrainerId = reader.GetInt32(reader.GetOrdinal("TrainerId"));
returnTrainer.FirstName = reader.GetString(reader.GetOrdinal("FirstName"));
returnTrainer.LastName = reader.GetString(reader.GetOrdinal("LastName"));
returnTrainer.PhoneNumber = reader.GetString(reader.GetOrdinal("PhoneNumber"));
returnTrainer.Address = reader.GetString(reader.GetOrdinal("Address"));
returnTrainer.City = reader.GetString(reader.GetOrdinal("City"));
returnTrainer.PostalCode = reader.GetString(reader.GetOrdinal("PostalCode"));
returnTrainer.EmailAddress = reader.GetString(reader.GetOrdinal("EmailAddress"));
}
return returnTrainer;
}
Update: Changing the Trainer object to ProFitWebService.Trainer on the Web site fixed the issue:
ProFitWebService.Service serviceConn = new ProFitWebService.Service();
ProFitWebService.Trainer authenticatedTrainer = (ProFitWebService.Trainer)serviceConn.GetAuthenticatedTrainer(_TrainerLogin.UserName);
I think the answer to this is simply that library objects returned from a Web Service will always be type based/prefixed on the service - and I should not reference the class Library from both the Website and the Service - I should just always create the WebService version of the object - ProFitWebService.Trainer etc.
Could someone confirm this as a standard practice when you're using libraries within a web service? or if I'm making this more difficult then it really is!