Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to serialize objects without a parameterless constructor in WCF?

I know that a private parameterless constructor works but what about an object with no parameterless constructors?

I would like to expose types from a third party library so I have no control over the type definitions.

If there is a way what is the easiest? E.g. I don't what to have to create a sub type.

Edit:

What I'm looking for is something like the level of customization shown here: http://msdn.microsoft.com/en-us/magazine/cc163902.aspx although I don't want to have to resort to streams to serialize/deserialize.

like image 591
Jonathan Parker Avatar asked Mar 03 '09 03:03

Jonathan Parker


3 Answers

You can't really make arbitrary types serializable; in some cases (XmlSerializer, for example) the runtime exposes options to spoof the attributes. But DataContractSerializer doesn't allow this. Feasible options:

  • hide the classes behind your own types that are serializable (lots of work)
  • provide binary formatter surrogates (yeuch)
  • write your own serialization core (a lot of work to get right)

Essentially, if something isn't designed for serialization, very little of the framework will let you serialize it.

like image 120
Marc Gravell Avatar answered Nov 15 '22 10:11

Marc Gravell


I just ran a little test, using a WCF Service that returns an basic object that does not have a default constructor.

//[DataContract]
//[Serializable]
public class MyObject
{
    public MyObject(string _name)
    {
        Name = _name;
    }

    //[DataMember]
    public string Name { get; set; }

    //[DataMember]
    public string Address { get; set; }
}

Here is what the service looks like:

public class MyService : IMyService
{
    #region IMyService Members

    public MyObject GetByName(string _name)
    {
        return new MyObject(_name) { Address = "Test Address" };
    }

    #endregion
}

This actually works, as long as MyObject is either a [DataContract] or [Serializable]. Interestingly, it doesn't seem to need the default constructor on the client side. There is a related post here:

How does WCF deserialization instantiate objects without calling a constructor?

like image 43
Andy White Avatar answered Nov 15 '22 11:11

Andy White


I am not a WCF expert but it is unlikely that they support serialization on a constructor with arbitrary types. Namely because what would they pass in for values? You could pass null for reference types and empty values for structs. But what good would a type be that could be constructed with completely empty data?

I think you are stuck with 1 of 2 options

  1. Sub class the type in question and pass appropriate default values to the non-parameterless constructor
  2. Create a type that exists soley for serialization. Once completed it can create an instance of the original type that you are interested in. It is a bridge of sorts.

Personally I would go for #2. Make the class a data only structure and optimize it for serialization and factory purposes.

like image 40
JaredPar Avatar answered Nov 15 '22 11:11

JaredPar