Click here to Skip to main content
Click here to Skip to main content

How to Return a User Defined Object from Webservice

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

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

License

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

Share

About the Author

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

Comments and Discussions

 
GeneralMy vote of 1 Pinmemberyugender30-Apr-10 1:52 
Questionhow to implement this in Web Application? Pinmemberiamdking15-Dec-09 0:42 
AnswerRe: how to implement this in Web Application? Pinmemberrohan991-Mar-10 2: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 PinmemberNgonidzashe Munyikwa12-Jul-08 7:02 
QuestionA question from Gilles PinstaffSean Ewington9-May-07 8:46 
GeneralOnly temporary solution! Pinmemberjoe.se16-Oct-06 5:28 
GeneralRe: Only temporary solution! Pinmemberinterface Mirror17-Oct-06 22:07 
GeneralThanks PinmemberNathan Evans15-Oct-06 3:03 
GeneralRe: Thanks Pinmemberinterface Mirror15-Oct-06 3:07 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141220.1 | Last Updated 14 Oct 2006
Article Copyright 2006 by interface Mirror
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid