Azure function failing with “Object reference not set to an instance of an object”


I have an Azure application that uses an Office 365 App only token. Followed similar instructions to the one here: https://docs.microsoft.com/en-us/sharepoint/dev/solution-guidance/security-apponly-azuread

I’m using OfficeDevPnP to connect to SharePoint with my Office 365 App only token. Demo code shown below.


string clientId = GetApplicationSetting("clientId");
string tenant = GetApplicationSetting("tenant");
string certificateFilename = GetApplicationSetting("certificateFilename");
string certificatePath = GetCertificatePath(exCtx.FunctionAppDirectory, certificateFilename);

SecureString certificatePassword = ConvertToSecureString(GetApplicationSetting("certificatePassword"));

string tenantAdminUrl = GetApplicationSetting("tenantAdminUrl");
log.Info("Got details");
OfficeDevPnP.Core.AuthenticationManager authManager = new OfficeDevPnP.Core.AuthenticationManager();

try{
   using (var clientContext = authManager.GetAzureADAppOnlyAuthenticatedContext(siteProperties.Url, clientId, tenant, certificatePath, certificatePassword))
   {
     Web web = clientContext.Web;
     clientContext.Load(web, w => w.Webs, w => w.Title, w => w.Url);
     clientContext.ExecuteQueryRetry();
   }
}catch(Exception ex){
log.Info("Failed! " + ex.Message);
}

The Azure function that has been deployed and running daily for a while now, when all of a sudden it stopped working last Wednesday. Just as it was authenticating with SharePoint it displayed the error message “Object reference not set to an instance of an object”

The stack trace is below:

2018-02-06T09:45:28.347 Failed! Object reference not set to an instance of an object.
2018-02-06T09:45:28.364 StackTrace:    at Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext.RunAsyncTask[T](Task`1 task)
   at Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext.AcquireToken(String resource, ClientAssertionCertificate clientCertificate)
   at OfficeDevPnP.Core.AuthenticationManager.<>c__DisplayClass36_0.<GetAzureADAppOnlyAuthenticatedContext>b__0(Object sender, WebRequestEventArgs args)
   at Microsoft.SharePoint.Client.ClientRuntimeContext.OnExecutingWebRequest(WebRequestEventArgs args)
   at Microsoft.SharePoint.Client.ClientContext.GetFormDigestInfoPrivate()
   at Microsoft.SharePoint.Client.ClientContext.EnsureFormDigest()
   at Microsoft.SharePoint.Client.ClientContext.ExecuteQuery()
   at AppFunctionDemo.Function2.GetTenantSiteProperties(AuthenticationManager authManager, String tenantAdminUrl, String clientId, String tenant, String certificatePath, SecureString certificatePassword, TraceWriter log)
   at AppFunctionDemo.Function2.Run(TimerInfo myTimer, ExecutionContext exCtx, TraceWriter log)

After a call with Microsoft, it turns out that the Azure Functions are slowly being updated throughout the tenants.

Last one was 11 days ago (at time of writing this) https://github.com/Azure/azure-functions-host/releases

If you go to your Azure functions and look in the Application Settings, you will see an application setting saying:


FUNCTIONS_EXTENSION_VERSION: ~1

This means it will keep itself up to the latest date. By changing the version “~1” to a previous version “1.0.11490” after saving the Application Settings, and re-running my Azure function, I no longer get an error message saying “Object Reference not set to an instance of an object” and the function runs to completion.

There is obviously a bigger bug issue here, but at least my Azure functions can continue running until then.