Web Service doesn't return values remotely

caeanis

Active member
Joined
Sep 6, 2006
Messages
26
I was tasked to find a way to allow our team to dynamically pull down a set of lists within a Time Correction program we wrote. I figured that putting an xml file on a web server and use a webservice to not only call the specific lists but also allow us to update it as well. The web service does what it is supposed to do, we can retrieve a list of users, update the list with new members or delete them as we see fit. The problem is, it only appears to return data when I browse to it in the browser. When I try to do the same via the vb.net form, it doesnt error, it just doesnt return the expected string. I found that it does if I deploy the web service locally on my own box in IIS, but that of course defeats the purpose of accessing it anywhere. I noticed that when I test it using the test page that the returned string actually comes back in a separate window; I wonder if this is why our form doesnt get the data because its not being directed back to where the request originated. Any ideas about this?

Thank you!

Caeanis
 
The results being displayed in a new window is just how the test page does things - it isnt affecting how the results are returned when the web service is running normally.

Where is the xml file the web service is handling hosted (same server? what folder? what permissions on folder?). What information are you passing to the service that could be used to identify the file in question?

Without seeing any code or without further information though everything is just speculation...
 
The xml file is on the same server, same web folder as the asmx file. Here is my web service code:
Code:
Imports System.Web
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.Xml
Imports Microsoft.VisualBasic

<WebService(Namespace:="ws")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Public Class ListManager
    Inherits System.Web.Services.WebService
    Dim XmlPath As String = "list.xml"
    Dim listDoc As New XmlDocument
    Dim node As XmlNode
    Dim strList As String = ""
    Dim nms As XmlNodeList

    <WebMethod()> Public Sub SetList(ByVal cbo As String, ByVal NameList As String)
        Dim NameNode = Nothing
        Dim node As XmlNode
        Dim nms As XmlNodeList = Nothing
        Dim listDoc As XmlDocument = New XmlDocument()
        Dim ws As XmlWhitespace = listDoc.CreateWhitespace(ControlChars.CrLf)
        Dim tb As XmlWhitespace = listDoc.CreateWhitespace(ControlChars.Tab)
        On Error GoTo ErrResponse
        listDoc.Load(Server.MapPath(XmlPath))
        Select Case cbo
            Case "Name"
                nms = listDoc.SelectNodes("/List/Team/Name")
                NameNode = listDoc.SelectSingleNode("/List/Team")
            Case "Reason"
                nms = listDoc.SelectNodes("/List/Reasons/Reason")
                NameNode = listDoc.SelectSingleNode("/List/Reasons")
            Case "Type"
                nms = listDoc.SelectNodes("/List/Types/Type")
                NameNode = listDoc.SelectSingleNode("/List/Types")
        End Select
        Dim i As Integer
        Assign the string of comma delimited values to an array
        Dim listToTxt() As String = Split(NameList, ",")
        For i = 0 To UBound(listToTxt)
            If Item is NOT found in the collection of nodes then add it
            If IsDupe(nms, listToTxt(i)) = False Then
                node = listDoc.CreateNode(XmlNodeType.Element, "", cbo, "")
                node.InnerText = listToTxt(i)
                NameNode.AppendChild(node)
                Indent the new node
                NameNode.InsertBefore(tb, node)
                Insert a carriage return after the new node
                NameNode.InsertAfter(ws, node)
            End If
        Next
        listDoc.Save(Server.MapPath(XmlPath))
ErrResponse:
        If Err.Number > 0 Then
            Resume Next
        End If
    End Sub


<WebMethod()> Public Function GetList(ByVal cbo As String) As String
        On Error GoTo ErrResponse
        listDoc.Load(Server.MapPath(XmlPath))
        Select Case cbo
            Case "Name"
                nms = listDoc.SelectNodes("/List/Team/Name")
            Case "Reason"
                nms = listDoc.SelectNodes("/List/Reasons/Reason")
            Case "Type"
                nms = listDoc.SelectNodes("/List/Types/Type")
        End Select
         Loop through the nodes and add the names to the list
        For Each node In nms
            If Len(strList) = 0 Then
                strList += node.InnerText
            Else
                strList += "," & node.InnerText
            End If
        Next
        Return strList
ErrResponse:
        If Err.Number > 0 Then
            Resume Next
        End If
    End Function


<WebMethod()> Public Sub RemoveEntry(ByVal cbo As String, ByVal nme As String)
        On Error GoTo ErrResponse
        Dim rmNode, srchNode As System.Xml.XmlNode
        listDoc.Load(Server.MapPath(XmlPath))
        Select the node to search
        srchNode = listDoc.SelectSingleNode("/List/" & cbo)
         Loop through the children nodes and find the one to remove
        For Each rmNode In srchNode.ChildNodes
            If rmNode.InnerText = nme Then
                Remove the specified node
                srchNode.RemoveChild(rmNode)
                Save the file
                listDoc.Save(Server.MapPath(XmlPath))
                Exit For
            End If
        Next
ErrResponse:
        If Err.Number > 0 Then
            Resume Next
        End If
    End Sub
    
Private Function IsDupe(ByVal ns As XmlNodeList, ByVal strEntry As String) As Boolean
        Dim node As XmlNode
        For Each node In ns
            If node.InnerText = strEntry Then
                Return True
                Exit Function
            End If
        Next
        Return False
    End Function
End Class
 
Last edited by a moderator:
Is there any chance you could attach a sample of the xml file? Just to give me something to look at in a debugger...

At first glance however I would be cautious about using class level variables and redefining them at the method level - this can introduce all sorts of odd errors.

Equally you need to be careful if you are storing any state between method calls as under IIS web services are going to be be stateless unless you deliberately choose to use session variables or similar.
 
Last edited by a moderator:
FYI - By default session variables are stored in the IIS worker process which resets itself in order to avoid memory leaks. You can use the ASP.NET State Service to maintain session variables between IIS worker process resets.

To Setup the ASP.NET State Service, Go to Start >> Run >> Services.msc find the “ASP.NET State Service” go to Properties and start. (for servers, change startup type to "automatic") Also drop this in your Web.config:

Code:
<sessionState
      mode="StateServer"
      stateConnectionString="tcpip=127.0.0.1:42424"
      cookieless="false"
      timeout="20" />
 
Back
Top