Tuesday 18 August 2020

Debugging Sitecore Commerce SDK

Sitecore Commerce Engine is a light weight .Net Core application which is extended with plenty of other plugins (Pricing, Carts, Promotions, Search etc.), which in turn are other independent .Net Core projects, to make an Experience Commerce solution.

Sitecore provides an SDK which can be used as a starting point for customizing the commerce solution. You can use this SDK to extend the commerce solution either by adding new plugins or extending the existing plugins.

Debugging the .Net Core applications are slightly different. To make .Net Core applications run across multiple platforms like Windows, Linux, Unix etc. it uses Kestrel web server. Kestrel web server is by default specified in ASP.Net Core project templates and Sitecore Commerce Engine is no exception. 

So, we will configure Kestrel web server middleware for debugging commerce SDK. 

Alright, let's start...

Commerce Engine Project Configuration

  • Open Commerce.Sample.Solution.sln file in Visual Studio (Run As Admin). Visual Studio version should not be lower than 2017.

  •  In Sitecore.Commerce.Engine project, open wwwroot/config.json file.
 
  • Search SslPort setting in config.json and change its value to 5001. We will use 5001 because default port 5000 might already been in use by one of the commerce engine service that you might have installed on Sitecore. Keep other settings as is. All these configurations are used by Kestrel web server middleware which you can find in Program.cs file BuildWebHost method
 
TIP: This is just for your information,



 
  •  One more thing, you may have to verify is the SQL server configuration in /wwwroot/bootstrap/Global.json file. If it is not properly configured, copy this block from one of your already running Commerce Engine application viz. CommerceAuthoring or CommerceOps. You will find this block in Global.json file at the same location in hosted webroot.
 
  •  Optional Step: Update the port in Sitecore.Commerce.Engine/Properties/launchSettings.json
 

Export SSL Certificate

  • Kestrel web server will be listening to https://localhost:5001 as per above configuration, so we need to setup SSL certificate for this. 
  • We will export certificate attached to one of the Commerce applications from IIS.
  • To export certificate, open IIS, right click on CommerceAuthoring site, then click Edit Bindings...
 
  •  Double click on the host line item which has a valid certificate attached.
  •  Click on View... to view the certificate attached with this host name and switch to Details tab.
  • Click on Copy to File.. to export this certificate. 
 
  • Select Yes, export the private key option and click Next.
  • Select .PFX format and click Next. On the Next step, use password as sitecore (all lowercase) and click Next.

  • Click on Browse button and save the certificate as localhost.pfx under Sitecore.Commerce.Engine/wwwroot/ folder

Attach to the debugger

  • Once the certificate is generated and placed under appropriate location, next thing is to attach the project to the debugger
  • Set Sitecore.Commerce.Engine project as the Startup project and select Engine from the launch profiles
  • Press F5 to run the application. You will see CLI window popping up. After some time hit https://localhost:5001/api/$metadata url in browser. If you followed these steps properly, you should see entity metadata loading on this page. 
  • You will see this request logged in CLI.
  • Your debugger is running now, you can replace the ServiceHost parameter value with https://localhost:5001 for debugging your postman APIs.

 

TIP: Put a break point in BrainTree plugin, and complete the checkout process by postman APIs. Debugger should hit on the Create Order step.

Do let me know in the comment section with error description and screenshot if you are facing issue running the debugger.

Till then, Happy Commerce Debugging 🙂 

 

No comments:

Post a Comment