By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,852 Members | 994 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,852 IT Pros & Developers. It's quick & easy.

Pass by reference to a WebService

P: n/a
Hi folks,

I would be interested to hear peoples views on whether or not 'pass by
reference' is allowed when using a Web Service method.

The thing that troubles me about pass-by-reference into a WebService is that
essentially we are passing an address of an object which resides on the
'local machine' i.e. a local machine object address. Surely when the
WebService method is called and run 'on the server', the reference type will
be place some value at that address, however, the address is now on the
Server machine...how can it set the local machine objcet address if the
location is different?

Is this correct? Or does .NET WebServices wrap up this functionality to
allow it in some special way? Or am I missing something?

Thanks for any views, insights,

Best Regards,
David
Aug 17 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"David++" wrote:
Hi folks,

I would be interested to hear peoples views on whether or not 'pass by
reference' is allowed when using a Web Service method.

The thing that troubles me about pass-by-reference into a WebService is that
essentially we are passing an address of an object which resides on the
'local machine' i.e. a local machine object address. Surely when the
WebService method is called and run 'on the server', the reference type will
be place some value at that address, however, the address is now on the
Server machine...how can it set the local machine objcet address if the
location is different?

Is this correct? Or does .NET WebServices wrap up this functionality to
allow it in some special way? Or am I missing something?

Thanks for any views, insights,

Best Regards,
David
I performed a little test with a web service running locally. It seemed to
work fine when passing by reference using the 'ref' keyword i.e.

// In the web service code..
[WebMethod]
public void PassByRef(ref string s)
{
s = "This is pass-by-reference";
}

// In the client code..
string s = "";

Service service = new Service();

service.PassByRef(ref s);
MessageBox.Show(s); // outputs 'This is pass-by-reference'

Using the 'out' keyword behaved a little more unusual than was expected -
// In the web service code..
[WebMethod]
public void PassByOut(out string s)
{
s = "This is pass-by-out";
}

// In the client code..
s = service.PassByOut();
MessageBox.Show(s); // outputs 'This is pass-by-out'

Note that the WebMethod PassByOut is a void function yet it is treated as
function which returns a string and takes no parameter...which is
strange...this must be how .NET handles this situation? Compared to the
traditional use of 'out' when using a local function -

// In the client code...
private void Method(out string s)
{
s = "This is a local method";
}

// In the client code...
Method(out s);
MessageBox.Show(s); // outputs - 'This is a local method'

Now the method call takes an out parameter and doesnt return anything as
would be expected, which is different from the WebMethod version. Strange and
interesting.
Aug 17 '06 #2

P: n/a
Because the data passed by a web service is serialized it's inherently
a copy of your object that travels across the wire - which is anologous
with ByVal.

Sadly, there's no way around this using web services but web services
deal with messages and aren't a 'remote objects' implementation.

If you change the data in your client, you're changing your local copy.
The server will never know about it unless you create a way to 'push'
the changes back across.
David++ wrote:
"David++" wrote:
Hi folks,

I would be interested to hear peoples views on whether or not 'pass by
reference' is allowed when using a Web Service method.

The thing that troubles me about pass-by-reference into a WebService is that
essentially we are passing an address of an object which resides on the
'local machine' i.e. a local machine object address. Surely when the
WebService method is called and run 'on the server', the reference type will
be place some value at that address, however, the address is now on the
Server machine...how can it set the local machine objcet address if the
location is different?

Is this correct? Or does .NET WebServices wrap up this functionality to
allow it in some special way? Or am I missing something?

Thanks for any views, insights,

Best Regards,
David

I performed a little test with a web service running locally. It seemed to
work fine when passing by reference using the 'ref' keyword i.e.

// In the web service code..
[WebMethod]
public void PassByRef(ref string s)
{
s = "This is pass-by-reference";
}

// In the client code..
string s = "";

Service service = new Service();

service.PassByRef(ref s);
MessageBox.Show(s); // outputs 'This is pass-by-reference'

Using the 'out' keyword behaved a little more unusual than was expected -
// In the web service code..
[WebMethod]
public void PassByOut(out string s)
{
s = "This is pass-by-out";
}

// In the client code..
s = service.PassByOut();
MessageBox.Show(s); // outputs 'This is pass-by-out'

Note that the WebMethod PassByOut is a void function yet it is treated as
function which returns a string and takes no parameter...which is
strange...this must be how .NET handles this situation? Compared to the
traditional use of 'out' when using a local function -

// In the client code...
private void Method(out string s)
{
s = "This is a local method";
}

// In the client code...
Method(out s);
MessageBox.Show(s); // outputs - 'This is a local method'

Now the method call takes an out parameter and doesnt return anything as
would be expected, which is different from the WebMethod version. Strange and
interesting.
Aug 17 '06 #3

P: n/a
Hi David,

I agree with Josh. The following method declaration,

// In the web service code..
[WebMethod]
public void PassByRef(ref string s)
{
s = "This is pass-by-reference";
}

It is the same as this declaration,

// In the web service code..
[WebMethod]
public string PassByRef(string s)
{
return "This is pass-by-reference";
}

The .NET framework creates a proxy using that trick so you do not notice the
difference.

Regards,
Pablo Cibraro.

"Josh Twist" <jo********@gmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
Because the data passed by a web service is serialized it's inherently
a copy of your object that travels across the wire - which is anologous
with ByVal.

