Dynamically creating a property depending on what an API is calling.

Yves Guyon 1 Reputation point
2020-12-01T22:02:04.823+00:00

I have the following scenario.

My ultimate goal is to return my "trackingId" if I have (serviceResults.Status == ServiceStatus.Success). First I'm getting a pending status and my loop will work correctly then it keep looping until I get a success status and my variable are not set correctly.

My only issue is visual studio does not recognize this property on the first call serviceResults.partnerReference.trackingID . I'm trying to come with a better logic. Any help will be appreciated...

OLD CODE

       var finished = false;  
        var trackingId = ""  
  
        do {  
           var serviceResults = await client.Services.GetServiceOrderStatusAsync.....   
              
           //This is correct  
           if (serviceResults.Status == ServiceStatus.Pending) {  
               finished = false;  
           }  
           // This is not correct because this serviceResults.partnerReference.trackingID was never   
           //initialized on the first call.  
           if (serviceResults.Status == ServiceStatus.Success) {  
               trackingId = serviceResults.partnerReference.trackingID  -> this never exist visual studio doesn't recognize it  
               finished = true;  
           }  
              }  
       while (!finished);  

New Code on what I tried.

   var finished = false;  
            string trackingId;  
  
            do {  
               var serviceResults = await client.Services.GetServiceOrderStatusAsync("11158174", transactionId).ConfigureAwait(false);  
                  
               if (serviceResults.Status == ServiceStatus.Pending) {  
                   finished = false;  
               }  
               if (serviceResults.Status == ServiceStatus.Success) {  
                      
                    dynamic serviceResulys = await client.Services.GetServiceOrderStatusAsync("11158174", transactionId).ConfigureAwait(false);  
                    trackingId = serviceResulys.serviceResults.trackingID;  
                    finished = true;  
               }  

Here is my response from postman:

44156-capturebefore.png

44212-captureafter.png

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,358 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Jamal Khan 1 Reputation point
    2021-02-18T03:37:20.94+00:00

    I'm not seeing all of your code, but I suspect you're returning a dynamic object as the result of an API call. Are you able to return a specific type?

    Here's what I mean...

    I think this is what you're doing:

    [HttpGet("people/all")] 
    public ActionResult GetAll()
    {
        return new []
        {
            new Person { Name = "Ana" },
            new Person { Name = "Felipe" },
            new Person { Name = "Emillia" }
        };
    }
    

    Can you instead do this:

    [HttpGet("people/all")] 
    public ActionResult<IEnumerable<Person>> GetAll()
    {
        return new []
        {
            new Person { Name = "Ana" },
            new Person { Name = "Felipe" },
            new Person { Name = "Emillia" }
        };
    }
    

    Notice the return type is ActionResult<IEnumerable<Person>>. Can you define the return type that includes partnerReference and trackingID as nullable data elements?

    0 comments No comments