How to change the serializer of WebMethod parameters - asp.net

Ive discovered an issue in some of our old web code. The problem is that the default serializer isnt serializing the date properly. I want to use JSON.Net in order to serialize the parameters for the web methods in our aspx code. But im not sure how to tell it to use JSON.NET instead of using the default serializer.
Here is an example snipit of our code
<System.Web.Services.WebMethod()> _
<System.Web.Script.Services.ScriptMethod()> _
Public Shared Function EditApplication(ByVal Application As ApplicationModel, ByVal Country As String, ByVal Language As String) As jsonResponse
Dim r As New jsonResponse
Dim g As New ApplicationRequest
g.country = Country
g.locale = Language
g.platform = "Android"
g.timestamp = ""
g.transactionid = "abc123"
....
So I need ApplicationModel to serialize using JSON.Net. Thanks for your help.

Not sure if this will be helpful (since this is taken from the REST web service) but you can define it on OperationContract like this one below:
System.ServiceModel.Web.WebMessageFormat.Json
<OperationContract()> _
<WebGet(responseformat:=System.ServiceModel.Web.WebMessageFormat.Json)> _

Related

Convert JSon to dynamic object in VB.Net

I am using VB.Net and calling salesforce API. It returns very ugly JSON which I am not able to deserialize. I have a following code using JSON.Net
Dim objDescription As Object = JsonConvert.DeserializeObject(Of Object)(result)
objDescription contains many properties, one on=f them in fields. But when I write something like objDescription.fields it gives me error.
objDescription.fields Public member 'fields' on type 'JObject' not found. Object
I am not very sure but I think it C# allow to convert any JSON to dynamic object. How can I use it in VB.Net?
You can turn Option Strict Off and use ExpandoObject which is recognized by JSON.NET. In order to leverage the dynamic features you can use a variable of type object.
Option Strict Off
Sub Main
Dim jsonData As Object = JsonConvert.DeserializeObject(Of System.Dynamic.ExpandoObject)("{""Id"":25}")
Dim test As Integer = jsonData.Id
Console.WriteLine(test)
End Sub
If you would like to use JObject because you need some of its features, you can index the JObject instead.
Sub Main
Dim jsonData As Object = JsonConvert.DeserializeObject(Of Object)("{""Id"":25}")
Dim test = jsonData("Id")
Console.WriteLine(test)
End Sub

Are variables inside VB.NET shared function local only?

