This works, but should it?

  • Thread starter Thread starter Greg Merideth
  • Start date Start date
G

Greg Merideth

I was looking for a way to pass, by reference, a class to a webmethod to allow
the web method to alter the values of some (now) 50 variables of the class.

Now, to get this to work, I have to put a copy of the class in the webmethod
itself. When I create the method I do the old:

public void mymethod(ref my_refclass)

however, when visual studio creates the proxy code it puts a reference to the
class of the webmethod (naturally) however visual studio then complains that
*my* local reference to the class cannot be done example:

client.cs
using Whatever.Webmethod;
class HelloClass { int a; } <---local
private void some func()
{
HelloClass _hello = new HelloClass();
webmethod web = new webmethod()
web.somefunc(ref _hello);
}

--

server.cs
class HelloClass { int a; } <---server
private HelloClass _hello = new HelloClass(); // or public, does the same thing

public void somefunc(ref _hello)
{
_hello.a = 10;
}

now, visual studio complains on the client that:

(client) HelloClass._hello cannot be matched against
Whatever.Webmethod.HelloClass._hello

Even thou they are the same class.. so what I did was go into the proxy code
generated by Visual studio and changed each method call in the proxy to point
to my local class. The upside is that now it works just fine, the webmethod
gets passed the entire empty class from the client and sends back the altered
values which get properly assigned to the clients class.

The problem is, this seems like a nasty hack method versus a "real" method of
doing it and everytime I load in or update the .asmx file I have to re-do the
changes and this hack wont work when I try it with the pocket PC build as the
proxy code created with pocket pc is different than a windows app build.

Anyone know if this is an accetable way of doing this or should I be looking
for other ways?
 
Greg,

This is actually how you have to do this. The reason is that when VS
generates the WDSL, and you subsequently generate the proxy, it doesn't know
about your type residing on the server. However, it can construct a new
type definition based on the WDSL, and that is what it does. Unfortunately,
this doesn't mean that they are the same type.

I believe that the web method doesn't use fully qualified type names, so
you should be able to place a using statement at the top of the proxy in
order to have it use your type instead of the imported type.

Other than that though, this is the way that you would have to handle
this situation.

Hope this helps.
 
Back
Top