Click here to Skip to main content
6,305,776 members and growing! (17,549 online)
Email Password   helpLost your password?
Web Development » Web Services » General     Intermediate

How to return a user defined Object from webservice

By interface Mirror

How to return a user defined Object from webservice
C#, Windows, .NET, Visual Studio, Dev
Posted:14 Oct 2006
Views:30,672
Bookmarked:21 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
5 votes for this article.
Popularity: 1.63 Rating: 2.33 out of 5
2 votes, 40.0%
1
1 vote, 20.0%
2

3
1 vote, 20.0%
4
1 vote, 20.0%
5

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 like you to note here is that I assume Object as a wrapper of data along with container of business logic. During this article I am going to point toward 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 how to share objects among different parts of your application. Here I am going to discuss is 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 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 same User class library.

Implementation

I just simply jump into coding and create my User Library as follow:

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 reference of this library to it. In this manner we can create and 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 send it to service for being change.


 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 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 you solution Explorer of your client where you have added web reference. It would be like this:



Show all hidden items by pressing following button:



You may see Reference.cs generated by visual studio and represents the service for your application. Try to find definition for class user in that proxy file. Remove that definition from your proxy class so that your reference can return proper type. That is it. Just compile the code and now you are on proper track. Just share same library among both server and client.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

interface Mirror


Member

Occupation: Team Leader
Location: United Arab Emirates United Arab Emirates

Other popular Web Services articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 6 of 6 (Total in Forum: 6) (Refresh)FirstPrevNext
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 Munyikwa7:02 12 Jul '08  
QuestionA question from Gilles PinstaffSean Ewington8:46 9 May '07  
GeneralOnly temporary solution! Pinmemberjoe.se5:28 16 Oct '06  
GeneralRe: Only temporary solution! Pinmemberinterface Mirror22:07 17 Oct '06  
GeneralThanks PinmemberNathan Evans3:03 15 Oct '06  
GeneralRe: Thanks Pinmemberinterface Mirror3:07 15 Oct '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 14 Oct 2006
Editor:
Copyright 2006 by interface Mirror
Everything else Copyright © CodeProject, 1999-2009
Web12 | Advertise on the Code Project