2012-07-17 17 views
0

我一直在試圖讓一個Web服務在我的本地和調試工作,但它給了我這個錯誤:datacontracts並不等同

System.InvalidOperationException: DataContract for type 'ParticipantDataService.Participant+OutsideAccountDetails+FundCollection' cannot be added to DataContractSet since type 'ParticipantDataService.Participant+FundCollection' with the same data contract name 'ArrayOfFundDetails' in namespace 'http://schemas.datacontract.org/2004/07/ParticipantDataService' is already present and the contracts are not equivalent.

有沒有人在那裏有這個錯誤之前?

謝謝。

Abridged Code below(注意:不是我的代碼,我正在修改以前的開發人員的代碼,誰不在這裏了,還有一個noobie w/web服務在下面的代碼塊中,因爲這個問題似乎與資金現在,我做了所有的「基金」的引用比其他人更明顯。謝謝。)

Namespace COMParticipantNameSpace 
<DataContract(Namespace:="XYZ", Name:="COMParticipant"),       
KnownType(GetType(COMParticipant))> _ 
Public Class COMParticipant 
    Inherits COMResponse 

    Private _FullName As FullName 
    (Other initialized variables...) 

    Protected Friend Sub New... 

    #Region "Properties" 
    <DataMember(Name:="Name", Order:=0)>... 
    <Other DataMembers...> 
    #End Region 

    #Region "Structures" 
    Public Structure FullName... 
      (Other Public Structures) 
    #End Region 
    #Region "Classes" 
    <DataContract(Name:="ParticipantPortfolio")>.... 

    Public Class GroupCollection... 
    End Class 

    <DataContract(Name:="Group")>... 
      <DataMember(Name:="Funds", Order:=6)> _ 
      Public Property Funds() As COMFundCollection 
       Get 
        Return _Funds 
       End Get 
       Set(ByVal value As COMFundCollection) 
        _Funds = value 
       End Set 
      End Property 
    End Class 

    Public Class COMAccountCollection 
     Inherits System.Collections.ObjectModel.Collection(Of COMAccountDetails) 
    End Class 

    <DataContract(Name:="COMAccountDetails")> _ 
    Public Class COMAccountDetails 

     (Other initialized variables...) 
     Private _Funds As COMFundCollection 

     <DataMember Order 0,1,2,3,etc...)>... 

     <DataMember(Name:="Funds", Order:=7)> _ 
     Public Property Funds() As COMFundCollection 
      Get 
       Return _Funds 
      End Get 
      Set(ByVal value As COMFundCollection) 
       _Funds = value 
      End Set 
     End Property 

    End Class 

    Public Class COMFundCollection 
     Inherits System.Collections.ObjectModel.Collection(Of COMFundDetails) 
    End Class 

    Public Class OutsideAccountCollection 
    End Class 

    <DataContract(Name:="OutsideAccountDetails")> _ 
     Public Class OutsideAccountDetails 

     (Other initialized variables...) 
     Private _Funds As FundCollection 

     <Order 1, 2, 3, etc...> 

     <DataMember(Name:="Funds", Order:=15)> _ 
     Public Property Funds() As FundCollection 
      Get 
       Return _Funds 
      End Get 
      Set(ByVal value As FundCollection) 
       _Funds = value 
      End Set 
     End Property 

     Public Class FundCollection 
      Inherits System.Collections.ObjectModel.Collection(Of FundDetails) 
     End Class 

     <DataContract(Name:="FundDetails")>... 

    End Class 

    Public Class TransactionTypeCollection 
     Inherits System.Collections.ObjectModel.Collection(Of TransactionTypeDetail) 
    End Class 
    Public Class TransactionTypeDetail... 
    Public Class TransactionCollection... 

    <DataContract(Name:="Transaction")> _ 
    Public Class Transaction 

     (Other initialized variables...) 
     Private _Funds As TransactionFundCollection 

     <DataMember Order 1,2,3,etc ...> 

     <DataMember(Name:="Funds", Order:=7)> _ 
     Public Property Funds() As TransactionFundCollection 
      Get 
       Return _Funds 
      End Get 
      Set(ByVal value As TransactionFundCollection) 
       _Funds = value 
      End Set 
     End Property 

    End Class 
    Public Class TransactionFundCollection 
     Inherits System.Collections.ObjectModel.Collection(Of TransactionFundDetails) 
    End Class 

    #End Region 
    End Class 

    End Namespace} 
+1

你有兩個不同的類名爲「基金」? – 2012-07-17 15:41:38

+0

你可以發佈你的代碼嗎? – 2012-07-17 19:58:35

+0

請參考上面的修改。謝謝。 – user1333792 2012-07-19 17:46:39

回答

0

雖然不是解決你的問題,我有同樣的錯誤消息。所以,我認爲這對其他人來說很有用。在我看來,它與鏈式繼承有關。

我有一個基類稱爲DerivedTypes(很像這裏描述:Generally accepted way to avoid KnownType attribute for every derived class),調用擴展方法來確定其繼承人的靜態方法。

如果A:B,B:C,那麼C應該是這樣的:

[DataContract] 
[KnownType("DerivedTypes")] 
public class C { 
    internal static Type[] DerivedTypes() { 
    typeof(C).GetDerivedTypes(); 
    } 
} 

調用B.DerivedTypes()會(錯誤地)獲得太多的類型,因爲它會調用typeof(C).GetDerivedTypes() 。這在我的案例中引發了同樣的錯誤。

除非B是:

[DataContract] 
[KnownType("DerivedTypes")] 
public class B : C { 
    internal **new** static Type[] DerivedTypes() { 
    typeof(B).GetDerivedTypes(); 
    } 
} 

沒有 '**' 自然。現在,如果遇到類型B,B.DerivedTypes()將不會對其父進行調用。由於A沒有子類,因此不需要KnownType屬性。

順便說一句,我對擴展方法所做的修改是擴展方法使用了類型的程序集(所以Assembly.GetAssembly(baseType)),並且我爲了測試目的而將內部靜態方法做了。私人也應該沒問題。