如何将我的代码转换为Web服务 [英] how to convert my code to a web service

查看:79
本文介绍了如何将我的代码转换为Web服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:



我想将其转换为网络服务,我不知道该怎么做。



I have the following code:

I want to convert it to a web service and im not sure how to do it.

Sub Search(ByVal intSearchType As Search.enmSearchType)
        Try

            Dim objSearch As New Search             'search object
            Dim objSearchInst As New Search         'search object
            Dim intResult As Integer                'function call results
            Dim objList As New ArrayList            'array list of search objects

            'hide search panel and check at least 3 characters entered in search field
            panSearch.Visible = True
            If Len(txtSearch.Text) < 3 Then
                txtResults.Text = "Enter at least 3 characters to do a search"
                Exit Sub
            End If

            'do keyword search
            intResult = objSearch.SearchByKeyword(txtSearch.Text, Session("Person"), Session("Office"), Session("Organisation"), _
                                                  Session("Role"), intSearchType, objList)

            'if error is returned show user and clear results
            If intResult <> 0 Then
                txtResults.Text = "Oops. There was a problem performing the search, please try again later"
                Exit Sub
            End If
            txtResults.Text = ""

            'show results for contacts
                For Each objSearchInst In objList
                    txtResults.Text += "<a href='#' class=lnks  önclick=""return OpenWindow('../contactmanagement/contact.aspx?PersonRef=" & objSearchInst.Return1 & "',750,600)"">#" & objSearchInst.Return1 & ": " & objSearchInst.Return2 & " " & objSearchInst.Return3 & "</a><br/>"
                Next

            'no results found show user
            If txtResults.Text = "" Then
                txtResults.Text = "no matching records found"
            End If

        Catch ex As Exception
            txtResults.Text = "Oops. There was a problem performing the search, please try again later"
        End Try
    End Sub





我尝试过以下方法:



I have tried the following:

    <WebMethod()> _
<ScriptMethod(ResponseFormat:=ResponseFormat.Json, UseHttpGet:=True)> _
    Public Function ContactGet(ByVal searchField As String) As String

        Dim objSearch As New ArrayList
        Dim objSearching As New Search
        Dim intResult As Integer

        Try

            intResult = objSearching.SearchByKeyword(searchField, str_person_ref, str_office_ref, str_organization_ref, _
                                             str_role_ref, compX.ETMyProperty.Search.enmSearchType.enmContact, objSearch)

                Dim objContact As New Person
                Dim dt As New DataTable("Contacts")

                Dim col_Name As New DataColumn("Name", GetType(String))
                dt.Columns.Add(col_Name)

                Dim col_Mobile As New DataColumn("Mobile", GetType(String))
                dt.Columns.Add(col_Mobile)

                Dim col_Office As New DataColumn("ContactNum", GetType(String))
                dt.Columns.Add(col_Office)

                Dim col_Category As New DataColumn("Category", GetType(String))
                dt.Columns.Add(col_Category)

                Dim dr As DataRow

                'add new row to datatable
            'For Each objSearching In objSearch

            'For Each drow As DataRow In objSearch
            '    dr = dt.NewRow()
            '    dr("Name") = objContact.FullName
            '    dr("Mobile:") = objContact.MobileNumber
            '    dr("ContactNum") = objContact.OfficeNumber
            '    dr("Category") = objContact.PersonRelationshipType
            '    dt.Rows.Add(dr)
            'Next

            For i = 0 To objSearch.Count - 1
                dr = dt.NewRow()
                dr("Name") = DirectCast(objSearch(i), compX.ETMyProperty.Search).SearchByKeyword(searchField, Session("Person"), Session("Office"), Session("Organisation"), _
                                                  Session("Role"), compX.ETMyProperty.Search.enmSearchType.enmContact, objSearch)
                dr("Mobile") = DirectCast(objSearch(i), compX.ETMyProperty.Search).SearchByKeyword(searchField, Session("Person"), Session("Office"), Session("Organisation"), _
                                                  Session("Role"), compX.ETMyProperty.Search.enmSearchType.enmContact, objSearch)
                dr("ContactNum") = DirectCast(objSearch(i), compX.ETMyProperty.Search).SearchByKeyword(searchField, Session("Person"), Session("Office"), Session("Organisation"), _
                                                  Session("Role"), compX.ETMyProperty.Search.enmSearchType.enmContact, objSearch)
                dr("Category") = DirectCast(objSearch(i), compX.ETMyProperty.Search).SearchByKeyword(searchField, Session("Person"), Session("Office"), Session("Organisation"), _
                                                  Session("Role"), compX.ETMyProperty.Search.enmSearchType.enmContact, objSearch)
                dt.Rows.Add(dr)
            Next

            Dim serializer As New JavaScriptSerializer()
            Dim rows As New List(Of Dictionary(Of String, Object))()
            Dim row As Dictionary(Of String, Object) = Nothing

            'serialize dt row to json output
            For Each drow As DataRow In dt.Rows
                row = New Dictionary(Of String, Object)()
                For Each col As DataColumn In dt.Columns
                    row.Add(col.ColumnName, dr(col))
                Next
                rows.Add(row)
            Next

            Dim str_json = JsonConvert.SerializeObject(dt, Formatting.Indented)

            Return str_json
        Catch ex As Exception
            Return Nothing
        End Try
    End Function





当我运行网络服务并调用它时,我得到:





When I run the webservice and I invoke it i get:

<string xmlns="http://compX.net/">[]</string>

我不确定这意味着什么。



基本上我想知道我的转换是否正确,我的结果意味着什么?有什么建议吗?



c#help也欢迎

and I am not sure what that means.

So basically I want to know is my conversion correct and what does my result mean? Any suggestions?

c# help also welcome

推荐答案

这篇关于如何将我的代码转换为Web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