Archive for June, 2022

Handling the effect of HttpContext null in a separate thread in ASP.NET

If you have an ASP.NET page that needs to respond quickly, but perhaps has a slow background task to do, that would be ideal to perform in a background thread. However, suddently you find that HttpContext is null, so you can no longer access server features in the background thread.

The case could be a webhook, where a third party calls your server, and has a timeout of a few seconds before they kill the connection to your server. but you have some large processing to do, that won’t be done in time, or potentially won’t be done in time.

Otherwise, it could be just a performance optimization, you have a few things you need the user to see, but there are other tasks that can happen in the background, that the user doesn’t need to wait for.

Here’s the problematic code:

    protected void Page_Load(object sender, EventArgs e)
        var asyncJob = new Thread(() => HandleJob());

    private static void HandleJob()

But this code fails, because HttpContext.Current will be null in the context of the thread. It also will fail silently, since the page will load fine.

The Fix? – move any variables that you might need to access outside of the Thread

    protected void Page_Load(object sender, EventArgs e)
        var jobId = Request.QueryString["jobId"];
        var asyncJob = new Thread(() => HandleJob(jobId));

    private static void HandleJob(string JobId)

Categories: Uncategorized

Get a user’s profile picture from their Email address in #PHP using AvatarAPI

The new Avatar API “v2” has code examples for cUrl (Command Line), Ruby, Python and Node, but PHP was overlooked, so here is a code example to help users get their PHP code up and running;

$email = "";

$json = '{ 
    "username" : "your-username-here",
    "password" : "yout-password-here",
    "email" : "$email"

$json = eval('return "' . addslashes($json) . '";');

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => '',

$response = curl_exec($curl);

if (curl_errno($curl)) {
	// Optional (but recommended) error handling.
    $error_msg = curl_error($curl);
	echo $error_msg;

echo $response;



Note that you can remove the line CURLOPT_SSL_VERIFYPEER – I just had a local issue with SSL certs.

The output will look something like this;

  "Name": "",
  "Image": "",
  "Valid": true,
  "City": "",
  "Country": "",
  "IsDefault": true
Categories: Uncategorized
%d bloggers like this: