1

Using SC10.1 & XC 10.1
I'm trying to get catalog items with a search criteria.
The code in this Sitecore document to get the Storefrontcontext is not working. enter image description here

var searchManager = CommerceTypeLoader.CreateInstance<ISearchManager>();
var storefrontContext = ServiceLocator.ServiceProvider.GetRequiredService<IStorefrontContext>();
SearchRepository searchRepository = new SearchRepository(searchManager, storefrontContext);
searchRepository.SearchCatalogItemsByKeyword("mdn","Brand", 10);

'IServiceProvider' does not contain a definition for 'GetRequestService' and no accessible extension method 'GetRequestService' accepting a first argument of type 'IServiceProvider' could be found.

enter image description here

1 Answer 1

7

Solution 1 : You can create a constructor and then find it from there as follows -

public IStorefrontContext StorefrontContext { get; set; }

public ISearchManager SearchManager { get; set; }

public YourController(IStorefrontContext storefrontContext, ISearchManager searchManager)
{
    this.StorefrontContext = storefrontContext;    
    this.SearchManager = searchManager;   
}

Then you can use your above code as -

SearchRepository searchRepository = new SearchRepository(this.SearchManager, this.StorefrontContext);
searchRepository.SearchCatalogItemsByKeyword("mdn","Brand", 10);

Solution 2 : If you want to use your code as is then add reference of Microsoft.Extensions.DependencyInjection.Abstractions.dll in your solution and use using Microsoft.Extensions.DependencyInjection; in your class

EDIT : If you haven't added your controller in config then need to add it as below. Create a patch file and add below lines -

<services>
      <register
        serviceType="abc.Feature.Catalog.Controllers.YourController, abc.Feature.Catalog"
        implementationType="abc.Feature.Catalog.Controllers.YourController, abc.Feature.Catalog"
        lifetime="Transient" />
</services>
2
  • I tried both solutions. It is null in both cases. Even tried including Sitecore.DependencyInjection namespace with no luck.
    – sukesh
    Commented Apr 22, 2021 at 7:37
  • 1
    The first soln. worked after adding the patch config.
    – sukesh
    Commented Apr 22, 2021 at 8:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.