Enable Sql Dependency in Application Insights on Azure Functions with EF Core

此生再无相见时 提交于 2021-02-08 06:25:15

问题


I have an Azure Function v3 application which uses Microsoft.EntityFrameworkCore 3.1.5. I am not able to enable SQL Dependency tracking. Struggling with it 1 day already.

To isolate the issue, I have created a standalone AzureFunction without EF. Instead of EF I was using Microsoft.Data.SqlClient 1.0.19269.1 which is also used by EF. Here goes the function:

 public static class Function1
    {
        [FunctionName("Function1")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            var ids = "";
            using (var connection = new SqlConnection("connectionstring"))
            {
                var command = connection.CreateCommand();
                command.CommandText = "SELECT TOP 10 Id FROM table";
                await connection.OpenAsync();
                using (var reader = await command.ExecuteReaderAsync())
                {
                    while (reader.Read())
                    {
                        ids += reader.GetInt32(0).ToString() + ",";
                    }

                    ids = ids.Substring(0, ids.Length - 1);
                }
            }
            return new OkObjectResult("Ids:" + ids);
        }
    }

References for app

    <PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.0.0" />
    <PackageReference Include="Microsoft.Data.SqlClient" Version="1.0.19269.1" />
    <PackageReference Include="Microsoft.Extensions.Logging" Version="3.1.5" />
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.0.3" />

After deploying the application to Azure(without EF), SQL dependencies are tracked as expected.

After adding a reference Microsoft.EntityFrameworkCore to the project and nothing else is changed, the tracking stops working. After removing the reference tracking is working again. Updating the references to the latest version does not help either.

Do you have any idea why is this happening? Any suggestion how to solve this?


回答1:


It seems in Microsoft.EntityFrameworkCore 3.1.4 they introduced an issue which causes this behavior. Downgrading to 3.1.3 solves it.

UPDATE: After I have opened a bug on github, they pointed me towards an issue with Microsoft.Data.SqlClient and EventSource which are related to this one.



来源:https://stackoverflow.com/questions/63053334/enable-sql-dependency-in-application-insights-on-azure-functions-with-ef-core

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!