Click here to Skip to main content
15,028,657 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey!

I working on a hotel search engine with gathers the result from a API of Expedia

I am displaying the first 15 results and then I am starting a async function to save the rest of the results in a database.
Async Function RequestMoreHotels(ByVal city As String, ByVal country As String, ByVal countryCode As String, ByVal RegionID As String, ByVal checkInDate As String, ByVal CheckOutDate As String, ByVal NrOfRooms As String, ByVal Room1 As String, ByVal Room2 As String, ByVal Room3 As String) As Task(Of JsonResult)
ReqCountry = Server.HtmlEncode(country)
ReqCity = Server.HtmlEncode(city)
ReqcountryCode = Server.HtmlEncode(countryCode)
ReqRegID = Server.HtmlEncode(RegionID)
ReqArrival = Server.HtmlEncode(checkInDate)
ReqDeparture = Server.HtmlEncode(CheckOutDate)
ReqRoomsRequested = Server.HtmlEncode(NrOfRooms)
ReqRoom1 = Server.HtmlEncode(Room1)
ReqRoom2 = Server.HtmlEncode(Room2)
ReqRoom3 = Server.HtmlEncode(Room3)
RebelCurrency = Session("Currency")
If Session("Culture") = "de" Then
LangSetting = "de_DE"
Else
LangSetting = "en_US"
End If
UserSession = Session("UserSessionID")
Await GetAsyncHotelsAsync()
Return Json("done", JsonRequestBehavior.AllowGet)
End Function

Async Function GetAsyncHotelsAsync() As Task
get more results and save them in a database
End Function

So, this is working fine.

But when in the meantime (while the above function is running) the user requesting more results
(ajax request:
Public Function GetNextResults() As JsonResult
Dim GetHotels = (From gh In _db2.temp_hotels Where gh.Session = UserSession And gh.shown = False Order By gh.ID Descending Select gh).Take(20).ToList

Dim HotelHtml As New StringBuilder
For Each item In GetHotels
HotelHtml.Append("make the HTML")

Next
Return Json(New With {.element = HotelHtml.ToString, .moreresults = MoreResults}, JsonRequestBehavior.AllowGet)
End Function

and this request is pending a long time and when it is finally executed, the UserSession is nothing.

It would be awesome if someone of you can help me with that

Thanks in advance
Cheers
Posted

1 solution

I think I am doing something wrong with the async await funcionality:
Anyway, I changed the code to

Public Function RequestMoreHotels(ByVal city As String, ByVal country As String, ByVal countryCode As String, ByVal RegionID As String, ByVal checkInDate As String, ByVal CheckOutDate As String, ByVal NrOfRooms As String, ByVal Room1 As String, ByVal Room2 As String, ByVal Room3 As String) As JsonResult
System.Threading.ThreadPool.QueueUserWorkItem(AddressOf GetHotelsAsync)
Return Json("done", JsonRequestBehavior.AllowGet)

Function GetHotelsAsync(state As Object)
Do something
End Function

So in jQuery i am making an ajax request to RequestMoreHotels and it returns immediatley "done" BUT before he is doing that the function GetHotelsAsync gets started

Cheers
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900