webservices: simple question, accessing webservice members

H

hellrazor

Hi, (new to webservices here)

Is it possible to access a class instance variable defined in a webservice?
I want to access this variable, in addition to the data being returned by
the [webmethod].

so let's say I have the following webservice class:


public class Service1 : System.Web.Services.WebService
{

public string hello2;

public ModelServices()
{
InitializeComponent();
}

[WebMethod]
public string sayHello()
{
hello2 = "another hello to you!"
return "hello there";
}
}


................................

So when I consume this webservice, I want to be able to call the sayHello()
method, and have access to the hello2 variable after having called sayHello
() and thus expecting the "hello2" variable to contain the "another hello
to you!" value:


remoteWS.Service1 ws = new remoteWS.Service1();
string hello1 = ws.sayHello();
string hello2 = ws.hello2;



Is this possible, and how do I go about doing it? thanks!
 
D

Dan Rogers

Hi Jorge,

As you've already found out, the code you showed works fine. What you are
trying to do, however, is to implement static or shared data (singleton)
behaviors across all instances of a service method call. As such, the code
you are trying will not work between method invocations - but it WILL work
for a single method invocation.

The web service class you write is a facade of sorts that lets a stateless
method call be dispatched from a remote caller. As such, a new instance of
the web service class is implemented for each method call. You can prove
this by putting a debug break point on the IntializeComponent call in the
constructor. Each time any service method is invoked, the constructor is
called - which means that each call to the web service entry point cause a
new invocation of the class to be constructed.

To get a shared data behavior, you either have to implement applicaiton
state (within IIS) or use some other successful singleton pattern
implementation outside of your web service class. This could be a
database, a COM+ component, or some other in memory persistent store that
you can access from your own code.

I hope this helps,

Dan Rogers
Microsoft Corporation
--------------------
Subject: webservices: simple question, accessing webservice members
From: hellrazor <[email protected]>
Organization: packet monkeys inc.
Message-ID: <[email protected]>
User-Agent: Xnews/5.04.25
Newsgroups: microsoft.public.dotnet.languages.csharp,microsoft.public.dotnet.framework.w
ebservices,microsoft.public.dotnet.framework.aspnet.webservices
Date: Mon, 15 Nov 2004 17:01:25 -0800
NNTP-Posting-Host: s0106000625d84bd8.vn.shawcable.net 24.86.11.226
Lines: 1
Path: cpmsftngxa10.phx.gbl!TK2MSFTFEED02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP12
.phx.gbl
Xref: cpmsftngxa10.phx.gbl microsoft.public.dotnet.framework.webservices:7454
microsoft.public.dotnet.framework.aspnet.webservices:26582
microsoft.public.dotnet.languages.csharp:286847
X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webservices

Hi, (new to webservices here)

Is it possible to access a class instance variable defined in a webservice?
I want to access this variable, in addition to the data being returned by
the [webmethod].

so let's say I have the following webservice class:


public class Service1 : System.Web.Services.WebService
{

public string hello2;

public ModelServices()
{
InitializeComponent();
}

[WebMethod]
public string sayHello()
{
hello2 = "another hello to you!"
return "hello there";
}
}


...............................

So when I consume this webservice, I want to be able to call the sayHello()
method, and have access to the hello2 variable after having called sayHello
() and thus expecting the "hello2" variable to contain the "another hello
to you!" value:


remoteWS.Service1 ws = new remoteWS.Service1();
string hello1 = ws.sayHello();
string hello2 = ws.hello2;



Is this possible, and how do I go about doing it? thanks!
 
N

Nick Malik

you can either add another webmethod to return the new value (which is
chatty and inefficient) or you can return a class that contains both values.

For example:

public class SharedClass
{
string MyHello1;
string MyHello2;
}

public class Service1: System.Web.Services.WebService
{
// put initializer here... ommitted for brevity
[WebMethod]
public SharedClass SayHello()
{
SharedClass sc = new SharedClass();
sc.MyHello1 = "Hello There";
sc.MyHello2 = "And Again";
}
}

