Azure Fluent API 创建 SQL Server 时出错 - 缺少 x-ms-request-id 标头

Posted

技术标签:

【中文标题】Azure Fluent API 创建 SQL Server 时出错 - 缺少 x-ms-request-id 标头【英文标题】:Azure Fluent API Error creating SQL Server - Missing x-ms-request-id header 【发布时间】:2017-12-23 09:25:11 【问题描述】:

我正在尝试使用 Azure Fluent API (https://github.com/Azure/azure-sdk-for-net/tree/Fluent) 创建一个新的 SQL Server,但我总是收到 Microsoft.Rest.Azure.CloudException。其他一切(创建存储帐户、应用服务、资源组)都可以正常工作 - 只是 SQL 部分不起作用。

ISqlServer sqlServer = await Azure.SqlServers
                    .Define(serverName)
                    .WithRegion(regionName)
                    .WithExistingResourceGroup(rgName)
                    .WithAdministratorLogin(administratorLogin)
                    .WithAdministratorPassword(administratorPassword)
                    .WithNewElasticPool(elasticPoolName, elasticPoolEdition)
                    .CreateAsync();

但在尝试创建服务器时出现异常:

Microsoft.Rest.Azure.CloudException: Invalid value for header 'x-ms-request-id'. The header must contain a single valid GUID.
   at Microsoft.Azure.Management.Sql.Fluent.ServersOperations.<CreateOrUpdateWithHttpMessagesAsync>d__10.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Azure.Management.Sql.Fluent.ServersOperationsExtensions.<CreateOrUpdateAsync>d__11.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Azure.Management.Sql.Fluent.SqlServerImpl.<CreateResourceAsync>d__53.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Azure.Management.ResourceManager.Fluent.Core.ResourceActions.Creatable`4.<Microsoft-Azure-Management-ResourceManager-Fluent-Core-ResourceActions-IResourceCreator<IResourceT>-CreateResourceAsync>d__15.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Azure.Management.ResourceManager.Fluent.Core.DAG.CreatorTaskItem`1.<ExecuteAsync>d__6.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Azure.Management.ResourceManager.Fluent.Core.DAG.TaskGroupBase`1.<ExecuteNodeTaskAsync>d__14.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at XircuitAPI.Controllers.AzureSqlServerController.<Create>d__9.MoveNext() in C:\Users\ThimoBuchheister\Documents\Code\Xircuit\xircuit\XircuitAPI\Controllers\AzureSqlServerController.cs:line 235

【问题讨论】:

我通过@Siphamandla 的回答中提到的应用洞察中的配置更改解决了这个问题。如果这不能解决您的问题,请尝试检查请求中是否添加了 HTTP 标头。 【参考方案1】:

好的,我已经解决了我的问题,在使用 fiddle 跟踪我的 http 请求后,我发现 Application Insights 在我对 AAD 应用程序的请求中添加了标头。所以我完全删除了 Application Insights 并重新上线。希望它可以帮助你。看这个。如果您想继续使用 Application Insight,请查看此 disable application insight

【讨论】:

即使您没有在您的 asp.net 核心应用程序中使用应用程序洞察力,您也应该禁用它。【参考方案2】:

您所要做的就是排除此链接中写入的域或其他选项。

https://blog.wille-zone.de/post/disable-application-insights-correlation-id-headers-on-httpclient-requests-in-aspnet-core/

    var modules = app.ApplicationServices.GetServices<ITelemetryModule>();
    var dependencyModule = modules.OfType<DependencyTrackingTelemetryModule>().FirstOrDefault();

    if (dependencyModule != null)
    
        var domains = dependencyModule.ExcludeComponentCorrelationHttpHeadersOnDomains;
        domains.Add("management.azure.com");
    

【讨论】:

使用此解决方案将 Application Insights 和 DependencyTracking 保留在我的应用程序中。效果很好。我想知道为什么这个域默认不包含在配置文件中,但可能有一个很好的理由:)。【参考方案3】:

对于那些不在 .NET Core 上但在 Web API 或 ASP.NET MVC 5 上的用户,请使用如下模块:

var dependencyModule = Microsoft.ApplicationInsights.Extensibility.Implementation.TelemetryModules.Instance.Modules
    .OfType<Microsoft.ApplicationInsights.DependencyCollector.DependencyTrackingTelemetryModule>()
    .FirstOrDefault();

if (dependencyModule != null)

    var domains = dependencyModule.ExcludeComponentCorrelationHttpHeadersOnDomains;
    domains.Add("management.azure.com");

【讨论】:

【参考方案4】:

我无法重现您在Microsoft.Azure.Management.Sql.Fluent SDK 版本 1.1.3 中提到的问题。以下是我的详细步骤:

准备

注册一个AD应用程序并将应用程序分配给相应的角色,更多详细信息请参考Azure official tutorials。之后我们就可以从 Azure Portal 获取 tenantIdappIdsecret key

步骤:

1.创建一个.net core C#控制台项目。

2.参考Microsoft.Azure.Management.Sql.Fluent SDK,更多细节请参考以下截图

3.将以下代码添加到 Program.cs 文件中。

string clientId = "xxxxxxx";
string secretKey = "xxxxxxx";
string tenantId = "xxxxxxx";
var credentials = new AzureCredentials(new ServicePrincipalLoginInformation  ClientId = clientId, ClientSecret = secretKey , tenantId, AzureEnvironment.AzureGlobalCloud);
var azure = Azure
            .Configure()
            .Authenticate(credentials)
            .WithDefaultSubscription();
var serverName = "tomtestsqlazure";//test sql name
var regionName = Region.AsiaEast.ToString(); //region name
var administratorLogin = "tom";
var administratorPassword = "xxxxxxx";
var rgName = "xxxxx"; //resource group name
var elasticPoolName = "testelastic"; 
var elasticPoolEdition = "standard";
ISqlServer sqlServer =  azure.SqlServers
.Define(serverName)
.WithRegion(regionName)
.WithExistingResourceGroup(rgName)
.WithAdministratorLogin(administratorLogin)
.WithAdministratorPassword(administratorPassword)
.WithNewElasticPool(elasticPoolName, elasticPoolEdition)
.CreateAsync().Result;

4.从本地调试并用fiddler捕获请求。

5.从 Azure 门户查看。

【讨论】:

以上是关于Azure Fluent API 创建 SQL Server 时出错 - 缺少 x-ms-request-id 标头的主要内容,如果未能解决你的问题,请参考以下文章

如何在 EF Core 中通过 Fluent Api 创建加密列

Azure.Fluent.Management 和 Azure.Storage.Blobs 包之间的冲突?

如何使用 fluent api 命名地图关联

如何将 AzureDefaultCredentials 与 Azure Fluent 结合使用?

EF Core Fluent API

使用 Entity Framework 6.1 fluent API 创建唯一索引