Search code examples
azurefunctionwcfservice-model

Error: 'Could not load file or assembly 'System.ServiceModel, Version=4.0.0.0' after deploying Function to Azure


I use an Azure v1 Function (C#, .NET Framework 4.6.1) to call an external WCF service and for making this work, I added a reference to the System.ServiceModel assembly. This works fine on my local dev machine, but after deploying the function to Azure, I get the following error:

Error: Unable to load one or more of the requested types. Could not load file or assembly 'System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. The system cannot find the file specified.

I tried adding the dll as a local copy/directly to live binary with app service editor, but this caused another error:

... (as above) Reference assemblies should not be loaded for execution. They can only be loaded in the Reflection-only loader context.

The same error occurs when I remove the assembly reference and install nuget package System.ServiceModel.Primitives instead.


Solution

  • A few things you should check / try:

    1-FUNCTIONS_EXTENSION_VERSION has the value "~2" by default. Make sure your has "~1". More info: https://learn.microsoft.com/en-us/azure/azure-functions/functions-app-settings#functions_worker_runtime

    1.1-If the above didn't work, you can consider working with Azure Function into a Container, then you would be able to specify / deploy the dependencies: https://learn.microsoft.com/en-us/azure/devops/pipelines/targets/function-app-container?view=azure-devops&tabs=yaml

    1.2-The above may be complex, so as a simplified version, you could keep Azure Function just as the triggering mechanism, and move all the business logic to an Azure Container Instance, where you have more control over the dependencies for your app.