powershell: [BUG] PnP and Azure functions assembly conflict. Could not load type 'Microsoft.Extensions.Logging.Abstractions.Internal.NullScope' from assembly 'Microsoft.Extensions.Logging.Abstractions'

Reporting an Issue or Missing Feature

This is an issue related to PnP using a conflicting assembly dependency than what the Azure functions service loads by default. To be more specific, the issue occurs for us with the module Microsoft.Extensions.Logging.Abstractions on each call of Connect-PnPOnline. PowerShell 7.2 on Azure functions automatically loads version 3.0.3.0 while the one included with PnP is 2.2.0.0. I have tried many potential resolutions but have been unable to fix (Wrapping in ThreadJob does not work). The one solution about downgrading from 7.2 to 7.0 is not an option for us, as we require 7.2 for its functionalities. Due to this issue, we are unable to rely on PnP for our needs at the moment. If a fix for PnP in this scenario is possible, please do. If not, then notify of any possible workarounds. This problem had me stumped for a while, so anything that can be done about it is appreciated.

Expected behavior

Successful connection via Connect-PnPOnline cmdlet.

Actual behavior

An error related to a module in Azure functions: Screenshot 2022-07-05 094943

Steps to reproduce behavior

  • Create an Azure functions app version 4.0 and set it to use PowerShell 7.2
  • Add PnP.PowerShell 1.10.0 to the requirements.psd1 file.
  • Create a basic function that just tries to execute Connect-PnPOnline. Ensure it logs error info.
  • Deploy the function to the cloud and execute via your API. (<app_url>/api/<func_name>)
  • Wait a bit for logs to save, then go to monitor log menu for that function.
  • View its logs and observe error.

NOTE: To reproduce, ensure code is run in the cloud within an Azure function. Running Connect-PnPOnline locally will likely NOT cause an error. (The Azure assemblies won’t be loaded)

What is the version of the Cmdlet module you are running?

PnP.PowerShell 1.10.0, PowerShell Core 7.2

Which operating system/environment are you running PnP PowerShell on?

  • Linux
  • MacOS
  • Windows
  • Azure Cloud Shell
  • Azure Functions
  • Other : please specify

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 8
  • Comments: 89 (4 by maintainers)

Commits related to this issue

Most upvoted comments

Hello all on this thread, we are looking into fixing this for Azure functions. Is more complex than we initially thought. It is surely at the top of our minds and a priority issue for us. Will try to fix it asap but can’t provide an ETA at this point.

@Francisco-Gamino can you comment on the rollout of the updated Azure Functions in January and the situation between now and then with PowerShell 7.0 Azure Functions being deprecated?

The PowerShell 7.2 language worker with the fix will start rolling out January 3rd, and it is expected to complete in one or two weeks. In the meantime, we are committing to provide support for the apps that are on PowerShell 7.0 until this deployment completes.

/cc @anirudhgarg @AnatoliB @MadhuraBharadwaj-MSFT @michaelpeng36 @VpOfEngineering

To downgrade your function app from PowerShell 7.2 to PowerShell 7.0, follow these steps:

Prerequisites: Az.Functions and Az.Resources, which can be downloaded from the PowerShell Gallery.

$SubId = "<your sub id>"
$ResourceGroupName = "<your resource group name>"
$AppName = "your app name"

Set-AzContext -Subscription $Subscription | Out-Null

For a Windows function app

Set-AzResource -ResourceId "/subscriptions/$SubId/resourceGroups/$ResourceGroupName/providers/Microsoft.Web/sites/$AppName/config/web" -UsePatchSemantics -Properties @{ powerShellVersion = '~7' } -Force

# Restart your app for the version changes to take effect
Restart-AzFunctionApp -Name $AppName -ResourceGroupName $ResourceGroupName -Force

# Verify that version was updated
$app = Get-AzFunctionApp -Name $AppName -ResourceGroupName $ResourceGroupName 
$app.SiteConfig.PowerShellVersion

