1

One of my WCF functions returns an object that has a member variable of a type from another library that is beyond my control. I cannot decorate that library's classes. In fact, I cannot even use DataContractSurrogate because the library's classes have private member variables that are essential to operation (i.e. if I return the object without those private member variables, the public properties throw exceptions).

If I say that interoperability for this particular method is not needed (at least until the owners of this library can revise to make their objects serializable), is it possible for me to use WCF to return this object such that it can at least be consumed by a .NET client?

How do I go about doing that?

Update: I am adding pseudo code below...

// My code, I have control

[DataContract]
public class MyObject
{
  private TheirObject theirObject;

  [DataMember]
  public int SomeNumber
  {
    get { return theirObject.SomeNumber; } // public property exposed
    private set { }
  }
}

// Their code, I have no control

public class TheirObject
{
  private TheirOtherObject theirOtherObject;

  public int SomeNumber 
  { 
    get { return theirOtherObject.SomeOtherProperty; }
    set { // ... }
  }
}

I've tried adding DataMember to my instance of their object, making it public, using a DataContractSurrogate, and even manually streaming the object. In all cases, I get some error that eventually leads back to their object not being explicitly serializable.

Mayo
  • 10,544
  • 6
  • 45
  • 90
  • Have you looked into Binary Serialization? – Sergey Akopov Dec 29 '10 at 23:04
  • @Sergei: I tried manually serializing to binary using BinaryFormatter but got an object not serializable exception. Do you think that WCF's binary serialization could perform better given this error? – Mayo Dec 30 '10 at 14:52

2 Answers2

2

Sure, write a wrapper class that has all of the same public properties available and simply put "get { return internalObject.ThisProperty; }. Decorate the wrapper class so that it works with WCF.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Chris Gessler
  • 22,727
  • 7
  • 57
  • 83
  • But the child properties he is using are still hidden and still aren't serializable, and I don't think there's a way around that... – veljkoz Dec 30 '10 at 15:11
  • @veljkoz: but since he can't serialize the real object, he can't send the real object. He has to send enough information so that a copy of the "real" object can be created on the other side, that's all. That probably doesn't require sending the private fields. – John Saunders Dec 30 '10 at 15:13
  • @John/vel/Chris: I think this approach would normally work (and it's kind of what I'm doing now), but the state of their object must be preserved because that object has a method that does some serious heavy lifting against the private members. I found a workaround where I could re-initialize the object with serializable data (i.e. I got lucky), but I'm thinking it isn't possible to serialize an object with private/unserializable dependencies. – Mayo Dec 30 '10 at 15:46
  • @Mayo: sometimes, you just have to say "no". If their object wasn't designed to be serializable, then it's possible that you just can't serialize it. Even without a network involved, can their object be persisted and later "rehydrated"? If not, then it just can't be done that way, and you'll have to look into "remote access". – John Saunders Dec 30 '10 at 19:20
  • It's not possible to serialize an object that that has complex members that are not serializable and not marked with the attribute NonSerializedAttribute. You shouldn't care about the private members either since you have no control over them anyway. Write a wrapper object that IS serializable for sending and receiving the public members and recreate the target object. Of course, with Reflection, I believe you can get/set private members. Check out this question: http://stackoverflow.com/questions/1565734/is-it-possible-to-set-private-property-via-reflection – Chris Gessler Jan 04 '11 at 00:30
  • @Chris: I believe this answer along with the comments is the best approach for handling this scenario. I'd be interested in trying the reflection approach if I hit this snag again. Thanks all! – Mayo Jan 04 '11 at 14:29
0

Another option is to write a Proxy class which mirrors the properties of the type you wish to use exactly, and return that via WCF.

You can use AutoMapper to populate the proxy object.

This approach has the advantage that your service's consumers don't need to take a dependency on the third party library in trying to use it.