Home > Back-end >  Nswag throwing an error message on a simple get trying to consume it
Nswag throwing an error message on a simple get trying to consume it

Time:12-15

I am blind or am simply not seieng the issue here. I am using nswagger to gen my clients and am getting an issue with the models

@code {
private Task<System.Collections.Generic.IEnumerable<Customer>>? customers;

protected override async Task OnInitializedAsync()
{
    var test =new  TraditioWebApIClient(Constants.BaseUrl,new HttpClient());
    customers = await test.GetAllCustomersAsync();
}
}

Model

public class Customer
{
    public int Id { get; set; }
    public ICollection<Address>? Addresses { get; set; }
    public int? CompanyType { get; set; }
    public Company? Company { get; set; }
    public string? FirstName { get; set; }
    public string? LastName { get; set; }
    public string? Dob { get; set; }
    public string? Email { get; set; }
    public string? Mobile { get; set; }
    public string? Description { get; set; } 
    public string? City { get; set; }
    public string? Region { get; set; }
    public string? PostalCode { get; set; }    
  
}

Now the error I get is

Severity Code Description Project File Line Suppression State Error (active) CS1579 foreach statement cannot operate on variables of type 'Task<IEnumerable>' because 'Task<IEnumerable>' does not contain a public instance or extension definition for 'GetEnumerator' D:\GitMaster\Traditio\Traditio.Blazor\Pages\Customers.razor 29

Nswagger generated this for the method call.

/// <returns>Success</returns>
/// <exception cref="ApiException">A server side error occurred.</exception>
public virtual 
     System.Threading.Tasks.Task<System.Collections.Generic.IEnumerable<Customer>> 
        GetAllCustomersAsync()
{
        return GetAllCustomersAsync(System.Threading.CancellationToken.None);
}

I just dont no why the error is their as you see am expecting a task here.

private Task<System.Collections.Generic.IEnumerable<Customer>>? customers;

Edit 2 Output window output

error CS0266: Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<Traditio.Models.Customer>' to 'System.Threading.Tasks.Task<System.Collections.Generic.IEnumerable<Traditio.Models.Customer>>'. An explicit conversion exists (are you missing a cast?)

CodePudding user response:

await runs the task, returning its value.

In this case, when the task is run, the returned value has type:
System.Collections.Generic.IEnumerable<Customer>.

Which obviously can't go into customers, as the error message indicates.

Add to question an example code snippet, showing how you intend to use customers later.

Once you've shown how you will use customers later, it will be possible to tell you the needed code correction.

Two possibilities:

  • You really do intend customers to be a task, rather than an enumeration of customers. In this case, consider renaming it customersTask. I'll add to answer the other code changes you need.

  • You meant to have customers be an enumeration of customers. Change its declaration to:
    private System.Collections.Generic.IEnumerable<Customer>? customers.

  • Related