views:

72

answers:

2

Hi All, I'm quite new to Web Services. I've created a Web Serivce with a class like this:

public class ClassA
{
     private ClassB _varA;

     public ClassB VarA
     {
         get {
             if(_varA == null)
                _varA = new ClassB();
             return _varA;
         }
         set { _varA = value; }
     }
}

When I try to access the property from a website, it gives me null.

using WebServiceA;

ClassA obj = new ClassA();
obj.VarA // gives me null ?

Am I missing something here? Please help. Thanks.

+4  A: 

When you send an object over a web-service, the actual functions don't come with it, only the property values (so the get in your example doesn't actually happen client side). Instead, it creates a 'mock' type version of the same object.

I figured I'd clarify in this edit:

When you connect to a web service that returns an object, it actually just returns an XML representation of the object. This XML representation only contains data that gets serialized (method depends on the settings, in plain-jane web services, it is usually just an XML Serializer), thus does not contain any functions or property definitions.

So, the class in this example is:

public class ClassA
{
     public ClassB VarA
     {
         get;
         set;
     }
}

Also: Fredrik Mörk said it correclty, its called a 'Proxy' object, not a mock object, I couldn't think of the word.

Erich
+1 - The generated class is often referred to as a *proxy* rather than a *mock*.
Fredrik Mörk
So, it means it would return null value irrespective of what's in the get part? If so, can you please suggest a solution for this?
Sridhar
To amplify "'mock' type version": the proxy class created for the client has the same name and properties as the class defined on the server, but the properties are implemented as "plain old data" i.e. just getting and setting a backing field, with no business logic.
itowlson
If this is WCF, you can add the ClassA and ClassB assembly's reference to the consuming project, right click on the Service Reference, click 'configure', then check the "Reuse Types in referenced assemblies".I'm not sure about plain-jane web services however.
Erich
A: 

Is class B being implemented inside the project of the webservice?

Jonathan
Yes. Actually I wanted to access the members of ClassB using this....obj.VarA.VarB(member of ClassB)
Sridhar