'Azure function error : BinaryFormatter serialization and deserialization are disabled within this application

i have existing azure function in .netCore 3.1, Trying to upgrade it to .net6.0. after upgrade getting error 'BinaryFormatter serialization and deserialization are disabled within this application. See https://aka.ms/binaryformatter for more information.'. after below<EnableUnsafeBinaryFormatterSerialization>true</EnableUnsafeBinaryFormatterSerialization> solution it works fine on local environment but once deployed on Azure function it gives same error again. Can someone please help?

its very had to go through all code it has lot of custom nuget packages and tone of code in each package.



Solution 1:[1]

csproj is not part of the deployed package. in azure function use

AppContext.SetSwitch("System.Runtime.Serialization.EnableUnsafeBinaryFormatterSerialization", true);

Solution 2:[2]

Here is the workaround I did by migrating my Azure Function App from 3 to 6 and the function has some NuGet Packages installed and a bit of Serialization Code.

  • Deployed the .Net 3.1 Function App to Azure Portal from Visual Studio, which is running successfully.

enter image description here

  • To migrate the function app from 3.1 to 6, change the .csproj file code related to TargetFramework and AzureFunctionsVersion.

enter image description here

After that, the IDE asks to reload the project and its dependencies, click yes.

enter image description here

- Published the Migrated Function App to the Same Function App in the Azure Portal from Visual Studio, which is running successfully:

enter image description here

Note:

If any external libraries/extensions/packages are missed when publishing to the Azure Portal after the functions version migration, we need to import the required packages manually in the function app of the portal, here is a live example, please visit this thread.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Bharat
Solution 2 HariKrishnaRajoli-MT