Wednesday, May 17, 2023

Crystal Reports font problem in Exported PDF

So, we had this issue, that one of our Web App running on Server was generating a PDF file using Crystal Reports Export API. The Crystal Reports file had barcode fonts to generate barcodes. But the exported PDF was showing the normal Arial font instead of barcodes.

First I investigated that PDF file itself, whether barcode fonts are being embedded in the PDF file or not. So, I realized that instead of barcode fonts, it has Arial font only. So Crystal Reports export system was not embedding the Barcode Fonts during PDF file creation process.

Though we had already installed the fonts on the server, but the font was installed for the user which was logged in at the time of installation. So, I right clicked on the font file and clicked on Install for all Users. This resolved the issue and barcode fonts started to appear in PDF.

Note that this problem can occur for other reasons as well, for example, the fonts are secured and they are not allowed to be embedded. But in my case installing the fonts for all users resolved the problem.

Sunday, April 16, 2023

How to prevent authentication cookie to be renewed on background (polling) AJAX call in ASP.net core

I know that we should be using Web Sockets / SignalR for server side push notifications instead of polling. But I was looking for a quick solution to a simple problem where there is an AJAX call being made after every 5 seconds to server.

This web application is using cookie based authentication with SlidingExpiration. So, whenever any AJAX call is being made to the server, the cookie is renewed. Which is causing the user to be logged in for an indefinite time.

I wanted to have one specific API call to be marked in a way so that it should not renew the cookie. In this way, non-active user will be logged out automatically, even with continuous AJAX calls being made in the background.

We can use OnCheckSlidingExpiration event in cookie settings where we can check if our specific action is being called then we can simply set ShouldRenew to false.

    cookieOptions.Events.OnCheckSlidingExpiration = context =>
    {
        var controllerName = context.Request.RouteValues["controller"] as string;
        var actionName = context.Request.RouteValues["action"] as string;
        if (controllerName == "MyController" && actionName == "MyAction")
            context.ShouldRenew = false;
        return Task.CompletedTask;
    };

I didn't like this solution but unfortunately, I couldn't find any ActionFilterAttribute or any other way so that I can set some type of flag in the pipeline from action level, which can tell cookie event to not renew the cookie. It seems like the cookie events are the first thing in the pipeline that's executed, and action filters are executed later.

Thursday, January 26, 2023

Cache or Not to Cache LambdaExpression.Compile

As per below statement from Microsoft, don't need to create a cache mechanism to avoid LambdaExpression.Compile() calls.

I will caution you against trying to create any more sophisticated caching mechanisms to increase performance by avoiding unnecessary compile calls. Comparing two arbitrary expression trees to determine if they represent the same algorithm will also be time consuming to execute. You'll likely find that the compute time you save avoiding any extra calls to LambdaExpression.Compile() will be more than consumed by the time executing code that determines of two different expression trees result in the same executable code.

However, still not sure if again and again compiling of an expression tree is memory friendly or not.

Source: https://learn.microsoft.com/en-us/dotnet/csharp/expression-trees-execution

A guy here is claiming that there seems to be no memory problem. I guess we'll see in production :P.