views:

1106

answers:

4

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!

+1  A: 

This type of thing can happen when using reflection, when the actual assemblies are different - either different versions, compile-time or even sometimes when using a different copy of the assembly. I'm not sure if your code uses reflection or not though...

ripper234
+1  A: 

The problem here is that the client of the web service actually is a proxy, or generated object rather than the normal type you were expecting. I think you will have to map the type into the instance you want.

Kev Hunter
+2  A: 

When creating the web reference to your web service you will get proxy classes generated for you. These proxy classes look like the classes from your library, but they are not the same types. You will need to have some method translating between the library version and the proxy versions of these types if you want to treat objects returned from the web service as types from your shared library.

Fredrik Mörk
+1  A: 

What I did is make extension methods for each class to convert them to the correct signature. Not so much fun when you have 20 business classes that are shared between 6 web services :'(