Azure / Azure/azure-functions-host
Apply FunctionAssemblyLoadContext fixes to DynamicAssemblyLoadContext
- Dominant language
- C#
- Stars
- 2k
- Forks
- 482
- Avg merge
- 2d 12h
- Merged PRs (30d)
- 38
Description
We've recently made several bug fixes to the `FunctionAssemblyLoadContext`, but it seems that they may also need to be ported to `DynamicAssemblyLoadContext` to handle scenarios for csx projects.
The recent fixes were:
- #6566
- #6632
- #6639
Copying the details from an internal thread here:
---
1. The repro .csx code is as follows:
```csharp
#r "Newtonsoft.Json"
using Microsoft.Data.SqlClient;
using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
using System;
using System.Globalization;
public static async Task Run(HttpRequest req, ILogger log)
{
string str = "Data Source=(local);Initial Catalog=Northwind;" + "Integrated Security=SSPI";
string qs = "SELECT OrderID, CustomerID FROM dbo.Orders;";
log.LogInformation("C# HTTP trigger function processed a request.");
using (SqlConnection connection = new SqlConnection(str))
{
Microsoft.Data.SqlClient.SqlCommand commandx = new Microsoft.Data.SqlClient.SqlCommand(qs, connection);
SqlCommand command = new SqlCommand(qs, connection);
command.Connection.Open();
command.ExecuteNonQuery();
}
try
{
SqlConnection conn = null;
log.LogInformation($"C# ");
}
catch (Exception testException)
{
log.LogInformation("Exception:" + testException.ToString());
}
return new OkResult();
}
```
2. The repro function.proj is as follows:
```xml
netstandard2.0
```
3. I would get the below exception from output log:
```
Id=943a0d70-25d8-4e94-bc9f-eb2a8aae0723)
2020-11-13T07:07:11.489 [Information] C# HTTP trigger function processed a request.
2020-11-13T07:07:12.590 [Error] Executed 'Functions.HttpTrigger1' (Failed, Id=943a0d70-25d8-4e94-bc9f-eb2a8aae0723, Duration=5341ms)Microsoft.Data.SqlClient is not supported on this platform.
```
4. The loaded SQL Client module is from the location “D:\home\data\Functions\packages\nuget\Microsoft.Data.SqlClient\2.0.1\lib\netstandard2.0\Microsoft.Data.SqlClient.dll”:
```
D:\home\data\Functions\packages\nuget\Microsoft.Data.SqlClient\2.0.1\lib\netstandard2.0\Microsoft.Data.SqlClient.dll
Size is: 360448
2.0.20168.4
```

5. Suppose I copy the SQL related assembly generated by VS Code and uploaded to function site bin folder, it works as expected:


6. I compared the two Microsoft.Data.SqlClient.dll and found the below reference:
```
Working Scenario:
==================
[assembly: AssemblyFileVersion("4.700.20.41903")]
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyDefaultAlias("System.Runtime")]
[assembly: AssemblyMetadata(".NETFrameworkAssembly", "")]
[assembly: AssemblyMetadata("Serviceable", "True")]
[assembly: AssemblyMetadata("PreferInbox", "True")]
[assembly: AssemblyCopyright("© Microsoft Corporation. All rights reserved.")]
[assembly: AssemblyDescription("System.Runtime")]
[assembly: AssemblyInformationalVersion("3.1.8+d6302a72f8eafa326d7572a02acf8f3021ebb9e8")]
[assembly: AssemblyProduct("Microsoft® .NET Core")]
[assembly: AssemblyTitle("System.Runtime")]
[assembly: AssemblyCompany("Microsoft Corporation")]
[assembly: CLSCompliant(true)]
[assembly: AssemblyVersion("4.2.2.0")]
```
```
No Working Scenario:
==================
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(/*Could not decode attribute arguments.*/)]
[assembly: CLSCompliant(true)]
[assembly: TargetFramework(".NETStandard,Version=v2.0", FrameworkDisplayName = "")]
[assembly: AssemblyCompany("Microsoft Corporation")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyCopyright("© Microsoft Corporation. All rights reserved.")]
[assembly: AssemblyDescription("Microsoft.Data.SqlClient")]
[assembly: AssemblyFileVersion("2.0.20168.4")]
[assembly: AssemblyInformationalVersion("2.0.1")]
[assembly: AssemblyProduct("Core Microsoft SqlClient Data Provider")]
[assembly: AssemblyTitle("Microsoft.Data.SqlClient")]
[assembly: SecurityPermission(8, SkipVerification = true)]
[assembly: AssemblyVersion("2.0.20168.4")]
```
7. It seems the working version SqlClient dll is for .NetCore3.1 but suppose I changed it in function.proj as follows I even cannot get it complied successfully:
```xml
netcoreapp3.1
```
```
[Error] run.csx(8,17): error CS0234: The type or namespace name 'Data' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?)
```
Contributor guide
Assessment
This issue has not been assessed yet.