如何让wcf datacontract类拥有另一个datacontract类的对象的字段

时间:2014-01-06 17:17:55

标签: wcf

我有一个WCF DataContract类,如下所示:

[DataContract]
public class Breads
{
    public Breads() { }

    public Breads(int breadid, string breadname, int specieid)
    {
        breadId = breadid;
        breadName = breadname;
        specieId = specieid;
    }

    private int breadId;

    [DataMember]
    public int BreadId
    {
        get { return breadId; }
        set { breadId = value; }
    }

    private string breadName;

    [DataMember]
    public string BreadName
    {
        get { return breadName; }
        set { breadName = value; }
    }

    private int specieId;

    [DataMember]
    public int SpecieId
    {
        get { return specieId; }
        set { specieId = value; }
    }

    private Specie.Species specie;

    [DataMember]
    public Specie.Species Specie
    {
        get
        {
            if (specie == null)
            {
                BLL_HIS.Classes.Animals.Specie s = new BLL_HIS.Classes.Animals.Specie();
                s = s.SelectById(specieId);
                specie = new Specie.Species(s.SpecieId, s.SpecieName);
            }
            return specie;
        }
    }


}

正如您所看到的,有一个名为Specie的属性,它是另一个类的实例,描述如下:

    [DataContract]
public class Species
{
    public Species() { }

    public Species(int specieid, string speciename)
    {
        specieId = specieid;
        specieName = speciename;
    }

    private int specieId;

    [DataMember]
    public int SpecieId
    {
        get { return specieId; }
        set { specieId = value; }
    }


    private string specieName;

    [DataMember]
    public string SpecieName
    {
        get { return specieName; }
        set { specieName = value; }
    }

}

当我遗漏属性Specie时,代码工作得很好。虽然它使用属性进行编译和运行,但是当我调用它时,会发生错误。错误文本如下:

无法调用该服务。可能的原因:服务离线或无法访问;客户端配置与代理不匹配;现有代理无效。有关更多详细信息,请参阅堆栈跟踪。您可以尝试通过启动新代理,还原到默认配置或刷新服务来进行恢复。

错误详情如下:

*The underlying connection was closed: The connection was closed unexpectedly.
Server stack trace: 
   at System.ServiceModel.Channels.HttpChannelUtilities.ProcessGetResponseWebException(WebException webException, HttpWebRequest request, HttpAbortReason abortReason)
   at System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
   at System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout)
   at System.ServiceModel.Dispatcher.RequestChannelBinder.Request(Message message, TimeSpan timeout)
   at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
   at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
   at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)
Exception rethrown at [0]: 
   at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
   at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
   at IBread.SelectById(Int64 id)
   at BreadClient.SelectById(Int64 id)
Inner Exception:
The underlying connection was closed: The connection was closed unexpectedly.
   at System.Net.HttpWebRequest.GetResponse()
   at System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)*

注释:

1-我尝试手动填写Specie以确保问题不在于该方法,并且“底层连接”与sql连接无关。

2-我试图为我的Breads类使用“KnownType(typeof(Species))”属性。

顺便说一句,对不起该帖子的长度。

2 个答案:

答案 0 :(得分:0)

[DataMember]
public Specie.Species Specie { get; }

目前,您只定义了get属性。它还必须定义要反序列化的set操作。

“连接意外关闭”可能是服务端解串器的未处理异常。如果打开WCF日志记录,您将能够看到错误消息。

答案 1 :(得分:0)

我有两个问题,第一个问题由ErnieL解决(所有数据成员必须同时设置和获取操作)

另一个解决我的问题的是,显然所有的服务都必须在同一个命名空间中,我有一些文件夹,每个服务与它的相关内容都在那个文件夹中,我删除了那些文件夹并将所有服务放在一个文件夹然后它工作!!