Thursday, July 23, 2020

EF Sum Error Due to Empty Rows After Filtering on MySQL

I actually have been waiting when I will encounter this kind of error. This time the error happens when performing EF query on MySQL database.

Problem

After filtering, the query returns empty rows thus sum can't work. The error message in my case is:

"The cast to value type 'System.Decimal' failed because the materialized value is null. Either the result type's generic parameter or the query must use a nullable type."

The following is an example code:
Dim totalPrice = dbContext.Items.Where(Function(i) i.Color = "Blue").Sum(Function(i) i.Price)

However it works fine if we execute the query first before Sum, but it requires the rows to be pulled to memory which can be resource intensive.
Dim totalPrice = dbContext.Items.Where(Function(i) .Color = "Blue").ToList().Sum(Function(i) i.Price)

Solution

One helpful article:


The solution in my case is to perform projection, followed by DefaultIfEmpty and call Sum() afterwards. The code becomes:
Dim totalPrice = dbContext.Items.Where(Function(i) i.Color = "Blue").Select(Function(i) i.Price).DefaultIfEmpty(Decimal.Zero).Sum()

Wednesday, July 22, 2020

Frustrating "Please wait for an editor command to finish" Pop-up When Editing JavaScript

It happens to be I need to edit a massive JavaScript file and I tried to speed through it, however, my instance of Visual Studio 2017 is not helpful by trying to be helpful.

Problem

Every single key stroke triggers a 1-5 seconds pause which sometimes caused a pop-up with "Please wait for an editor command to finish" message to show. Worse, that pop-up is not cancelable.

Additionally, my CPU usage was hovering at 80-90% just editing JavaScript! As of this time, my Visual Studio 2017 version is 15.9.25.

Solution

Seems like it is triggered by some auto-suggestion tools which in Visual Studio, Intellisense seems to be the culprit. I ended up going to Tools > Options > JavaScript/TypeScript > Formatting > General and uncheck everything under Automatic Formatting. 

Even after that I still noticed a half-a-second delay on some keystrokes, so I also uncheck Tools > Options > JavaScript/TypeScript > Linting > General > Enable ESLint.

That made my life a whole lot easier. It also drops my CPU usage to 10-20%. 

Monday, July 13, 2020

Checking Anonymous Authentication Allowed on ASP.NET OWIN Middleware and Web Forms

Some business logic on our web application apparently caused issue when hitting a page that allow anonymous authentication. And it seems there is no simple flag that indicates whether a page requires authorization or not.

Problem

I need to check for allow anonymous in ASP.NET Web Forms page and on OWIN middleware.


Solution


For ASP.NET Web Forms page, I found the following thread in StackOverflow which works great:


In my case, I need to convert the code to VB.NET, so it becomes:

Dim principal = New GenericPrincipal(New GenericIdentity(String.Empty, String.Empty), New String() {})
Dim isAllowAnonymous = UrlAuthorizationModule.CheckUrlAccessForPrincipal(Page.AppRelativeVirtualPath, principal, Context.Request.HttpMethod).ToString()

And on OWIN middleware, I need to tweak the above a little bit, so it becomes:

Dim principal = New GenericPrincipal(New GenericIdentity(String.Empty, String.Empty), New String() {})
Dim isAllowAnonymous = UrlAuthorizationModule.CheckUrlAccessForPrincipal(context.Request.Uri.AbsolutePath, principal, context.Request.Method)

IIS 401 Error on New Website

I had a new site setup on IIS and thought I got everything setup fine. Everything seems ok from:
  • Binding
  • SNI
  • Certificate
  • Enable Allow Anonymous
  • Directory permission
  • Redirect
  • etc
Guess what? Can't even access my home page.

Problem

The server throws the following error:

401 - Unauthorized: Access is denied due to invalid credentials.
You do not have permission to view this directory or page using the credentials that you supplied.

Usually it is because of permission on the directory. But this time it looks fine. Weird thing is I can visit aspx page, but some of the static files are blocked.

Solution

After some browsing, I noticed the value under Site > Authentication > Anonymous Authentication > Edit... (right-click) of the other site that works fine was set to Application pool identity. Meanwhile, my new site was set to Specific user: IUSR and my directory only allow access to IIS_IUSRS.

In my case, setting the above to Application pool identity solves my issue.

Sunday, July 12, 2020

AWS CodeDeploy Failed Deployment in Windows Server

Our CI/CD pipeline has been going smoothly for a long time. Today, I happened to need to deploy something and it failed.

Problem


First, I looked in the console and viewing the events, it failed at the first step which is ApplicationStop.

So I thought the application is still running which is usually the case when the deployment failed but this time no instance of my application is being run at that moment.

Next, I check the log file which can be found in:


The log file indicated the agent can't connect to the host. One of the error message in the log is "certificate verify failed".

I restarted the CodeDeploy agent but the issue persisted.

Solution

Eventually, by uninstalling and updating the CodeDeploy agent solves my issue. In my case, which is in Windows Server, I had to stop the agent service and uninstall to enable the update to success.