使用EF Core在Azure功能上的Application Insights中启用Sql依赖关系 [英] Enable Sql Dependency in Application Insights on Azure Functions with EF Core

查看:69
本文介绍了使用EF Core在Azure功能上的Application Insights中启用Sql依赖关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 Microsoft.EntityFrameworkCore 3.1.5 的Azure Function v3应用程序.我无法启用SQL依赖关系跟踪.已经挣扎了1天.

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.

为解决此问题,我创建了一个没有EF的独立AzureFunction.我使用的是 Microsoft.Data.SqlClient 1.0.19269.1 ,而不是EF.函数如下:

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);
        }
    }

应用参考

    <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" />

将应用程序部署到Azure(无EF)之后,将按预期跟踪SQL依赖项.

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

将引用 Microsoft.EntityFrameworkCore 添加到项目后,没有其他更改,跟踪将停止工作.删除参考跟踪后,它又可以工作了.将引用更新为最新版本也无济于事.

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?

推荐答案

似乎在 Microsoft.EntityFrameworkCore 3.1.4 中,他们引入了导致此问题的问题.降级到3.1.3即可解决.

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

更新:在github上打开错误后,他们指出了我的一个问题与此相关的Microsoft.Data.SqlClient和EventSource.

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.

这篇关于使用EF Core在Azure功能上的Application Insights中启用Sql依赖关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