Greg lindquist (Customer) asked a question.

iRestClient returns 'Object reference not set to an instance of an object' until the first page is loaded during application lifecycle.

Hi all,

 

We're writing an API from the renderer application of a headless instance of Sitefinity. Everything works great, except we notice any of the SDK requests to the CMS throw an error if the API happens to be the first request made directly after a build or app pool recycle. The error resolves if we visit the homepage of the website - which pulls information directly from the CMS. Is there any way we can 'warm up' the API? Currently using 15.3.8523

 

This is the stack trace when the error occurs:

  1. Progress.Sitefinity.RestSdk.Client.RestClientExtensionsTaxonomies.AdjustArgsToHandleTaxonomy(IServiceMetadata metadata, CommonArgs args)
  2.  
  3. at Progress.Sitefinity.RestSdk.Client.RestClientExtensionsTaxonomies.AdjustArgs(IServiceMetadata serviceMetadata, CommonArgs args)
  4.  
  5. at Progress.Sitefinity.RestSdk.Client.RestClient.<GetItems>d__28`1.MoveNext()

 


  • Jonathan Read (Progress Software)

    What does this mean?

    We're writing an API from the renderer application of a headless instance of Sitefinity.

    Can you share code or abbreviated code?

    You mention headless. Are you creating your own frontend app or are you using the Sitefinity ASP.NET Core renderer?

     

    If it is a custom app how are you configuring the RestClient or where are you configuring it? Could be a race condition.

     

     

  • Greg lindquist (Customer)

    Good questions! We host a .net 9 core application that leverages the Sitefinity 'Progress.Sitefinity.RestSdk' namespace logic to retrieve custom module data for frontend purposes in addition to the headless functionality built into the Sitefinity CMS hosted at cms.example.com.

     

    Embedded in this .net 9 core application is a custom openAPI 3.0 implementation hosted at www.domain/api. When a GET request is made to our endpoint at www.example.com/api/request, the API eventually calls this block of code below.

    1. List<TagDto> tags = new List<TagDto>();
    2.  
    3. var getAllRqst = new GetAllArgs
    4. {
    5. Type = RestClientContentTypes.Tags,
    6. Take = 100,
    7. Fields = new String[] { "Id", "Title", "UrlName" }
    8. };
    9.  
    10. var tagItem = await restClient.GetItems<TagDto>(getAllRqst);
    11. tags.AddRange(tagItem.Items);

    If the API request happens to be the first request during the application lifecycle (for example, immediately after an app pool recycle), this request fails with the stack trace I provided above. To fix this, I just need to visit the homepage - which leverages a widget and other built in CMS based HTML. Once I visit the page and retry the same API endpoint - the data retrieves as expected.

     

    In our program.cs, we setup the services in this order:

    1. var app = builder.Build();
    2. app.MapOpenApi();
    3.  
    4. app.UseHsts();
    5. app.UseStaticFiles();
    6. app.UseRouting();
    7.  
    8. app.UseSitefinity();
    9.  
    10. app.UseMiddleware<RequestIdMiddleware>();
    11.  
    12. app.UseAuthentication();
    13. app.UseAuthorization();
    14.  
    15. app.UseEndpoints(endpoints =>
    16. {
    17. endpoints.MapSitefinityEndpoints();
    18. });
    19.  
    20. app.Run();