DataContract not available at WCF client service reference

坚强是说给别人听的谎言 提交于 2019-12-08 08:32:39

问题


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.


回答1:


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;
        }
    }
}


来源:https://stackoverflow.com/questions/18286785/datacontract-not-available-at-wcf-client-service-reference

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!