Sadly, there's no way around this using web services but web services
deal with messages and aren't a 'remote objects' implementation.

If you change the data in your client, you're changing your local copy.
The server will never know about it unless you create a way to 'push'
the changes back across.
David++ wrote:
>"David++" wrote:
Hi folks,

I would be interested to hear peoples views on whether or not 'pass by
reference' is allowed when using a Web Service method.

The thing that troubles me about pass-by-reference into a WebService is
that
essentially we are passing an address of an object which resides on the
'local machine' i.e. a local machine object address. Surely when the
WebService method is called and run 'on the server', the reference type
will
be place some value at that address, however, the address is now on the
Server machine...how can it set the local machine objcet address if the
location is different?

Is this correct? Or does .NET WebServices wrap up this functionality to
allow it in some special way? Or am I missing something?

Thanks for any views, insights,

Best Regards,
David

I performed a little test with a web service running locally. It seemed
to
work fine when passing by reference using the 'ref' keyword i.e.

// In the web service code..
[WebMethod]
public void PassByRef(ref string s)
{
s = "This is pass-by-reference";
}

// In the client code..
string s = "";

Service service = new Service();

service.PassByRef(ref s);
MessageBox.Show(s); // outputs 'This is pass-by-reference'

Using the 'out' keyword behaved a little more unusual than was expected -
// In the web service code..
[WebMethod]
public void PassByOut(out string s)
{
s = "This is pass-by-out";
}

// In the client code..
s = service.PassByOut();
MessageBox.Show(s); // outputs 'This is pass-by-out'

Note that the WebMethod PassByOut is a void function yet it is treated as
function which returns a string and takes no parameter...which is
strange...this must be how .NET handles this situation? Compared to the
traditional use of 'out' when using a local function -

// In the client code...
private void Method(out string s)
{
s = "This is a local method";
}

// In the client code...
Method(out s);
MessageBox.Show(s); // outputs - 'This is a local method'

Now the method call takes an out parameter and doesnt return anything as
would be expected, which is different from the WebMethod version. Strange
and
interesting.

Aug 17 '06 #4

P: n/a
Thanks guys for your answers. Its always good to discover the hidden secrets
of .NET !

Cheers!

David
Aug 18 '06 #5

P: n/a
Sorry, but I don't follow your point.

Of course, you are never going to give one machine the physical address
of variable on another machine (especially in .NET).

When you pass a variable by reference to a web service, the client
marshals the data and sends a copy to the web service. The web service
demarshals it, manipulates it, marshals the new value again and sends
it back. On return from the Web Service call, the client then
demarshals it again and stuffs it back in the original variable. This
is all done transparently so that it behaves virtually like a call by
reference on the local machine.

In this way, passing a parameter ByRef to a web service IS NOT the same
as calling ByVal. True that ultimately you are passing just a copy of
the parameter (not the address), but the end result is that the value
of the variable is changed by the called method like a traditional call
by reference since it is automatically updated on return from the web
service method.

Of course, if you change the value again on the local machine, the web
service won't know about it unless you make another web service call.

Sorry if I misunderstood your post,

-Paul

Josh Twist wrote:
Because the data passed by a web service is serialized it's inherently
a copy of your object that travels across the wire - which is anologous
with ByVal.

Sadly, there's no way around this using web services but web services
deal with messages and aren't a 'remote objects' implementation.

If you change the data in your client, you're changing your local copy.
The server will never know about it unless you create a way to 'push'
the changes back across.
David++ wrote:
"David++" wrote:
Hi folks,
>
I would be interested to hear peoples views on whether or not 'pass by
reference' is allowed when using a Web Service method.
>
The thing that troubles me about pass-by-reference into a WebService is that
essentially we are passing an address of an object which resides on the
'local machine' i.e. a local machine object address. Surely when the
WebService method is called and run 'on the server', the reference type will
be place some value at that address, however, the address is now on the
Server machine...how can it set the local machine objcet address if the
location is different?
>
Is this correct? Or does .NET WebServices wrap up this functionality to
allow it in some special way? Or am I missing something?
>
Thanks for any views, insights,
>
Best Regards,
David
>
I performed a little test with a web service running locally. It seemed to
work fine when passing by reference using the 'ref' keyword i.e.

// In the web service code..
[WebMethod]
public void PassByRef(ref string s)
{
s = "This is pass-by-reference";
}

// In the client code..
string s = "";

Service service = new Service();

service.PassByRef(ref s);
MessageBox.Show(s); // outputs 'This is pass-by-reference'

Using the 'out' keyword behaved a little more unusual than was expected -
// In the web service code..
[WebMethod]
public void PassByOut(out string s)
{
s = "This is pass-by-out";
}

// In the client code..
s = service.PassByOut();
MessageBox.Show(s); // outputs 'This is pass-by-out'

Note that the WebMethod PassByOut is a void function yet it is treated as
function which returns a string and takes no parameter...which is
strange...this must be how .NET handles this situation? Compared to the
traditional use of 'out' when using a local function -

// In the client code...
private void Method(out string s)
{
s = "This is a local method";
}

// In the client code...
Method(out s);
MessageBox.Show(s); // outputs - 'This is a local method'

Now the method call takes an out parameter and doesnt return anything as
would be expected, which is different from the WebMethod version. Strange and
interesting.
Aug 24 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.