2014-01-06 85 views
0

我有一個WCF DataContract類,如下的領域:如何有WCF datacontract類有另一個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; 
     } 
    } 


} 

,你可以看到有一個屬性命名正金這是一個實例另一類描述如下:

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

} 

當我離開屬性物種代碼工作就好了。雖然它編譯並運行屬性,但當我調用它時會發生錯誤。錯誤文本如下:

無法調用該服務。可能的原因:服務處於脫機狀態或無法訪問;客戶端配置與代理不匹配;現有的代理無效。有關更多詳細信息,請參閱堆棧跟蹤。您可以嘗試通過啓動新代理,還原爲默認配置或刷新服務來進行恢復。

錯誤信息是如下:

*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 - 我tryed手動填寫的正金,以確保問題不是與方法,以及「底層連接」與sql連接無關。

2-我嘗試爲我的Breads類使用「KnownType(typeof(Species))」屬性。

順便說一句,對不起,帖子的長度。

回答

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

當前你只定義一個get屬性。它還必須定義set操作進行反序列化。

「連接意外關閉」可能是服務端反序列化器未處理的異常。如果您打開WCF日誌記錄,您將能夠看到錯誤消息。

+0

謝謝你的回答ErnieL,那是我的問題之一。 – mesmoll

0

我有兩個問題,第一個是由ErnieL解決(所有數據成員必須同時set和get操作)

,另一個該解決我的問題,顯然所有的服務都必須在相同的命名空間,我有一些文件夾和每個服務與它的相關的東西都在該文件夾中,我刪除了這些文件夾,並將所有服務放在一個文件夾中,然後它的工作原理!