I'm getting a compilation error when attempting to convert a C# function into a VB.Net equivalent. PageAsyncTask in C# is expecting a Task input, but in VB.Net it's looking for Func(Of Task). None of the online converters I can find are correctly translating the language. The error is: Value of type 'Task' cannot be converted to 'Func(Of Task)'
Not sure how to proceed (I'm guessing I need to define an Event?). Here's the original C# code
protected void Page_Load(object sender, EventArgs e)
{
{
AsyncMode = true;
if (!dictionary.ContainsKey("accessToken"))
{
if (Request.QueryString.Count > 0)
{
var response = new AuthorizeResponse(Request.QueryString.ToString());
if (response.State != null)
{
if (oauthClient.CSRFToken == response.State)
{
if (response.RealmId != null)
{
if (!dictionary.ContainsKey("realmId"))
{
dictionary.Add("realmId", response.RealmId);
}
}
if (response.Code != null)
{
authCode = response.Code;
output("Authorization code obtained.");
PageAsyncTask t = new PageAsyncTask(performCodeExchange);
Page.RegisterAsyncTask(t);
Page.ExecuteRegisteredAsyncTasks();
}
}
else
{
output("Invalid State");
dictionary.Clear();
}
}
}
}
else
{
homeButtons.Visible = false;
connected.Visible = true;
}
}
}
And what the code is converted to:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If True Then
AsyncMode = True
If Not dictionary.ContainsKey("accessToken") Then
If Request.QueryString.Count > 0 Then
Dim response = New AuthorizeResponse(Request.QueryString.ToString())
If response.State IsNot Nothing Then
If oauthClient.CSRFToken = response.State Then
If response.RealmId IsNot Nothing Then
If Not dictionary.ContainsKey("realmId") Then
dictionary.Add("realmId", response.RealmId)
End If
End If
If response.Code IsNot Nothing Then
authCode = response.Code
output("Authorization code obtained.")
Dim t As New PageAsyncTask(performCodeExchange)
Page.RegisterAsyncTask(t)
Page.ExecuteRegisteredAsyncTasks()
End If
Else
output("Invalid State")
dictionary.Clear()
End If
End If
End If
Else
homeButtons.Visible = False
connected.Visible = True
End If
End If
End Sub
The problem area:
Dim t As New PageAsyncTask(performCodeExchange)
The task function is performCodeExchange which returns a Task
Public Async Function performCodeExchange() As Task
output("Exchanging code for tokens.")
Try
Dim tokenResp = Await oauthClient.GetBearerTokenAsync(authCode)
If Not _dictionary.ContainsKey("accessToken") Then
_dictionary.Add("accessToken", tokenResp.AccessToken)
Else
_dictionary("accessToken") = tokenResp.AccessToken
End If
If Not _dictionary.ContainsKey("refreshToken") Then
_dictionary.Add("refreshToken", tokenResp.RefreshToken)
Else
_dictionary("refreshToken") = tokenResp.RefreshToken
End If
If tokenResp.IdentityToken IsNot Nothing Then
idToken = tokenResp.IdentityToken
End If
If Request.Url.Query = "" Then
Response.Redirect(Request.RawUrl)
Else
Response.Redirect(Request.RawUrl.Replace(Request.Url.Query, ""))
End If
Catch ex As Exception
output("Problem while getting bearer tokens.")
End Try
End Function
And for thoroughness, the original C# code:
public async Task performCodeExchange()
{
output("Exchanging code for tokens.");
try
{
var tokenResp = await oauthClient.GetBearerTokenAsync(authCode);
if (!dictionary.ContainsKey("accessToken"))
dictionary.Add("accessToken", tokenResp.AccessToken);
else
dictionary["accessToken"] = tokenResp.AccessToken;
if (!dictionary.ContainsKey("refreshToken"))
dictionary.Add("refreshToken", tokenResp.RefreshToken);
else
dictionary["refreshToken"] = tokenResp.RefreshToken;
if (tokenResp.IdentityToken != null)
idToken = tokenResp.IdentityToken;
if (Request.Url.Query == "")
{
Response.Redirect(Request.RawUrl);
}
else
{
Response.Redirect(Request.RawUrl.Replace(Request.Url.Query, ""));
}
}
catch (Exception ex)
{
output("Problem while getting bearer tokens.");
}
}
I'm not sure what to do here - pass in a delegate? how can this be done with a task (in VB.Net)?
When executing PageAsyncTask t = new PageAsyncTask(performCodeExchange);
in C#, a delegate pointing to the performCodeExchange
method is implicitly created and passed to the constructor of PageAsyncTask
.
Now the statement Dim t As New PageAsyncTask(performCodeExchange)
in VB is subtly different. Functions in VB can be evaluated without parenthesis, so this is equivalent to Dim t As New PageAsyncTask(performCodeExchange())
. Which means that the constructor of PageAsyncTask
receives the result of the evaluation of performCodeExchange
instead of a delegate to the method.
To get a delegate in VB, you can use the AdressOf
keyword. The code should be rewritten as:
Dim t As New PageAsyncTask(AddressOf performCodeExchange)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.