Azure / Azure/azure-functions-host
"Could not load file or assembly 'System.ValueTuple, Version=0.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51’ or one of its dependencies" when net461/net462/net47 function references netstandard2.0 project.
- Dominant language
- C#
- Stars
- 2k
- Forks
- 482
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 36
Description
#### Investigative information
Trying to create instance of class which uses System.ValueTuple from netstandard2.0 project which is referenced by net461/net462/net47 function project results in runtime exception:
> Could not load file or assembly 'System.ValueTuple, Version=0.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51’ or one of its dependencies.
#### Repro steps
1. Create new Azure Function project.
2. Create generic webhook function.
3. Create new netstandard2.0 class library with class with constructor accepting value tuple parameter.
4. Reference netstandard2.0 class library in function project and instantiate class created in previous step.
5. Publish function to Azure Function App.
6. Try to execute function.
#### Expected behavior
Function compiles and works without errors.
#### Actual behavior
Function compiles without errors but throws FileNotFoundException at runtime.
> Could not load file or assembly 'System.ValueTuple, Version=0.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51’ or one of its dependencies.
#### Known workarounds
* #### net461/net462
1. Add reference to System.ValueTuple 4.4.0 from nuget to function project.
2. Manually redirect System.ValueTuple assembly to 4.0.2.0 at runtime.
Redirector class
```csharp
public static class AssemblyRedirector
{
public static void RedirectAssembly(string shortName, string publicKeyToken, string redirectToVersion)
{
ResolveEventHandler handler = null;
handler = (sender, args) =>
{
var requestedAssembly = new AssemblyName(args.Name);
if (requestedAssembly.Name != shortName)
{
return null;
}
var targetPublicKeyToken = new AssemblyName("x, PublicKeyToken=" + publicKeyToken).GetPublicKeyToken();
requestedAssembly.SetPublicKeyToken(targetPublicKeyToken);
requestedAssembly.Version = new Version(redirectToVersion);
requestedAssembly.CultureInfo = CultureInfo.InvariantCulture;
AppDomain.CurrentDomain.AssemblyResolve -= handler;
return Assembly.Load(requestedAssembly);
};
AppDomain.CurrentDomain.AssemblyResolve += handler;
}
}
```
Function code
```csharp
public static class Function
{
static Function()
{
AssemblyRedirector.RedirectAssembly("System.ValueTuple", "cc7b13ffcd2ddd51", "4.0.2.0");
}
[FunctionName("Function")]
public static Task Run([HttpTrigger(WebHookType = "genericJson")]HttpRequestMessage req, TraceWriter log)
{
...
}
}
```
* #### net47
1. Redirect assembly as shown above (no need to reference System.ValueTuple from nuget).
#### Related information
* Azure Function App .NET version - 4.7
* Same error occurs when using Azure Function CLI (1.0.0/1.0.4) for local development & debugging
* Microsoft.NET.Sdk.Functions version - 1.0.0/1.0.2
* Sample code for fast reproducing - https://github.com/kamilzzz/AzureFunction.ValueTuple
Contributor guide
Research direction
Start with the linked sample repository and reproduce the failure using the stated net461/net462/net47 function and netstandard2.0 class-library setup. Compare local Azure Functions CLI behavior with the published Function App, and use the reported System.ValueTuple FileNotFoundException as the completion criterion: the function should execute without the runtime assembly error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- azure, csharp
- Domain
- backend, cloud
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100