public class WebClient
{
public void MyClient()
{
Service1 mywebservice;
mywebservice.SayHello();
Console.WriteLine("{0}", mywebservice.MyHello1);
Console.WriteLine("{0}", mywebservice.MyHello2);
}
}

((Caveat: this code is from the top of my head... it has not been
compiled... please excuse typos))

Hope this helps,
--- Nick
 
N

Nick Malik

I screwed up a bit of that code.

Please replace the client class as follows:

public void MyClient()
{
Service1 mywebservice;
SharedClass myShared;
myShared = mywebservice.SayHello();
Console.WriteLine("{0}", myShared.MyHello1);
Console.WriteLine("{0}", myShared.MyHello2);
}

Sorry about that.

--- Nick

Nick Malik said:
you can either add another webmethod to return the new value (which is
chatty and inefficient) or you can return a class that contains both values.

For example:

public class SharedClass
{
string MyHello1;
string MyHello2;
}

public class Service1: System.Web.Services.WebService
{
// put initializer here... ommitted for brevity
[WebMethod]
public SharedClass SayHello()
{
SharedClass sc = new SharedClass();
sc.MyHello1 = "Hello There";
sc.MyHello2 = "And Again";
}
}

public class WebClient
{
public void MyClient()
{
Service1 mywebservice;
mywebservice.SayHello();
Console.WriteLine("{0}", mywebservice.MyHello1);
Console.WriteLine("{0}", mywebservice.MyHello2);
}
}

((Caveat: this code is from the top of my head... it has not been
compiled... please excuse typos))

Hope this helps,
--- Nick

hellrazor said:
Hi, (new to webservices here)

Is it possible to access a class instance variable defined in a webservice?
I want to access this variable, in addition to the data being returned by
the [webmethod].

so let's say I have the following webservice class:


public class Service1 : System.Web.Services.WebService
{

public string hello2;

public ModelServices()
{
InitializeComponent();
}

[WebMethod]
public string sayHello()
{
hello2 = "another hello to you!"
return "hello there";
}
}


...............................

So when I consume this webservice, I want to be able to call the sayHello()
method, and have access to the hello2 variable after having called sayHello
() and thus expecting the "hello2" variable to contain the "another hello
to you!" value:


remoteWS.Service1 ws = new remoteWS.Service1();
string hello1 = ws.sayHello();
string hello2 = ws.hello2;



Is this possible, and how do I go about doing it? thanks!
 
H

hellrazor

I screwed up a bit of that code.

Please replace the client class as follows:

public void MyClient()
{
Service1 mywebservice;
SharedClass myShared;
myShared = mywebservice.SayHello();
Console.WriteLine("{0}", myShared.MyHello1);
Console.WriteLine("{0}", myShared.MyHello2);
}

Sorry about that.

--- Nick

Nick Malik said:
you can either add another webmethod to return the new value (which is
chatty and inefficient) or you can return a class that contains both values.

For example:

public class SharedClass
{
string MyHello1;
string MyHello2;
}

public class Service1: System.Web.Services.WebService
{
// put initializer here... ommitted for brevity
[WebMethod]
public SharedClass SayHello()
{
SharedClass sc = new SharedClass();
sc.MyHello1 = "Hello There";
sc.MyHello2 = "And Again";
}
}

public class WebClient
{
public void MyClient()
{
Service1 mywebservice;
mywebservice.SayHello();
Console.WriteLine("{0}", mywebservice.MyHello1);
Console.WriteLine("{0}", mywebservice.MyHello2);
}
}

((Caveat: this code is from the top of my head... it has not been
compiled... please excuse typos))

Hope this helps,
--- Nick

hellrazor said:
Hi, (new to webservices here)

Is it possible to access a class instance variable defined in a webservice?
I want to access this variable, in addition to the data being
returned
by
the [webmethod].

so let's say I have the following webservice class:


public class Service1 : System.Web.Services.WebService
{

public string hello2;

public ModelServices()
{
InitializeComponent();
}

[WebMethod]
public string sayHello()
{
hello2 = "another hello to you!"
return "hello there";
}
}


...............................