ASP.NET VB.NET 3.5.
In the following function, called from a webpage AJAX, will the cVSession2 object be shared with other users (I don't want it to be)?
<WebMethod()> _
Public Shared Function fGPSUpdateSessionValues(ByVal strLatitude As String, ByVal strLongitude As String) As String
Dim strReturn As String = ""
Dim cVSession2 As New cVSession
With cVSession2
.pVSessionGPSLatitudeLastPosition = strLatitude
.pVSessionGPSLongitudeLastPosition = strLongitude
.fUpdate()
End With
cVSession2 = Nothing
Return strReturn
End Function
It appears local variables such as cVSession2 will not be shared with other users as per:
Does a static method share its local variables & what happens during concurrent usage from different threads?

How do I write a VB.Net method to filter a URLs?

I am attempting to write a method using VB.NET that will allow me to read in a URL and compare it to a list. If it is one of the URLs on the list then Bing Tracking conversion will be applied.
At the moment I can only think do write it as a comaparative method, comapring the current URL with the ones that require tracking (a list). This, however, sems a little long winded.
Each page may have a different querystring value/page id, there for its fundamental to get exactly the right page for the tracking to be applied to.
Any Ideas?
Sorry I really am a novice when developing functions in VB.Net
If I were to use th Contains() function then I would imagine that it would look a little something like this:
Private sub URL_filter (ByVal thisPage As ContentPage, brandMessage? As Boolean) As String
Dim url_1 As String = "/future-contact thanks.aspx"
Dim url_2 As String = "/find-enquiry thanks.aspx?did=38"
Dim url_3 As String = "/find-enquiry-thanks.aspx?did=90"
Dim url_4 As String = "/find-enquiry-thanks.aspx?did=62"
Dim result as String
result = CStr (url_1.Contains(current_URL))
txtResult.Text = result
End Sub
If I were to use this then what type of loop would I have to run to check all the URLs that are in my list against the current_URL? Also where would I define the current_URL?
You can use the Contains() function to check if the list contains the given value. You could also implement a binary search, but it is probably overkill for your purposes. Here is an example:
Dim UrlList As New List(Of String)
UrlList.Add("www.example2.net") 'Just showing adding urls to the list
UrlList.Add("www.example3.co.uk")
UrlList.Add("www.exampletest.com")
Dim UrlToCheck As String = "www.exampletest.com" 'This is just an example url to check
Dim result As Boolean = UrlList.Contains(UrlToCheck) 'The result of whether it was found
Make sure to add these imports Imports System and Imports System.Collections.Generic
Disclaimer: I have no experience with VB.NET

Calling of .NET Function Taking inputs as objects from Classic ASP

This is strange for me. I was able to set up the environment so that I can call .NET method (via COM) from a classic ASP page.
Everything actually works as intended until when I have to call a .NET method that requires a .NET type.
So I have a method named
I have a function like this in .Net
Public Sub SetTable(ByVal _City As City, ByVal _Country As Country)
'doing some thing
End Sub
and i have asp code like this:
dim CountryUtil, City, Country
set CountryUtil= Server.CreateObject("mydll.CountryUtil")
set city = Server.CreateObject("mydll.City")
set Country = Server.CreateObject("mydll.Country")
city.id= 123
city.property = "so and so"
Country.id= 123
Country.property = "so and so"
categoryUtil.SetTable(City, Country)
' I get this error here:
'Microsoft VBScript runtime error '800a0005'
'Invalid procedure call or argument: 'SetTable'
Thanks in advance.
if countryUtil is a class, you might have to initiate a new instance of it first.
Also, instead of Setting, you can just create new variables. Don't forget case sensitivity. If you try to pass the City instead of the city, it will give you problems.
''# Create a new instance of the categoryUtil Class
Dim countryUtil As New mydll.CountryUtil
Dim city As New mydll.City
Dim country As New mydll.Country
city.id= 123
city.property = "so and so"
country.id= 123
country.property = "so and so"
''# Instead of using the Class directly, you use the new instance of the Class
''# and pass the lowercase variables instead of the UpperCase Classes.
countryUtil.SetTable(city, country)
EDIT
If you were using a later version of the .NET framework, you could shorten it like this
''# Create a new instance of the categoryUtil Class
Dim countryUtil As New mydll.CountryUtil
Dim city As New mydll.City With {.id = 123, .property="so and so"}
Dim country As New mydll.Country With {.id=123, .property="so and so"}
''# Instead of using the Class directly, you use the new instance of the Class
''# and pass the lowercase variables instead of the UpperCase Classes.
countryUtil.SetTable(city, country)
EDIT
Check out this link to see how to mix asp and asp.net
http://knol.google.com/k/from-classic-asp-to-asp-net-and-back#
ASP values you set as values for the params are VARIANT.
But you defined different variable types in your function.
Example:
.NET Code:
Public Sub Test(param as String)
Classic ASP:
Dim yourParam : yourParam = "Testvalue"
YourClass.Test(yourParam)
This will fail.
Classic ASP:
Dim yourParam : yourParam = "Testvalue"
YourClass.Test(CStr(yourParam))
This will work.
So as resolution you need to take care to set the correct variable types while you call your function! In classic ASP everything is VARIANT.
Sidenote: Dictionary Objects, Array's are tricky to handle, I managed to define in .NET my variables as object[] and convert them inside the class.

vb.net Web Server linq to sql returns

My last question was not clear. Im trying to make a web service in VB.net is their a way that i can return the results that i get from LINQ. ie "return objreturnLINQResults"
I have tryed to set my Public Function GetAlarmsByGUIS(ByVal DeptGUID As String, ByVal IdNumber As String) As Linq.DataContext . i just keep getting errors. help please.
Public Function GetAlarmsByGUIS(ByVal DeptGUID As String, ByVal IdNumber As String) As Linq.DataContext
Dim lqAlarms As New linqAlarmDumpDataContext
Dim Temp As String = ""
Dim n As Integer = 0
Dim GetAlrms = From r In lqAlarms.AlarmDrops _
Where r.DeptGUID = DeptGUID And Not r.AlarmsHandled.Contains(IdNumber) _
Order By r.TimeDate Descending _
Select r
Return GetAlrms
End Function
1) You can't create web service's method which returns DataContext object.Return values and input parameters of Web service methods must be serializable through the XmlSerializer class. DataContext is not serializable
2) The simplest way to avoid errors it is return an array of serializable objects. Like this Return GetAlrms.ToArray();

Resources