Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>All types that are used with WCF must be either a data contract or marked as [Serializable]. This includes any objects that are in a WCF data contract already. If you have the ability, you must go and either add the [Serializable] tag to the classes, or add the WCF specific tags to them. This no kidding means that even though you add a Type to the KnownTypes, it doesn't mean that WCF will know that it is serializable.</p> <p>If neither of these options are available, I would suggest creating "proxy" objects that can contain the values you want to pass and have those convert to and from your target objects. Sounds crazy but that is the way it goes...</p> <p>ADDED EXAMPLE:</p> <p>WCF utilizes a serializer containined in the <a href="http://msdn.microsoft.com/en-us/library/kd1dc9w5.aspx" rel="nofollow">System.Runtime.Serialization</a> namespace to serialize and deserialize data into different formats (XML, SOAP, JSON, Binary). In order for this to work, the objects that are going to be serialized must be some type of serializable type (marked with an attribute). The built in WCF attributes for data objects looks like this:</p> <pre><code>//This is marked with the DataContract attribute, which is WCF specific [DataContract] public class Foo { //The DataMember attribute is also WCF specific and specifies what data // is included in the serialization. Any properties (or variables) must be // accessable, as in not read only. [DataMember] public string Property1{get;set;} //This variable will be serialized as well, even though it is private. This // works great if you have a readonly property but still need to pass the data [DataMember] private int Id = 0; //This does not have a DataMember attribute and will not be serialized private string var1; } </code></pre> <p>WCF can also utilize classes (such as the DataTable and DataSet) that are marked as <a href="http://msdn.microsoft.com/en-us/library/system.serializableattribute%28v=vs.110%29.aspx" rel="nofollow">Serializable</a>.</p> <pre><code>//This is marked with the Serializable attribute. All public and private // fields are automatically serialized (unless there is a containing object // that is not serializable then you get a SerializationException [Serializable] public class Bar { //gets serialized public string Property1{get;set;} //gets serialized private string var1; } </code></pre> <p>The idea of creating "proxy" objects is the same that is what is generated by Visual Studio when you add a service reference, only reversed. Say you have class "Foo2" that is not serializable, and has certain properties.</p> <pre><code>public class Foo2 { public string Property1{get;set;} public string Property2{get;set;} public int Property3{get;set;} } </code></pre> <p>You can make a proxy class (in nearly any way you want) that will allow you to pass the proerties back and forth to and from your service.</p> <pre><code>[DataContract] public class Foo2Proxy { [DataMember] public string Property1{get;set;} [DataMember] public string Property2{get;set;} [DataMember] public int Property3{get;set;} public Foo2Proxy() { } public Foo2Proxy(Foo2 foo) { this.Property1 = foo.Property1; this.Property2 = foo.Property2; this.Property3 = foo.Property3; } public static Foo2 Create(Foo2Proxy fProxy) { var foo = new Foo2(); foo.Property1 = fProxy.Property1; foo.Property2 = fProxy.Property2; foo.Property3 = fProxy.Property3; return foo; } } </code></pre> <p>I'm sure that there are probably 100,000 different ways and opinions on how to do this, but this is just an example of a possibility that worked for me at the time. If you take a look at <a href="http://www.codeproject.com/Articles/127395/Implementing-a-WCF-Service-with-Entity-Framework" rel="nofollow">this article on CodeProject</a> you can see what I was shooting for here.</p> <p>In your particular case, you may need to create "proxy" objects (or wrapper objects whatever you want to call it) for your Question and Decision types that are created by EF, since it would seem that they are not inherently serializable, unless you can get into the code that is generated for those objects and add the Attributes as described above. One additional consideration, is if they are derived from another class (or abstract class) that base class MUST ALSO be marked as either serializable or as a DataContract! </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload