views:

1817

answers:

4

Hi,

I have a simple class in my WCF service that doesn't seem to be showing up properly for the client that accesses my WCF.

My class has 4 public properties that are of type string.

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

Why is my constructor not visible? Is there a special [attribute] for constructors?

A: 

The parenthesises are not required when you mark a class as a data contract. Can you provide a code sample of you class?

Michael Kniskern
+6  A: 

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

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

public partial class SomeDataItem
{
    public SomeDataItem(int a, int b)
    {
        A = a;
        B = b;
    }
}
Brian Genisio
I like your suggestion....+1
Michael Kniskern
+1  A: 

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

Is your class being utilized in the service properly? I had an issue where I created an exposed data contract class but didn't utilize it in a service method so the class was not exported into the client code.

Chris Porter
+2  A: 

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

Found the following great example when I was searching on how to address this very thing -- 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");
    }
}
Lance Larsen