So when I consume this webservice, I want to be able to call the sayHello()
method, and have access to the hello2 variable after having called sayHello
() and thus expecting the "hello2" variable to contain the "another hello
to you!" value:


remoteWS.Service1 ws = new remoteWS.Service1();
string hello1 = ws.sayHello();
string hello2 = ws.hello2;



Is this possible, and how do I go about doing it? thanks!

Hi there, thanks!

When I try to compile the client application in VS, I get the following
error:

C:\clients\test.cs(1070): Cannot implicitly convert type
'WS.ReturnValuesClass' to 'MyApp.ReturnValuesClass'


I have placed the class in the source code for both the webservice and
the client. Is there anything specific involved that I have missed?

Thanks!

Jorge
 
D

Dan Rogers

Hi Jorge,

It seems you are trying to use the service class as a stateful remote
object. This isn't going to work, since you cannot access members on the
service class in the way you are trying to.

A service method takes input parameters and returns values either thru its
parameter list or via it's return type. Think of the method like you would
a function in a function library and not having any access to state on the
server side (which for the most part is not kept).

This means you need to pass in parameters and get back data that you are
going to use, and not expect the web serivice proxy to have any internal
knowlege about the work done on the server side.

I hope this helps

Dan Rogers
Microsoft Corporation
 
N

Nick Malik

Hi there, thanks!

When I try to compile the client application in VS, I get the following
error:

C:\clients\test.cs(1070): Cannot implicitly convert type
'WS.ReturnValuesClass' to 'MyApp.ReturnValuesClass'


I have placed the class in the source code for both the webservice and
the client. Is there anything specific involved that I have missed?

Thanks!

Jorge

Hi Jorge,

That's what I get for not compiling the code first. My apologies.

The shared class should NOT be declared again in the client code. The
definition of the shared class is passed via the WSDL and is coded in the
web service generated code. You can simply declare a variable of type
WS.ReturnValuesClass and refer to the members that way.

If you want to see the code the Visual Studio generates for you when you
make a web reference, then look carefully at your project explorer window.
There are a couple of small icons in the bar just above the treeview of your
solution and project(s). One of the icons, if you hover over it, says
something like "show all files". Click it. Then look into the client
project. You will see a new code file under the web reference to the web
service. In there, you will see the definition of the shared class.

Good Luck,
--- Nick
 
H

hellrazor

(e-mail address removed) (Dan Rogers) wrote in
Hi Jorge,

It seems you are trying to use the service class as a stateful remote
object. This isn't going to work, since you cannot access members on
the service class in the way you are trying to.

A service method takes input parameters and returns values either thru
its parameter list or via it's return type. Think of the method like
you would a function in a function library and not having any access
to state on the server side (which for the most part is not kept).

This means you need to pass in parameters and get back data that you
are going to use, and not expect the web serivice proxy to have any
internal knowlege about the work done on the server side.

I hope this helps

Dan Rogers
Microsoft Corporation

Hi there,

I'm not really trying to use it as a "state" object. Basically, I just
want to return the values I set within the webservice call. I tried
returning a simple Object[] array and that seemed to work. It was a
variable type array, example:

[WebMethod]
public Object[] myTestWSClass()
{
int intValue1 = 1;
string strValue1 = "hello there";
double dblValue1 = 2.3;
Object[] retObjArray = {intValue1 , strValue1 , dblValue1};

return retObjArray;

}



What I wanted to do is perhaps have a class to hold my values:

public class ReturnValuesClass
{
int intValue1 = 1;
string strValue1 = "hello there";
double dblValue1 = 2.3;}

and modify my webmethod function to return an instance of the class

[WebMethod]
public ReturnValuesClass myTestWSClass()
{
ReturnValuesClass retClassObj = new ReturnValuesClass();
retClassObj.intValue1 = 1;
retClassObj.strValue1 = "hello there";
retClassObj.dblValue1 = 2.3;
return retClassObj;

}



I think this would qualify as "stateless" since I'm not setting the
values and expecting the retrieve them at a later time.. I just wanted a
more elegant way of returning my values than using an Object array (which
works for now)

Jorge
 
H

hellrazor

(e-mail address removed) (Dan Rogers) wrote in
Hi Jorge,

