NMNamuAgain
NMNamuAgain

Reputation: 59

'there is no argument given that corresponds to required formal parameter'

I'm getting an error I can't seem to resolve.. I'm trying to fetch data from an API do a model and I'm getting an error which says:

'there is no argument given that corresponds to required formal parameter'

[HttpGet("basket/{identifier}")]
        public async Task<OrderDTO> FetchBasket(string identifier)
        {
            var httpRequestMessage = new HttpRequestMessage(
               HttpMethod.Get,
               $"https://localhost:5500/api/Basket/{identifier}")
            {
                Headers = { { HeaderNames.Accept, "application/json" }, }
            };

            var httpClient = httpClientFactory.CreateClient();

            using var httpResponseMessage =
                await httpClient.SendAsync(httpRequestMessage);

            OrderDTO orderDTO = null;

            if (!httpResponseMessage.IsSuccessStatusCode)
                return orderDTO;

            using var contentStream =
                await httpResponseMessage.Content.ReadAsStreamAsync();

            var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };

            var orderServiceDtoExtract = await JsonSerializer.DeserializeAsync
                    <OrderLine>(contentStream, options);

            orderDTO = new OrderLine // I'm getting the error here
            {
                ProductId = orderServiceDtoExtract.ProductId,
                Quantity = orderServiceDtoExtract.Quantity
            };

            return orderDTO; // 200 OK
        }

This is my model:

 public class OrderLine
    {
        public OrderLine(int productId, int quantity)
        {
            ProductId = productId;
            Quantity = quantity;
        }

        public int Id { get; set; }

        public int ProductId { get; set; }

        public int Quantity { get; set; }
    }

Upvotes: 0

Views: 1037

Answers (1)

gunr2171
gunr2171

Reputation: 17520

When making an instance of the OrderLine type, you're using the Object Initializer syntax. However, because you explicitly made a Constructor with non-zero arguments, you must call that first.

orderDTO = new OrderLine(orderServiceDtoExtract.ProductId, orderServiceDtoExtract.Quantity);

Optionally, you can use the Object Initializer syntax after calling the constructor:

orderDTO = new OrderLine(orderServiceDtoExtract.ProductId, orderServiceDtoExtract.Quantity)
{
   // set more properties...
};

Upvotes: 4

Related Questions