C#

Transient fault handling in Sitecore Commerce

Posted on

Transient fault handling can be cumbersome and lead developers to write repetitive code. There could be many situations when you want your code to be retried in case of failures/exceptions etc. While working with a few external system integrations, we had to implement a retry mechanism for certain transactional requests. Payment integrations are one of the examples that I am using here. Polly is a library that allows developers to express resilience and transient fault handling policies such as Retry, Circuit Breaker, Timeout, Bulkhead Isolation, and Fallback in a fluent and thread-safe manner. Almost all the payment providers we integrated to Sitecore Commerce project e.g. Braintree/DiectBank/Openpay recommend to retry the transactional requests in case of timeouts or internet issues. For example when we request Braintree payment gateway to settle the payment if the request times out you have to retry the same request 3 times before you treat it as a failure. Since the retry could be handled […]

C#

Http Caching for Web API Responses

Posted on

A Web API serves a client e.g. browser with an HTTP response upon receiving an HTTP request. Sometimes these responses could be huge size wise and this can lead to delays in displaying the response to the user. To cater such things, almost all the web browsers (Chrome, IE, Mozilla) have built-in capabilities to cache the http responses for a given timespan. A cache is an in memory store for data which is used to improve the performance. Read more about cache… Caching of a response at the browser is not automatic. In the response headers we have to raise max-age or expires flag to tell the browser, it has to cache the response for a given timespan. Until this cache remains valid, browser picks data from cached response rather going back to the server again. Validity of browser’s cache depends on the value of max-age or expires headers. You […]

Web API

Downloading a File Using Web Api with Jquery or Knockout

Posted on

This short walk-through is intended for those who work with WebApi and want to perform file download using Knockout or JQuery. While working on a project that uses Knockout.js for front end scripting and Asp.Net Web API as back-end service layer, we had to implement file downloading functionality using Ajax. Lets Code

This method in Api returns a FileStream, sets ContentDisposition and ContentType to tell browser the type of content is a file and browser popups save file dialog. GetFileStream method is defined in a Helper class that just converts a file into filestream

Knockout Devs Knockout developers have to create a view and a viewModel. The viewModel will expose a caller for API while the view will simply have an anchor tag which will have its click event bound to viewModel’s funtion. ViewModel will have the following caller function

While the view’s anchor tag will look like

JQuery Devs […]