0

I have been trying to get a webservice working in my localhost and debug, but it gives me this error:

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.

Has anyone out there had this error before?

Thanks.

Abridged Code below (Note: Not my code. I'm modifying a previous developer's code, who is not here anymore. Also a noobie w/ web services. In this code block below, since this problem seems to be with funds right now, I've made all the 'Funds' references more visible than others. Thanks.)

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}
user1333792
  • 11
  • 1
  • 4

1 Answers1

0

Although not being the solution to your problem, I had the same error message. So, I think it is useful for others to post it here. In my case it was related to chain inheritance.

I had a static method on a base class called DerivedTypes (much like described here: Generally accepted way to avoid KnownType attribute for every derived class) that calls a extension method to determine its heirs.

If you have A : B, B : C, then C should have something like this:

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

Calling B.DerivedTypes() will (wrongly) get too many types, as it will call typeof(C).GetDerivedTypes(). This triggered the same error in my case.

Unless B is:

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

Without the '**' naturally. Now, if type B is encountered, B.DerivedTypes() will not make the call to its parent. Since A does not have child classes, it does not need the KnownType attribute.

Btw, the modifications I made to the extension method are that the extension method uses the assembly from the type (so Assembly.GetAssembly(baseType)) and I made the static method internal for testing purposes. Private should be fine too.

Community
  • 1
  • 1