我有以下代码要转换为Web服务。
我在运行网络服务时获得<string xmlns="http://company.net/">[]</string>
,仅[]
这是什么意思?
旧代码:
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 onclick=""return OpenWindow('../contactmanagement/contact.aspx?PersonRef=" & objSearchInst.Return1 & "',750,600)""><b>#" & objSearchInst.Return1 & "</b>: " & objSearchInst.Return2 & " " & objSearchInst.Return3 & "</a><br/>"
Next
End If
'no results found show user
If txtResults.Text = "" Then
txtResults.Text = "no matching records found"
End If
Catch ex As Exception txtResults.Text =“糟糕。执行搜索时出现问题,请稍后再试” 结束尝试 结束子
我尝试过但不起作用的是:
<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, company.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), company.ETMyProperty.Search).SearchByKeyword(searchField, Session("Person"), Session("Office"), Session("Organisation"), _
Session("Role"), company.ETMyProperty.Search.enmSearchType.enmContact, objSearch)
dr("Mobile") = DirectCast(objSearch(i), company.ETMyProperty.Search).SearchByKeyword(searchField, Session("Person"), Session("Office"), Session("Organisation"), _
Session("Role"), company.ETMyProperty.Search.enmSearchType.enmContact, objSearch)
dr("ContactNum") = DirectCast(objSearch(i), company.ETMyProperty.Search).SearchByKeyword(searchField, Session("Person"), Session("Office"), Session("Organisation"), _
Session("Role"), company.ETMyProperty.Search.enmSearchType.enmContact, objSearch)
dr("Category") = DirectCast(objSearch(i), company.ETMyProperty.Search).SearchByKeyword(searchField, Session("Person"), Session("Office"), Session("Organisation"), _
Session("Role"), company.ETMyProperty.Search.enmSearchType.enmContact, objSearch)
dt.Rows.Add(dr)
Next
'Dim objList As New ArrayList
'For Each objSearching In objList
' Dim strText As String = ""
' strText += "<a href='#' class=lnks onclick=""window" & objSearching.Return1 & "',750,600)""><b>#" & objSearching.Return1 & "</b>: " & objSearching.Return2 & " " & objSearching.Return3 & "</a><br/>"
'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
这就是我所尝试过的,而且评论的代码也是我尝试过但不起作用的,或者它返回上述内容。我对Web服务很新,并希望将其转换为Web服务,但我正在努力。我不介意C#帮助!
答案 0 :(得分:0)
由于Web服务的代码应该与非Web服务方法的代码相同,并且您的问题是它没有返回预期的值,并且您不熟悉Web服务,我建议增量方法。
一旦您习惯于编写和使用Web服务,您就可以直接使用Web方法编写代码。
我应该补充一点,作为替代步骤5,复制/粘贴,您可以让您的webservice调用您的非webservive方法:
Private Function ContactGetInfo(ByVal searchField As String) As String
Return String.Format("user3458266less: {0}", searchField)
End Function
<WebMethod()> _
<ScriptMethod(ResponseFormat:=ResponseFormat.Json, UseHttpGet:=True)> _
Public Function ContactGet(ByVal searchField As String) As String
Return ContactGetInfo(searchField)
End Function