Nullqwerty
Nullqwerty

Reputation: 1168

Using A WCF Service To Pass An Instantiated Class

I've got a C# class library with approximately 50 classes with hundreds of properties. It can take a while to instantiate my classes, so as an optimization I was considering having them instantiated on a server and then have the server pass the instantiated classes to a client application via a WCF Service.

Seems like it should work fine. However, from what I understand, if I want to do this I would need to not only mark every class as [DataContract] but also every property as [DataMember]. Is this really true? Seems crazy that this would need to be done, especially on every property. Is there a way to just say the entire class is serializable without having to go through this process?

Upvotes: 2

Views: 1856

Answers (4)

Rob Levine
Rob Levine

Reputation: 41298

If you use the [DataContract] you'll need to specify the [DataMember] for each member too.

Alternatively - mark the class with [Serializable] and the serializer will serialize all the internals for you unless you explicitly say otherwise ([Serializable] works on an opt-out approach whereas [DataContract] works on an opt-in approach).

However - depending on what, exactly, you are trying to do - this approach may be flawed.

When you pass an object over WCF, you are not really passing the object at all - you are passing a copy of the object. In fact, the original object will be serialized, passed down the wire and deserialized at the other end. This means the client still has to instantiate reconstitute an instance of the class and then populate all the properties. You may gain nothing, and potentially have a rather expensive call with a lot of data being passed across the wire.

So it all depends what you are doing. If you don't mind the clients getting a copy of the instance and if the expense is not due to the sheer amount of data but other things like slow database access, then fine. Otherwise your approach may not buy you anything.

As an aside, in .Net remoting it is possible to have a reference to a remote object (i.e. you client can reference the object on the server) - if the type itself derives from MarshalByRefObject, but that is not the case here with WCF - you end up with your own copy locally.

Upvotes: 3

Yuck
Yuck

Reputation: 50825

If I want to do this, I would need to not only mark every class as [DataContract] but also every property as [DataMember]. Is this really true?

Yes and no.

If the client code can't import the assembly that contains those classes then, yes, you need to mark them up. This is so that WCF can tell clients how to interpret the result sets.

If you can use a common assembly both in the WCF service and the clients (e.g. like a POCO library) then you don't need to apply those attributes. WCF will just tell clients to refer to the assembly by it's strong name.

Here's what the WSDL might look like in case #2:

<wsdl:types>
  <xsd:schema targetNamespace = "http://tempuri.org/Imports">
    <xsd:import schemaLocation = "http://localhost:12902/MyService.svc?xsd=xsd0"
       namespace="http://tempuri.org/" />
    <xsd:import schemaLocation = "http://localhost:12902/MyService.svc?xsd=xsd1"
       namespace="http://schemas.microsoft.com/2003/10/Serialization/" />
    <xsd:import schemaLocation = "http://localhost:12902/MyService.svc?xsd=xsd2"
      namespace="http://schemas.datacontract.org/2004/07/MyAssembly.MyProject" />
  </xsd:schema>
</wsdl:types>

Where MyAssembly.MyProject above refers to a namespace in your assembly. If you follow the link at http://localhost:12902/MyService.svc?xsd=xsd2 (on your local machine of course) you'd get an XSD describing the entities serialized within that namespace. You don't have to decorate the class. WCF does all of this for you.

Upvotes: 2

Dani
Dani

Reputation: 15069

In a real separate client server scenario you have to. (if you cant share a file.)

Upvotes: 0

iefpw
iefpw

Reputation: 7042

Basically yes. It needs to have the two properties on the classes, structs, fields so that the NET knows to serialize those classes. Without those properties, those won't get serialized and you won't see that field or class in the client.

Upvotes: 0

Related Questions