简体   繁体   中英

Creating a Web Service Request

I would like to feed data to a proxy which creates a soap XML request formatted as below:

<dat:MusicCollection>
           <!--Zero or more repetitions:-->
           <dat:Song>
              <dat:songUserkey>TakemeHome</dat:songUserkey>
           </dat:Song>
</dat:MusicCollection>

I have written the file to call the service and provide the details as below:

dim ucizi1 as SongRequest 'this is the request class in the proxy
dim Songs as Song = New Song
Songs.songUserKey = "TakeMeHome"
dim ucz
ucz = Songs.SongUserKey
ucizi1.SongCollection.Add(ucz)

The MusicCollection class is as follows:

<System.Diagnostics.DebuggerStepThroughAttribute(),  _
 System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0"),  _
 System.Runtime.Serialization.CollectionDataContractAttribute(Name:="ProductCollection", [Namespace]:="http://ucizi.Musicservice/DataContracts", ItemName:="Song")>  _
Public Class SongCollection
    Inherits System.Collections.Generic.List(Of ucizi.Music.DataContracts.Song)
End Class

The song class is as follows:

<System.Diagnostics.DebuggerStepThroughAttribute(),  _
 System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0"),  _
 System.Runtime.Serialization.DataContractAttribute(Name:="Product", [Namespace]:="http://Ucizi.Music/DataContracts")>  _
Partial Public Class Product
    Inherits Object
    Implements System.Runtime.Serialization.IExtensibleDataObject

    Private extensionDataField As System.Runtime.Serialization.ExtensionDataObject

    Private SongUserkeyField As String

    Public Property ExtensionData() As System.Runtime.Serialization.ExtensionDataObject Implements System.Runtime.Serialization.IExtensibleDataObject.ExtensionData
        Get
            Return Me.extensionDataField
        End Get
        Set
            Me.extensionDataField = value
        End Set
    End Property

    <System.Runtime.Serialization.DataMemberAttribute(IsRequired:=true)>  _
    Public Property SongUserkey() As String
        Get
            Return Me.SongUserkeyField
        End Get
        Set
            Me.SongUserkeyField = value
        End Set
    End Property
End Class

However, when I run this code, I get an error: unable to cast object of type 'system.string' to type 'ucizi.music.DataContracts.Song'.

I cant see where this error is coming from, can some1 please help me and advise how I can correct this.

In lines

Songs.songUserKey = "TakeMeHome"
dim ucz
ucz = Songs.SongUserKey
ucizi.SongCollection.Add(ucz)

you set ucz to be SongUserKey - which is string.

Then, you add it to collection SongCollection

Public Class SongCollection
    Inherits System.Collections.Generic.List(Of ucizi.Music.DataContracts.Song)
End Class

Which is expected Song

added the following code after the Songs.songUserKey = "TakeMeHome"

dim ucizi2 as songCollection
ucizi2.Add(song)
ucizi1.songcollection = ucizi2

This sorted the problem smoothly. You guys opened my eyes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM