Background
Web services are being used as an interface for application to application communication. This communication can be considered as communicating data or communicating Objects if you talk in Object Oriented Contexts. The thing that I would like you to note here is that I assume that Object is a wrapper of data along with container of business logic. In this article, I am going to point towards a simple problem in communicating objects using web services.
Introduction
If you design a distributed application that shares data as well of objects, one of the necessary things that you want to take care of is how to share objects among different parts of your application. Here I am going to discuss how to return a user defined data type from a web service as one of the steps for communicating objects among applications. For that, I make a small scenario as follows: We have a class library that contains the definition for a type called User. This user contains two properties called Username
and Password
. I want to create a web service that returns this type to my console application having reference of the same User
class library.
Implementation
I just simply jump into coding and create my User Library as follows:
namespace UserLibrary
{
public class User
{
public User ( )
{
}
public User ( String userName , String password )
{
this.userName = userName;
this.password = password;
}
private String password;
public String Password
{
get { return password; }
set { password = value; }
}
private String userName;
public String UserName
{
get { return userName; }
set { userName = value; }
}
}
}
Compile this code as a DLL and take care of it.
Now create a simple web service and add a reference of this library to it. In this manner, we can create a user type User in this web service.
Then I make a simple method that gets a user type as input and make a small change in its state and returns the same Object.
[WebMethod]
public UserLibrary.User GetNewUser ( UserLibrary.User user )
{
user.UserName = "New " + user.UserName ;
user.Password = "New " + user.Password;
return user;
}
Here comes our so called Client (a console application that consumes the service). I just added the web reference and user library reference because I want to share object among client and service. After that, I just created a User object and sent it to service for being changed.
User user = new User ( "my name","my password");
user = new UserService.Service ( ).GetNewUser ( user );
Console.WriteLine ( user.UserName +" "+user.Password );
Actual Problem
Oops!!! I got an error. I have shared the same library between client and service but why am I getting this error?! Error is InvalidCastException
states that the User type on both ends is different whereas we have used the same reference. So what the problem is? And what can be used as a solution?
Ryan Farley gives a very cool reason and easy solution for it.
If we think about it, when we looked at the proxy class in the Reference.cs file, it defined the return of the GetNewUser method as a User object as defined in the proxy. So if we try to cast it as our User object then we are the ones that are using the wrong types according to the proxy class. All makes sense now, right?
Solution
Open the Solution Explorer of your client where you have added the web reference. It would be like this:
Show all hidden items by pressing the following button:
You may see Reference.cs generated by Visual Studio represents the service for your application. Try to find a definition for class user in that proxy file. Remove that definition from your proxy class so that your reference can return a proper type. That is it. Just compile the code and now you are on the proper track. Just share the same library among both server and client.
History
- 14th October, 2006: Initial post