0

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

6

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. – Qwerty Apr 22 at 7:37
  • The first soln. worked after adding the patch config. – Qwerty Apr 22 at 8:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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