webservice returning wrong object
please help!
I have a webservice with a webmethod tat returns a MessageCollection object that contains Message objects. My problem is that when i call the method from a client, i don't get a messagecollection object returned. the return value of the webmethod is a message object (which isn't correct)
here is the webmethod
Code:
<WebMethod()> _
Public Function GetMessage(ByVal userName As String, ByVal lastMessageID As Integer) As OI.Messages.MessageCollection
'could check if message needs to be returned by checking lastid passed here.
'get arraylistlist of groups to message. from most recent message
'get list of ous from username
'iterate thru ous from username
'if arraylist of groups to message contains a ou
'and the messageid > lastmessageid then return an arraylist containing
'messageid, message, sender
'first off check if there has been a new message
If Handshake() > lastMessageID Then
'there has been a more recent message, so go get it
Dim arrListOfUserOUs As ArrayList
Dim colMessage As I4M.I4M.its4meOI.Messages.MessageCollection
Dim enMsg As IEnumerator
Dim en As IEnumerator
Dim i As Integer
arrListOfUserOUs = GetOUFromUserName(userName)
'returns messagecollection of message
colMessage = GetOUsToMessage(lastMessageID)
'enumerate
en = arrListOfUserOUs.GetEnumerator
For i = 0 To colMessage.Count - 1
While en.MoveNext
If colMessage.Item(i).OUArrayList.Contains(en.Current()) Then
Else
colMessage.RemoveAt(i)
End If
End While
Next
'ok we have got a list of goodies lets return it to the client.
Return colMessage
Else
Return Nothing
End If
End Function
here is the code for the message and messagcollection objects
Code:
Imports System
Namespace OI.Messages
<serializable> _
Public Class Message
#Region "constructor"
Public Sub New()
End Sub
#End Region
#Region "membervariables"
Private iMessageID As Integer
Private dDateSent As Date
Private sMessageText As String
Private sSentBy As String
Private arrOUs As ArrayList
#End Region
#Region "properties"
Public Property MessageID() As Integer
Get
Return iMessageID
End Get
Set(ByVal Value As Integer)
iMessageID = Value
End Set
End Property
Public Property DateSent() As Date
Get
Return dDateSent
End Get
Set(ByVal Value As Date)
dDateSent = Value
End Set
End Property
Public Property MessageText() As String
Get
Return sMessageText
End Get
Set(ByVal Value As String)
sMessageText = Value
End Set
End Property
Public Property SentBy() As String
Get
Return sSentBy
End Get
Set(ByVal Value As String)
sSentBy = Value
End Set
End Property
Public ReadOnly Property OUArrayList() As ArrayList
Get
Return arrOUs
End Get
End Property
#End Region
Public Sub SetOUArray(ByVal strOU As String)
Dim arrOUsPass As New ArrayList(Split(strOU, ","))
arrOUs = arrouspass
End Sub
End Class
End Namespace
'''the message collection object
Imports System
Imports System.Collections
Namespace OI.Messages
<Serializable()> _
Public Class MessageCollection : Inherits CollectionBase
Public Overridable ReadOnly Property IsFixed() As Boolean
Get
Return False
End Get
End Property
''' <summary>
''' Indicates if the collection is read only.
''' </summary>
Public Overridable ReadOnly Property IsReadOnly() As Boolean
Get
Return False
End Get
End Property
Default Public Overridable Property Item(ByVal index As Integer) As OI.Messages.Message
Get
Return CType(Me.List(index), OI.Messages.Message)
End Get
Set(ByVal Value As OI.Messages.Message)
Me.List(index) = Value
End Set
End Property
''' <summary>
''' Returns the index (position) of an item in the collection.
''' </summary>
''' <param name="item">Item to be searched.</param>
''' <returns>Returns the index(position) of the item.</returns>
Public Overridable Function IndexOf(ByVal item As OI.Messages.Message) As Integer
Return Me.List.IndexOf(item)
End Function
''' <summary>
''' This method appends an item to the collection.
''' </summary>
''' <param name="item">Item to be appended.</param>
''' <returns>Returns an integer that indicates if the item was successfully appended.</returns>
Public Overridable Function Add(ByVal item As OI.Messages.Message) As Integer
Return Me.List.Add(item)
End Function
''' <summary>
''' Removes an item from the collection.
''' </summary>
''' <param name="item">Item to be removed from the collection.</param>
Public Overridable Sub Remove(ByVal item As OI.Messages.Message)
Me.List.Remove(item)
End Sub
''' <summary>
''' Copies an array to another.
''' </summary>
''' <param name="array">Array to be copied.</param>
''' <param name="index">Index to begin the copy.</param>
Public Overridable Sub CopyTo(ByVal array As Array, ByVal index As Integer)
Me.List.CopyTo(array, index)
End Sub
''' <summary>
''' This methoid adds one collection to this collection.
''' </summary>
''' <param name="collection">Collection to be added.</param>
Public Overridable Sub AddRange(ByVal collection As OI.Messages.MessageCollection)
Me.InnerList.AddRange(collection)
End Sub
''' <summary>
''' This methoid adds one collection to this collection.
''' </summary>
''' <param name="collection">Collection to be added.</param>
Public Overridable Sub AddRange(ByVal collection As OI.Messages.Message)
Me.InnerList.AddRange(collection)
End Sub
''' <summary>
''' This method tells if the collection contains the item.
''' </summary>
''' <param name="item">Item to be tested.</param>
''' <returns>Returns true if the collection contains the item.</returns>
Public Overridable Function Contains(ByVal item As OI.Messages.Message) As Boolean
Return Me.List.Contains(item)
End Function
''' <summary>
''' Inserts an item in the collection.
''' </summary>
''' <param name="index">Position to be inserted.</param>
''' <param name="item">Item to be inserted.</param>
Public Overridable Sub Insert(ByVal index As Integer, ByVal item As OI.Messages.Message)
Me.List.Insert(index, item)
End Sub
End Class
End Namespace
in the client code if i have a line like this which calls the webmethod,
it says it will return a message object
Code:
wsOI.GetMessage(UserName, LastMessageID)
Re: webservice returning wrogn object
This is the proxy class that is generated from the wsdl. notice at the bottom it has a definition for a Message class, but defines the arraylistOU as an object rather than an arraylist
and also notice the return values of the get message method. for some reason the proxy calss says it returns a Message object, when we can see in the actual webmethod in my last post that it returns a MessageCollection
Code:
'------------------------------------------------------------------------------
' <autogenerated>
' This code was generated by a tool.
' Runtime Version: 1.1.4322.2032
'
' Changes to this file may cause incorrect behavior and will be lost if
' the code is regenerated.
' </autogenerated>
'------------------------------------------------------------------------------
Option Strict Off
Option Explicit On
Imports System
Imports System.ComponentModel
Imports System.Diagnostics
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.Xml.Serialization
'
'This source code was auto-generated by Microsoft.VSDesigner, Version 1.1.4322.2032.
'
Namespace i4m
'<remarks/>
<System.Diagnostics.DebuggerStepThroughAttribute(), _
System.ComponentModel.DesignerCategoryAttribute("code"), _
System.Web.Services.WebServiceBindingAttribute(Name:="its4meOISoap", [Namespace]:="http://tempuri.org/its4meOI/its4meOI")> _
Public Class OI
Inherits System.Web.Services.Protocols.SoapHttpClientProtocol
'<remarks/>
Public Sub New()
MyBase.New
Me.Url = "http://localhost/OI/OI.asmx"
End Sub
'<remarks/>
<System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://tempuri.org/its4meOI/its4meOI/Test", RequestNamespace:="http://tempuri.org/OI/OI", ResponseNamespace:="http://tempuri.org/OI/OI", Use:=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle:=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)> _
Public Function Test() As Object
Dim results() As Object = Me.Invoke("Test", New Object(-1) {})
Return CType(results(0),Object)
End Function
'<remarks/>
Public Function BeginTest(ByVal callback As System.AsyncCallback, ByVal asyncState As Object) As System.IAsyncResult
Return Me.BeginInvoke("Test", New Object(-1) {}, callback, asyncState)
End Function
'<remarks/>
Public Function EndTest(ByVal asyncResult As System.IAsyncResult) As Object
Dim results() As Object = Me.EndInvoke(asyncResult)
Return CType(results(0),Object)
End Function
'<remarks/>
<System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://tempuri.org/OI/OI/GetMessage", RequestNamespace:="http://tempuri.org/OI/OI", ResponseNamespace:="http://tempuri.org/OI/OI", Use:=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle:=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)> _
Public Function GetMessage(ByVal userName As String, ByVal lastMessageID As Integer) As Message()
Dim results() As Object = Me.Invoke("GetMessage", New Object() {userName, lastMessageID})
Return CType(results(0),Message())
End Function
'<remarks/>
Public Function BeginGetMessage(ByVal userName As String, ByVal lastMessageID As Integer, ByVal callback As System.AsyncCallback, ByVal asyncState As Object) As System.IAsyncResult
Return Me.BeginInvoke("GetMessage", New Object() {userName, lastMessageID}, callback, asyncState)
End Function
'<remarks/>
Public Function EndGetMessage(ByVal asyncResult As System.IAsyncResult) As Message()
Dim results() As Object = Me.EndInvoke(asyncResult)
Return CType(results(0),Message())
End Function
'<remarks/>
<System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://tempuri.org/OI/OI/SendMessage", RequestNamespace:="http://tempuri.org/OI/OI", ResponseNamespace:="http://tempuri.org/OI/OI", Use:=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle:=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)> _
Public Function SendMessage(ByVal message As String, ByVal userName As String, ByVal csListOfGroups As String) As Integer
Dim results() As Object = Me.Invoke("SendMessage", New Object() {message, userName, csListOfGroups})
Return CType(results(0),Integer)
End Function
'<remarks/>
Public Function BeginSendMessage(ByVal message As String, ByVal userName As String, ByVal csListOfGroups As String, ByVal callback As System.AsyncCallback, ByVal asyncState As Object) As System.IAsyncResult
Return Me.BeginInvoke("SendMessage", New Object() {message, userName, csListOfGroups}, callback, asyncState)
End Function
'<remarks/>
Public Function EndSendMessage(ByVal asyncResult As System.IAsyncResult) As Integer
Dim results() As Object = Me.EndInvoke(asyncResult)
Return CType(results(0),Integer)
End Function
'<remarks/>
<System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://tempuri.org/OI/OI/GetAllGroups", RequestNamespace:="http://tempuri.org/OI/OI", ResponseNamespace:="http://tempuri.org/OI/OI", Use:=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle:=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)> _
Public Function GetAllGroups() As String
Dim results() As Object = Me.Invoke("GetAllGroups", New Object(-1) {})
Return CType(results(0),String)
End Function
'<remarks/>
Public Function BeginGetAllGroups(ByVal callback As System.AsyncCallback, ByVal asyncState As Object) As System.IAsyncResult
Return Me.BeginInvoke("GetAllGroups", New Object(-1) {}, callback, asyncState)
End Function
'<remarks/>
Public Function EndGetAllGroups(ByVal asyncResult As System.IAsyncResult) As String
Dim results() As Object = Me.EndInvoke(asyncResult)
Return CType(results(0),String)
End Function
'<remarks/>
<System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://tempuri.org/OI/OI/UserHandshake", RequestNamespace:="http://tempuri.org/OI/OI", ResponseNamespace:="http://tempuri.org/OI/OI", Use:=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle:=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)> _
Public Function UserHandshake() As Integer
Dim results() As Object = Me.Invoke("UserHandshake", New Object(-1) {})
Return CType(results(0),Integer)
End Function
'<remarks/>
Public Function BeginUserHandshake(ByVal callback As System.AsyncCallback, ByVal asyncState As Object) As System.IAsyncResult
Return Me.BeginInvoke("UserHandshake", New Object(-1) {}, callback, asyncState)
End Function
'<remarks/>
Public Function EndUserHandshake(ByVal asyncResult As System.IAsyncResult) As Integer
Dim results() As Object = Me.EndInvoke(asyncResult)
Return CType(results(0),Integer)
End Function
'<remarks/>
<System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://tempuri.org/OI/OI/AdminHandshake", RequestNamespace:="http://tempuri.org/OI/OI", ResponseNamespace:="http://tempuri.org/OI/OI", Use:=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle:=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)> _
Public Function AdminHandshake() As Integer
Dim results() As Object = Me.Invoke("AdminHandshake", New Object(-1) {})
Return CType(results(0),Integer)
End Function
'<remarks/>
Public Function BeginAdminHandshake(ByVal callback As System.AsyncCallback, ByVal asyncState As Object) As System.IAsyncResult
Return Me.BeginInvoke("AdminHandshake", New Object(-1) {}, callback, asyncState)
End Function
'<remarks/>
Public Function EndAdminHandshake(ByVal asyncResult As System.IAsyncResult) As Integer
Dim results() As Object = Me.EndInvoke(asyncResult)
Return CType(results(0),Integer)
End Function
End Class
'<remarks/>
<System.Xml.Serialization.XmlTypeAttribute([Namespace]:="http://tempuri.org/OI/OI")> _
Public Class Message
'<remarks/>
Public MessageID As Integer
'<remarks/>
Public DateSent As Date
'<remarks/>
Public MessageText As String
'<remarks/>
Public SentBy As String
'<remarks/>
Public OUArrayList() As Object
End Class
End Namespace
Re: webservice returning wrong object
In the end i thought f**k it and created a dataset into which i stuck my message objects, knowing that a dataset will return in the correct format.
I have seen loads of stuff whilst googling of people haviong the exact same problem as i had. making sure a complex type returns from the web service as it should. i haven't found any answer other than someone saying they edited the auto generated proxy class which is a pretty naf way of doing this i think.
if anyone has surmounted this problem please let me know, it al works swimmingly now, but i'm a bit pissed off that i have to use an ms object instead of my own in order to make sure that i return the correct object.