The constructor does not appear in my WCF client, is there a serialization problem?

I have a simple class in my WCF service that does not seem to display correctly for the client that accesses my WCF.

My class has 4 public properties of type string.

I marked the class with [DataContract ()] and each member with [DataMember].

Why is my constructor not showing up? Is there a special attribute for constructors?

+4
source share
4 answers

Data contracts have nothing to do with constructors. Therefore, when you create your proxy server on the client, you will only get a class that implements a contract with data.

If you want to add a similar constructor on the client side (suppose the generated type is called SomeDataItem), you can add it using a partial class:

public partial class SomeDataItem { public SomeDataItem(int a, int b) { A = a; B = b; } } 
+8
source

Agree that you can add a partial class to the WCF client application - you can also add the [OnDeserializing] and [OnDeserialized] methods to the server-side class to handle initialization, etc.

Found the following great example when I was looking for how to solve this problem - at http://social.msdn.microsoft.com/forums/en-US/wcf/thread/447149d5-b44c-47cd-a690-20928244b52b/

 [DataContract] public class MyClassWithSpecialInitialization { List<string> myList; string str1; [DataMember] public string Str1 { get { return str1; } set { this.myList.Add(value); str1 = value; } } string str2; [DataMember] public string Str2 { get { return str2; } set { this.myList.Add(value); str2 = value; } } public MyClassWithSpecialInitialization() { this.myList = new List<string>(); } [OnDeserializing] public void OnDeserializing(StreamingContext context) { Console.WriteLine("Before deserializing the fields"); this.myList = new List<string>(); } [OnDeserialized] public void OnDeserialized(StreamingContext context) { Console.WriteLine("After deserializing the fields... myList should be populated with the values of str1 and str2"); } } 
+6
source

How do you generate your client code: VS Add Service Reference, SVCUTIL? What parameters / settings are you using? Have you looked at the client code to see what is actually being created?

Is your class used correctly in the service? I had a problem when I created an open class of data contracts, but did not use it in the service method, so the class was not exported to client code.

+1
source

End signs are not required if you mark the class as a data contract. Can you provide some sample code for your class?

0
source

All Articles