Saturday, March 30, 2019

Method not found System.Net.Http.HttpContentExtensions.ReadAsAsync

Bumped into this error when moving my web app to another server. It happened to me before but this time the cause is different. So two ways that worked for me:

1. Install package Microsoft.AspNet.WebApi.Client. This will provide access to HttpFormatting.dll which actually contains the ReadAsAsync method and fixed the issue for me before.

2. I found out that my System.Net.Http was not referenced properly because it depends on the dll installed in the machine. So, installing System.Net.Http NuGet package fix the current issue for me.

Friday, March 29, 2019

Where is My Environment Variables? Journey to Linux Service

Ok, I had a .NET Core Web App running in Ubuntu behind Nginx. Everything else is fine except I can't retrieve the value of the environment variables that I put in /etc/environment.

After hours of googling, turns out systemd service strips all out except some variables. Two ways to fix this:

1. Put the environment variable in the .service config file

[Service]
Environment=MY_ENV_VAR=thevalue

2. Include /etc/environment in the service. (I don't think this is a good idea, especially for my use case).

[Service]
EnvironmentFile=/etc/environment


Thursday, March 21, 2019

AWS SSM Linux Shell Script Closing Paren Expected Error

I ran my scripts through AWS SSM and received the "closing paren expected" error message. Quick check on my code, I was missing items in two different situations:

1. I was missing closing parentheses \), so adding it solves the issue. My code was like:

if [ \( <expr> ]; then <do this>; fi

2. My closing parentheses was not prefixed by space, so adding a space fixed it. It was like:

if [ \( <expr>\) ]; then <do this>; fi

Linux Script Conditional Conditions

Like most people, I guess, I came from Windows background. Just recently, I have projects exploring multiple flavors of Linux and spending a lot of time to understand how conditions work in bash and/or shell script in Linux. And then my code didn't work and that took me on a journey.

Long time ago, I tried to do something as simple as the following:

if (<expr1> or <expr2>) and (<expr3> or <expr4>) then <do this> fi

But things get complicated as the expressions involve which and grep commands.

For example, I'm checking if any python is installed, so my first attempt was

if [ "`which python`" = "" -a "`which python3`" = "" ]; then echo "no python"; fi

then I realize that in RedHat, if there is no python installed, it will return a string containing "no python" instead of empty string, so my code becomes

if [ \( "`which python`" = "" -o "`which python | grep 'no python'`" = "" \) -a \( "`which python3`" = "" -o "`which python3 | grep 'no python3'`" = "" \) ]; then 

     echo "no python"; 

fi

Well, it didn't work. Scrutinizing it a bit more. I found out that when there is no python, grep doesn't return empty string, but instead a failure code, so that turns my code into:

if [ \( "`which python`" = "" -o "`which python | grep 'no python'`" \) -a \( "`which python3`" = "" -o "`which python3 | grep 'no python3'`" \) ]; then 

     echo "no python"; 

fi


Which seems to work so far, but for consistency, I prefer to use the flag -z to check for empty string and -n to check for non empty string, so the code becomes

if [ \( -z "`which python`" -o -n "`which python | grep 'no python'`" \) -a \( -z "`which python3`"  -o -n "`which python3 | grep 'no python3'`" \) ]; then 

     echo "no python"; 

fi