1

We have azure functions running on .net core 3.1 and we have methods on a seperate project running on .net framework 4.7.2 and we are trying to use the method in the .net framework 4.7.2 called from the azure function to access a database with encrypted columns using Always Encrypted. When trying to create new instance of Microsoft.Data.SqlClient.SqlConnection we are getting an exception :

System.TypeInitializationException: 'The type initializer for 'Microsoft.Data.SqlClient.SqlConnection' threw an exception.' "MissingMethodException: Method not found: 'System.Security.CodeAccessPermission System.Data.Common.DbProviderFactory.CreatePermission(System.Security.Permissions.PermissionState)'.

The connection string contains "Column Encryption Setting=enabled"

Example of Method in .net framework 4.7.2 ProjectA

public static string MethodA()
{
    string ssn = "";
    try
    {
        string sql = @" SELECT TOP 1 SSN FROM DBO.TESTAETABLE";
        List<SqlParameter> parameters = new List<SqlParameter>();
        using (Microsoft.Data.SqlClient.SqlDataReader reader = DBManager.ExecuteReaderMS(sql, parameters.ToArray()))
        {
            while (reader.Read())
            ssn = Convert.ToString(reader["SSN"]);
        }
    }
    catch (Exception e)
    {
        string error = e.ToString();
    }
    return ssn;
}

Example of Azure Function

[FunctionName("TestFunction")]
public static Task Run([ServiceBusTrigger("testfunction", Connection = "testconnection")]Message myQueueItem, ILogger log)
{
    try
    {
        string aType = Convert.ToString(myQueueItem.UserProperties["type"]);
        string aResourceName = Convert.ToString(myQueueItem.UserProperties["name"]);
        int aResult = 0;
        string aMessage = null;
        if (aType == "AKVTEST") 
        {
            string anSSN = ProjectA.ClassA.MethodA();
            return Task.CompletedTask;
        }

        log.LogInformation($"C# ServiceBus queue trigger function processed message: {myQueueItem.Body} for type: {aType}");
        return null;
    }
    catch (Exception Ex)
    {
        log.LogInformation($"C# ServiceBus queue trigger function EXCEPTION: {Ex.ToString()}");
        return null;
    }
}

Tried to change System.Data.SqlClient reference to Microsoft.Data.SqlClient (package version 5.1) and we received the exception mentioned above.

Before this change, while using System.Data.SqlClient, we were getting the following exception:

System.ArgumentException: 'Keyword not supported: 'column encryption setting'.'

Note: We have been using this set up in our production without the column encryption and have had no issues until trying to add this feature.

brandonwsl
  • 11
  • 1

0 Answers0