Sunil
Sunil

Reputation: 21406

Issues converting Task based C# sample to VB.Net

I have following C# method that uses System.Threading.Tasks.Task class.

public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
  {
    HttpResponseMessage response =
                             new HttpResponseMessage(HttpStatusCode.InternalServerError);
    response.Content = new StringContent(Content);
    response.RequestMessage = Request;
    return Task.FromResult(response);
  }

I am having some issues writing above code in VB.Net. I have tried the following, but VS 2107 shows an error for the last line in the below function saying FromResult is not a member of Task.

Question: What would be correct equivalent of C# code in VB.Net for the last line in C# method?

Public Async Function ExecuteAsync(ByVal cancellationToken As CancellationToken) As Task(Of HttpResponseMessage)
    Dim response As HttpResponseMessage = New HttpResponseMessage(HttpStatusCode.InternalServerError)
    response.Content = New StringContent(Content)
    response.RequestMessage = Request
    Return   Task.FromResult(Of HttpResponseMessage) (response) 'this line shows error
End Function

Screenshot of error

VB.Net error in Vs 2017 code editor

The full C# class that contains this original method is as below.

public class GlobalExceptionHandler : ExceptionHandler
{
    public GlobalExceptionHandler()
    {
        //
        // TODO: Add constructor logic here
        //

    }
    public override void Handle(ExceptionHandlerContext context)
    {
        context.Result = new TextPlainErrorResult
               {
                   Request = context.ExceptionContext.Request,
                   Content = "Oops! Sorry! Something went wrong." +
                             "Our team has been informed and we will try to fix it as soon as possible."
               };
        base.Handle(context);
    }

    private class TextPlainErrorResult : IHttpActionResult
    {
        public HttpRequestMessage Request { get; set; }

        public string Content { get; set; }

        public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
        {
            HttpResponseMessage response =
                             new HttpResponseMessage(HttpStatusCode.InternalServerError);
            response.Content = new StringContent(Content);
            response.RequestMessage = Request;
            return Task.FromResult(response);
        }
    }
}

The converted VB.Net code of above C# code is as below.

Public Class GlobalExceptionHandler
    Inherits ExceptionHandler

    Public Sub New()
    End Sub

    Public Overrides Sub Handle(ByVal context As ExceptionHandlerContext)
        context.Result = New TextPlainErrorResult With {
            .Request = context.ExceptionContext.Request,
            .Content = "Oops! Sorry! Something went wrong." & "Our team has been informed and we will try to fix it as soon as possible."
        }
        MyBase.Handle(context)
    End Sub

    Private Class TextPlainErrorResult
        Implements IHttpActionResult

        Public Property Request As HttpRequestMessage
        Public Property Content As String

        Public Function ExecuteAsync(ByVal cancellationToken As CancellationToken) As Task(Of HttpResponseMessage)

            Dim response As HttpResponseMessage = New HttpResponseMessage(HttpStatusCode.InternalServerError)
            response.Content = New StringContent(Content)
            response.RequestMessage = Request

            Return new Task.FromResult(response)

        End Function
    End Class
End Class

Upvotes: 0

Views: 1024

Answers (2)

GSerg
GSerg

Reputation: 78190

You have almost correctly translated

public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
{
    HttpResponseMessage response =
                             new HttpResponseMessage(HttpStatusCode.InternalServerError);
    response.Content = new StringContent(Content);
    response.RequestMessage = Request;
    return Task.FromResult(response);
}

as

Public Async Function ExecuteAsync(ByVal cancellationToken As CancellationToken) As Task(Of HttpResponseMessage)
    Dim response As HttpResponseMessage = New HttpResponseMessage(HttpStatusCode.InternalServerError)
    response.Content = New StringContent(Content)
    response.RequestMessage = Request
    Return Task.FromResult(Of HttpResponseMessage) (response) 'this line shows error
End Function

The only mistake was the addition of Async that was not present in the original, so it should be removed:

Public Function ExecuteAsync(ByVal cancellationToken As CancellationToken) As Task(Of HttpResponseMessage)
    Dim response As HttpResponseMessage = New HttpResponseMessage(HttpStatusCode.InternalServerError)
    response.Content = New StringContent(Content)
    response.RequestMessage = Request
    Return Task.FromResult(Of HttpResponseMessage) (response) 'this line shows error
End Function

Async is not a part of a method signature and is not a requirement for writing methods that can be consumed with await - as long as the method returns a Task, it can be awaited.

The C# original does not use async in its definition because it is in fact synchronous (and so is your VB translation). Using Async on that function will only result in a compiler warning, fixing which will require removing the Async.

The use of Task.FromResult(Of HttpResponseMessage) (response) is correct and may even be simplified to Task.FromResult(response) because the compiler is able to infer the type.

If for you it shows an error about FromResult not being a member of Task (which it certainly is), there there must be another class named Task defined in your solution that shadows the System.Threading.Tasks.Task class. In that case refer to it in a more elaborate way, e.g. Tasks.Task.FromResult(response) or Threading.Tasks.Task.FromResult(response).

Upvotes: 1

Code Pope
Code Pope

Reputation: 5459

Remove the Task.FromResult in the last line:

   Public Async Function ExecuteAsync(ByVal cancellationToken As CancellationToken) As Task(Of HttpResponseMessage)
        Dim response As HttpResponseMessage = New HttpResponseMessage(HttpStatusCode.InternalServerError)
        response.Content = New StringContent("")
        response.RequestMessage = Request
        Return   response 
    End Function

Upvotes: 0

Related Questions