Click here to Skip to main content
14,484,935 members

How to Return a User Defined Object from Webservice

Rate this:
2.75 (6 votes)
Please Sign up or sign in to vote.
2.75 (6 votes)
14 Oct 2006CPOL
How to return a user defined Object from webservice


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.


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.


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.

Image 1

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.

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?


Open the Solution Explorer of your client where you have added the web reference. It would be like this:

Image 2

Show all hidden items by pressing the following button:

Image 3

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.


  • 14th October, 2006: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

interface Mirror
Team Leader
United Arab Emirates United Arab Emirates
No Biography provided

Comments and Discussions

GeneralMy vote of 1 Pin
yugender30-Apr-10 0:52
Memberyugender30-Apr-10 0:52 
Questionhow to implement this in Web Application? Pin
iamdking14-Dec-09 23:42
Memberiamdking14-Dec-09 23:42 
AnswerRe: how to implement this in Web Application? Pin
r verma1-Mar-10 1:05
Memberr verma1-Mar-10 1:05 
GeneralTIP: If you keep Getting error "The type or namespace name 'User' could not be found ", just add the directive using UserLibrary; at the top of both Reference.cs in Webservice and to the console application file Pin
Ngonidzashe Munyikwa12-Jul-08 6:02
MemberNgonidzashe Munyikwa12-Jul-08 6:02 
QuestionA question from Gilles Pin
Sean Ewington9-May-07 7:46
editorSean Ewington9-May-07 7:46 
GeneralOnly temporary solution! Pin
joe.se16-Oct-06 4:28
Memberjoe.se16-Oct-06 4:28 
GeneralRe: Only temporary solution! Pin
interface Mirror17-Oct-06 21:07
Memberinterface Mirror17-Oct-06 21:07 
GeneralThanks Pin
Nathan Evans15-Oct-06 2:03
MemberNathan Evans15-Oct-06 2:03 
GeneralRe: Thanks Pin
interface Mirror15-Oct-06 2:07
Memberinterface Mirror15-Oct-06 2:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Posted 14 Oct 2006


29 bookmarked