Arrao4u

…a blog by Rama Rao

Archive for the ‘UserDefinedObject From WebService’ Category

How to return a user defined Object from webservice

Posted by arrao4u on December 19, 2009

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:

Collapse
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.

Collapse
   [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.

Collapse
 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.

ref: http:

//www.codeproject.com/KB/webservices/GetObjectFromWebservice.aspx

Posted in ASPNET, UserDefinedObject From WebService, Web Services | Leave a Comment »