TRS
TRS

Reputation: 2097

DataContract not available at WCF client service reference

I have this Data contract in my WCF service

[DataContract]
public class Department
{
    [DataMember]
    public List<Section> Sections { get; set; }
}


[DataContract]
public class Section
{
    [DataMember]
    public List<Room> Rooms { get; set; }
}

[DataContract]
public class Room
{
    [DataMember]
    public uint RoomId { get; set; }
}

When I reference my service in client application,I only see Room class,Can any body explain me why contract for Department and Section class are not available at client side.

Upvotes: 2

Views: 9724

Answers (1)

Rohit Vats
Rohit Vats

Reputation: 81233

In your ServiceContract interface add one single operation related to Department which will make Department and Section visible to your client application.

Since Department contains list of Sections, it will expose Section as well.

[ServiceContract]
public interface IService1
{
    [OperationContract]
    Room GetRoom();

    [OperationContract]
    List<Department> GetDepartments();
}

Explanation

You can verify it by using Svcutil.exe.

If no operation contract exist for user defined classes, its definition won't emit in proxy class generated using Svcutil.

If I omit second operation contract of Department, only Room class gets emitted in proxy class. So, you need to have atleast one operation contract on your class to make it visible to your client.

PROXY class for Room:

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", 
                                                  "4.0.0.0")]
[System.Runtime.Serialization.DataContractAttribute(Name="Room", 
             Namespace="http://schemas.datacontract.org/2004/07/DummyService")]
public partial class Room : object,
                System.Runtime.Serialization.IExtensibleDataObject
{        
    private System.Runtime.Serialization.ExtensionDataObject extensionDataField;        
    private uint RoomIdField;        
    public System.Runtime.Serialization.ExtensionDataObject ExtensionData
    {
        get
        {
            return this.extensionDataField;
        }
        set
        {
            this.extensionDataField = value;
        }
    }

    [System.Runtime.Serialization.DataMemberAttribute()]
    public uint RoomId
    {
        get
        {
            return this.RoomIdField;
        }
        set
        {
            this.RoomIdField = value;
        }
    }
}

Upvotes: 17

Related Questions