Problem object service parameter

Stefano Milanesi 21 Reputation points


in the startup.cs module of a Blazor server project I declared this endpoint:

            endpoints.MapPost("/timestamp/addtimestamp/", (context) =>
                var timestamp = context.Request.ReadFromJsonAsync<Timestamp>();
                (Timestamp ReturnTimestamp, var error) = app.ApplicationServices.GetService<TimestampService>().AddTimestamp(timestamp);
                var json = JsonSerializer.Serialize(new { ReturnTimestamp = ReturnTimestamp, error = error });
                return context.Response.WriteAsync(json);

the AddTimestamp service should receive a TimeStamp object as "parameter":

public (Timestamp, Error) AddTimestamp(Timestamp timestamp)

I get this error:
Error CS1503 Argument 1: cannot convert from 'System.Threading.Tasks.ValueTask<TSNet.BO.Timestamp?>' to 'TSNet.BO.Timestamp'

the "timestamp" object, that is passed as a parameter to AddTimestamp servize, is not Timestamp obejct

How can I "convert" it?


A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,375 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Bruce ( 54,866 Reputation points

    you should learn about c# types and nullability.

    ReadFromJson can return a null (no content), so it return Nullable<Timestamp>. This means you must cast the return value to non-null to pass as a value type rather than a nullable object.

                (Timestamp ReturnTimestamp, var error) = app.ApplicationServices.GetService<TimestampService>().AddTimestamp((Timestamp) timestamp);
    0 comments No comments