It seems you are trying to use the service class as a stateful remote
object. This isn't going to work, since you cannot access members on
the service class in the way you are trying to.

A service method takes input parameters and returns values either thru
its parameter list or via it's return type. Think of the method like
you would a function in a function library and not having any access
to state on the server side (which for the most part is not kept).

This means you need to pass in parameters and get back data that you
are going to use, and not expect the web serivice proxy to have any
internal knowlege about the work done on the server side.

I hope this helps

Dan Rogers
Microsoft Corporation

Hi,

That worked, thanks!

I need to get me a propper webservices tutorial / book. This "Webservices
for dummies" book is alright, but it just touches the surface. It's only
got a couple of pages worth of actual code.

Jorge
 
D

Dan Rogers

In general, you have to consider each method call to cause a new instance
of the server-side class. This instance is created to service the request.
The logic in your calling code that creates a new instance is really
creating an instance of a proxy for the server side object. It is that
proxy that forms the request and fields the response from the new instance
of the server side object.

So even though on the calling side you are using the same class to dispatch
two calls, on the server side, there is a new instance of the object being
made or each call. This is why the data passing you were expecting doesn't
work.

The array approach works because it happens in one round trip. You can
certanly create a complex class that is not an array, populate an instance
of that class in a single method call, and then have that class returned.
If you then want a different method to add data to the "return value
class", you will have to pass the data to the second method so that it can
effect the same instance of the answer.

I hope this helps

Dan Rogers
Microsoft Corporation
--------------------
Subject: Re: webservices: simple question, accessing webservice members
From: hellrazor <[email protected]>
References: <[email protected]>
<GKomd.513423$mD.491984@attbi_s02> <q9pmd.46404$5K2.39317@attbi_s03>
Organization: packet monkeys inc.
Message-ID: <[email protected]>
User-Agent: Xnews/5.04.25
Newsgroups: microsoft.public.dotnet.framework.aspnet.webservices
Date: Wed, 17 Nov 2004 14:43:23 -0800
NNTP-Posting-Host: s0106000625d84bd8.vn.shawcable.net 24.86.11.226
Lines: 1
Path: cpmsftngxa10.phx.gbl!TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP14
phx.gbl
Xref: cpmsftngxa10.phx.gbl microsoft.public.dotnet.framework.aspnet.webservices:26673
X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webservices

(e-mail address removed) (Dan Rogers) wrote in
Hi Jorge,

It seems you are trying to use the service class as a stateful remote
object. This isn't going to work, since you cannot access members on
the service class in the way you are trying to.

A service method takes input parameters and returns values either thru
its parameter list or via it's return type. Think of the method like
you would a function in a function library and not having any access
to state on the server side (which for the most part is not kept).

This means you need to pass in parameters and get back data that you
are going to use, and not expect the web serivice proxy to have any
internal knowlege about the work done on the server side.

I hope this helps

Dan Rogers
Microsoft Corporation

Hi there,

I'm not really trying to use it as a "state" object. Basically, I just
want to return the values I set within the webservice call. I tried
returning a simple Object[] array and that seemed to work. It was a
variable type array, example:

[WebMethod]
public Object[] myTestWSClass()
{
int intValue1 = 1;
string strValue1 = "hello there";
double dblValue1 = 2.3;
Object[] retObjArray = {intValue1 , strValue1 , dblValue1};

return retObjArray;

}



What I wanted to do is perhaps have a class to hold my values:

public class ReturnValuesClass
{
int intValue1 = 1;
string strValue1 = "hello there";
double dblValue1 = 2.3;}

and modify my webmethod function to return an instance of the class

[WebMethod]
public ReturnValuesClass myTestWSClass()
{
ReturnValuesClass retClassObj = new ReturnValuesClass();
retClassObj.intValue1 = 1;
retClassObj.strValue1 = "hello there";
retClassObj.dblValue1 = 2.3;
return retClassObj;

}



I think this would qualify as "stateless" since I'm not setting the
values and expecting the retrieve them at a later time.. I just wanted a
more elegant way of returning my values than using an Object array (which
works for now)

Jorge
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top