For a Linux function app

Set-AzResource -ResourceId "/subscriptions/$SubId/resourceGroups/$ResourceGroupName/providers/Microsoft.Web/sites/$AppName/config/web" -UsePatchSemantics -Properties @{ linuxFxVersion = 'PowerShell|7' } -Force

# Restart your app for the version changes to take effect
Restart-AzFunctionApp -Name $AppName -ResourceGroupName $ResourceGroupName -Force

# Verify that version was updated
$app = Get-AzFunctionApp -Name $AppName -ResourceGroupName $ResourceGroupName 
$app.SiteConfig.LinuxFxVersion

If you have any questions on why PowerShell 7.0 is "~7", please see https://github.com/Azure/azure-functions-powershell-worker/issues/885

@Francisco-Gamino can you comment on the rollout of the updated Azure Functions in January and the situation between now and then with PowerShell 7.0 Azure Functions being deprecated?

The PowerShell 7.2 language worker with the fix will start rolling out January 3rd, and it is expected to complete in one or two weeks. In the meantime, we are committing to provide support for the apps that are on PowerShell 7.0 until this deployment completes.

/cc @anirudhgarg @anatolib @MadhuraBharadwaj-MSFT @michaelpeng36 @VpOfEngineering

Barring any issues, we should be fully rolled out by the beginning of next week.

Just tested this in an environment which has version 4.15.x.x , works perfectly fine !

To fix this issue in your environment, please wait till the fix is there. I am guessing it will be available very soon worldwide.

I tested this in a resource group which is in North Europe region.

Once the fix is available, you will need to update the PnP PowerShell to the latest nightly builds.

You need to explicitly specify the version number like 2.0.1-nightly or later. The code will keep working as it is.

Once a major release of PnP PowerShell is done, you can also specify it as a wildcard entry 2.x

@KoenZomers if the solution proposed by @Micael-stack is suitable while you work with the PG and towards .NET 6 that would make a lot of us very happy!

Hi, I have created a pull request. I did a fork too, if it isnt approved so here it is :

https://github.com/Micael-stack/powershell/tree/fix/support-powershell7.2-azure-function

