Azure Fluent API Error creating SQL Server - Missi

2019-02-22 02:07发布

问题:

I'm trying to create a new SQL Server with the Azure Fluent API (https://github.com/Azure/azure-sdk-for-net/tree/Fluent) but I always get a Microsoft.Rest.Azure.CloudException. Everything else (Creating Storage Account, App Services, Resource Groups) works fine - it's just the SQL part which does not work.

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

But when trying to create the server I got an exception:

{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}

回答1:

Okay i have solved my problem, after using fiddle to trace my http request i discovered that, Application Insights added headers to my request to the AAD app. So i total removed Application Insights and im back online. Hope it helps you. Look at this. if you want to continue using Application Insight check this out disable application insight



回答2:

All you had to do was to exclude a domain or other options that are written in this link.

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


回答3:

For those not on .NET Core but on Web API or ASP.NET MVC 5, reach for the modules like this:

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:

I can't repro the issue that you mentioned with Microsoft.Azure.Management.Sql.Fluent SDK version 1.1.3. The following is my detail steps:

preparation

Registry an AD application and assgin application to corresponding role, more details please refer to Azure official tutorials. After that we can get tenantId, appId, secret key from the Azure Portal.

Steps:

1.Create a .net core C# console project.

2.Reference the Microsoft.Azure.Management.Sql.Fluent SDK, more details please refer to the following screenshot

3.Add the following code to the Program.cs file.

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.Debug from local and catch the request with fiddler.

5.Check it from the Azure portal.