Instructions to get your own files :

  1. Clone the git branch mentionned here

  2. Run the powershell script in “.\Build\Build-Debug.ps1” . .\Build\Build-Debug.ps1

  3. At the end of the script, there should be a module folder in your Documents/Powershell/Modules/Pnp.Powershell

  4. Zip it, and upload it to your azure function, if not in git push setup ( I use the powershell tool in azure portal and upload the zip here : image

  5. Reference the powershell module in your code like this : Import-Module .\Pnp.PowerShell\PnP.PowerShell.psd1

That’s it, you should be running with no problems!

  • You cannot use anymore PNP.Powershell managed dependencies like this. Remove it if you do from requirements.psd1

Thanks!

So I fixed it… by rebuilding Pnp.Powershell and Pnp.Framework. Updated refs to all Microsoft.Extensions.* from 2.2.0 to 3.0.3.0

PnP.Framework.csproj: image

PnP.PowerShell.csproj: image

I rebuilded a local module and uploaded it to my Azure function, so no more managed dependencies… referencing directly the module in scripts.

It would be good to know when the next major release of PnP will be out that works with the Azure fix. Some clients don’t like to use nightly versions in production 😃 thanks!

The Functions release with the fix for the PowerShell 7.2 language worker has been delayed. Currently, the release is set to start deploying on January 17th, 2023 and it is expected to be completed in one or two weeks if no blocking issues are found. I will continue to update this issue with the deployment status as more information becomes available. Thank you.

The Functions version 14.15.1 deployment that was supposed to start on 1/17/23 has been delayed. However, the deployment started rolling out today 1/23/23, and it is expected to be completed by the beginning of next week if no blocking issues are found.

/cc @anirudhgarg @AnatoliB @MadhuraBharadwaj-MSFT @michaelpeng36 @VpOfEngineering

Going to close this issue. It has been fixed from Microsoft side when using Az function 4.15.x or later version. To use PnP PowerShell, as we mentioned, please use the latest 2.x.x-nightly builds.

@desmay - no idea if this has been fixed for Azure Gov, but you can reach out to Microsoft support/PG for more information on this.

We have no ETA on the major release of PnP PowerShell as of now.

If you are not running a consumption plan, but under an app service plan, I ran into a weird issue and found a workaround. I was on a B2 sku and kept getting the nullscope error. I was not downgrading my ps version due to other production functions running on the same app plan. Maybe Azure gov does not back patch existing hosts or not fully finished yet. To make it work, upgrade your plan to a PV2/V3 and it will move it to a new host which should have the patched runtime. I verified it worked, then moved it back to a B2 plan which was put on a host that was patched as well.

The PowerShell 7.2 language worker which is being deployed with 4.15.1 updates Microsoft.Extensions.Logging.Abstractions to version 6.0.3. If the workaround was applied to the PowerShell 7.0 language worker, then this should continue to work as no changes has been introduced to this version given that it has reached EOL.

@KoenZomers – Could you please comment on next steps after 4.15.1 completes rolling out? Thank you.

How were you able to delete the “FUNCTIONS_WORKER_RUNTIME_VERSION”? I only see it as a dropdown with 7.0 and 7.2 options. Is there a file I can edit anywhere?

@Ofer-Gal What are you trying to achieve? To role back to version 7.0 you can use a PowerShell script.

# Configure App to use .Net Core 7.0
$SubId = "<SubScriptionID>"
$ResourceGroupName = "RGXXX"
$AppName = "func-AppName"
$TenantID = "TenantID"

Connect-AzAccount -Tenant $TenantID -SubscriptionId $SubId

Set-AzContext -Subscription $SubId | Out-Null

Set-AzResource -ResourceId "/subscriptions/$SubId/resourceGroups/$ResourceGroupName/providers/Microsoft.Web/sites/$AppName/config/web" -UsePatchSemantics -Properties @{ powerShellVersion = '~7' } -Force

# Restart your app for the version changes to take effect
Restart-AzFunctionApp -Name $AppName -ResourceGroupName $ResourceGroupName -Force

# Verify that version was updated
$app = Get-AzFunctionApp -Name $AppName -ResourceGroupName $ResourceGroupName 
$app.SiteConfig.PowerShellVersion

Or you can see the FUNCTIONS_WORKER_RUNTIME_VERSION locally in VSCode in the local.settings.json file. image

@Francisco-Gamino can you comment on the rollout of the updated Azure Functions in January and the situation between now and then with PowerShell 7.0 Azure Functions being deprecated?

The PowerShell 7.2 language worker with the fix will start rolling out January 3rd, and it is expected to complete in one or two weeks. In the meantime, we are committing to provide support for the apps that are on PowerShell 7.0 until this deployment completes.

/cc @anirudhgarg @AnatoliB @MadhuraBharadwaj-MSFT @michaelpeng36 @VpOfEngineering

The Functions release with the fix for the PowerShell 7.2 language worker has been delayed. Currently, the release is set to start deploying on January 17th, 2023 and it is expected to be completed in one or two weeks if no blocking issues are found. I will continue to update this issue with the deployment status as more information becomes available. Thank you.

No worries @BeekvanV, we’re working closely with the Product Group for a solution on this. We’re currently testing a resolution for this internally. Thus far things are looking well. It will not be that your current Azure Functions will stop working on December 3rd. The permanent resolution for this will follow in January or February as things look like now.

I see. Thanks for elaborating. For the current version that might work, but we’re aiming to go for .NET 6 support in the next version and there this would not be an option. I’ll discuss it in our team if we could apply your suggestion in the meantime.

@gautamdsheth , do you have any idea if the pnp.powershell is adapted on this issue or when it will happen? Thanks for your feedback!