C# Azure Management Libraries Sdk for.NET list()函数不适用于各种接口

C# Azure Management Libraries Sdk for.NET list()函数不适用于各种接口,c#,asp.net,database,asynchronous,azure-sdk-.net,C#,Asp.net,Database,Asynchronous,Azure Sdk .net,我正在使用Azure管理库(特别是fluent)创建针对其api的web请求,以获取订阅下的数据库列表。我可以使用fluent获取sqlserver的实例,但无法获取特定服务器下所有数据库的列表。 定义和删除很好,这只是list()函数 我曾尝试将其用于sqlserver.firewallrules,但列表函数在那里也不起作用 下面是一些代码: 日志在某个点暂停,然后写入“已退出,代码为0” 公共异步任务getSqlDatabaseList() { System.Diagnostics.Debu

我正在使用Azure管理库(特别是fluent)创建针对其api的web请求,以获取订阅下的数据库列表。我可以使用fluent获取sqlserver的实例,但无法获取特定服务器下所有数据库的列表。 定义和删除很好,这只是list()函数

我曾尝试将其用于sqlserver.firewallrules,但列表函数在那里也不起作用

下面是一些代码: 日志在某个点暂停,然后写入“已退出,代码为0”

公共异步任务getSqlDatabaseList() { System.Diagnostics.Debug.WriteLine(“开始获取数据库列表”); List dbNameList=新列表(); //var azure在项目的前面定义,并经过身份验证。 var sqlServer=await azure.SqlServers.GetByResourceGroupAsync(“,”); //下面的代码成功写入服务器名称 System.Diagnostics.Debug.WriteLine(sqlServer.Name); //下面的代码是每次停止和“已退出代码0”在延迟几秒钟后发生的代码 var dbList=sqlServer.Databases.List(); //永远也到不了这条线 System.Diagnostics.Debug.WriteLine(“此行从未写入”); foreach(数据库列表中的ISQLDB数据库) { 添加(db.Name); } 返回dbNameList; } 澄清: 我正在使用ASP.NETMVC 下面是我的控制器方法访问类方法的方式。Resource Manager是实现getSQlDatabaseList()的类的名称

//GET:Home
公共异步任务索引()
{
ResourceManager rm=新的ResourceManager();
List test=wait rm.getSqlDatabaseList();
//从不访问这行代码,也从不调用for each或之后的任何内容
foreach(测试中的var项目)
{
系统.诊断.调试.写入线(项目);
}
System.Diagnostics.Debug.WriteLine(“每个都是过去的”);
//AzureManager azm=等待AzureManager.createAzureManager();
//等待azm.getResourceGroupList();
返回视图(新的UserLogin());
}

根据您的代码和描述,我猜您的代码无法创建表的原因与您的异步getSqlDatabaseList有关

我猜您可以在控制台中调用这个方法main方法或其他方法

如果主方法已完全执行,则异步方法getSqlDatabaseList不会完全执行并返回字符串列表。它将结束所有异步方法

我建议您可以在调用getSqlDatabaseList方法时添加wait或result key关键字,以等待线程完全执行该方法

更多细节,您可以参考下面的测试演示

  static void Main(string[] args)
        {
            //use result to wait the mehtod executed completely
            List<String> test =  getSqlDatabaseList().Result;

            foreach (var item in test)
            {
                Console.WriteLine(item);
            }

            Console.Read();

        }

        public static async Task<List<String>> getSqlDatabaseList()
        {
            //System.Diagnostics.Debug.WriteLine("Starting to get database list");
            List<string> dbNameList = new List<string>();

            var credentials = SdkContext.AzureCredentialsFactory.FromFile(@"D:\Auth.txt");

            var azure = Azure
                .Configure()
                .WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
                .Authenticate(credentials)
                .WithDefaultSubscription();

            var sqlServer = await azure.SqlServers.GetByResourceGroupAsync("groupname", "brandotest");


            var dbList = sqlServer.Databases.List();

            foreach (ISqlDatabase db in dbList)
            {
                dbNameList.Add(db.Name);
            }
            return dbNameList;
        }
static void Main(字符串[]args)
{
//使用结果等待方法完全执行
List test=getSqlDatabaseList().Result;
foreach(测试中的var项目)
{
控制台写入线(项目);
}
Console.Read();
}
公共静态异步任务getSqlDatabaseList()
{
//System.Diagnostics.Debug.WriteLine(“开始获取数据库列表”);
List dbNameList=新列表();
var credentials=SdkContext.AzureCredentialsFactory.FromFile(@“D:\Auth.txt”);
var azure=azure
.Configure()
.WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
.验证(凭据)
.WithDefaultSubscription();
var sqlServer=await azure.SqlServers.GetByResourceGroupAsync(“groupname”、“brandotest”);
var dbList=sqlServer.Databases.List();
foreach(数据库列表中的ISQLDB数据库)
{
添加(db.Name);
}
返回dbNameList;
}

更新:

根据您的描述,我创建了一个测试MVC应用程序。正如你所说,我已经复制了你的问题

我认为azure management fluent SDK有问题

这里有一个解决方法,我建议您可以直接发送RESTAPI来获取数据库

更多详细信息,请参考以下代码:

将请求发送到以下url:

{subscriptionsid}/resourceGroups/{resourceGroupsname}/providers/Microsoft.Sql/servers/{servername}/databases?api版本={apiversion}

publicstaticlist getSqlDatabaseList()
{
//System.Diagnostics.Debug.WriteLine(“开始获取数据库列表”);
List dbNameList=新列表();
字符串tenantId=“yourtentantid”;
字符串clientId=“yourclientId”;
字符串clientSecret=“clientSecret”;
字符串subscriptionid=“subscriptionid”;
字符串resourcegroup=“resourcegroupname”;
字符串sqlservername=“brandotest”;
字符串版本=“2014-04-01”;
字符串authContextURL=”https://login.windows.net/“+租户;
var authenticationContext=新的authenticationContext(authContextURL);
var-credential=新的ClientCredential(clientId,clientSecret);
var result=authenticationContext.AcquireToken(资源):https://management.azure.com/“,客户端凭据:凭据);
如果(结果==null)
{
抛出新的InvalidOperationException(“未能获取JWT令牌”);
}
字符串令牌=result.AccessToken;
HttpWebRequest请求=(HttpWebRequest)HttpWebRequest.Create(string.Format(“https://management.azure.com/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.Sql/servers/{2}/databases?api版本={3},subscriptionid,resourcegroup,sqlservername,version));
request.Method=“GET
        // GET: Home
    public async Task<ActionResult> Index()
    {
        ResourceManager rm = new ResourceManager();
        List<string> test = await rm.getSqlDatabaseList();
        //Never Gets to this line of code and never calls the for each or anything after
        foreach (var item in test)
        {
            System.Diagnostics.Debug.WriteLine(item);
        }
        System.Diagnostics.Debug.WriteLine("Is past for each");
        //AzureManager azm = await AzureManager.createAzureManager();
        //await azm.getResourceGroupList();
        return View(new UserLogin());
    }
  static void Main(string[] args)
        {
            //use result to wait the mehtod executed completely
            List<String> test =  getSqlDatabaseList().Result;

            foreach (var item in test)
            {
                Console.WriteLine(item);
            }

            Console.Read();

        }

        public static async Task<List<String>> getSqlDatabaseList()
        {
            //System.Diagnostics.Debug.WriteLine("Starting to get database list");
            List<string> dbNameList = new List<string>();

            var credentials = SdkContext.AzureCredentialsFactory.FromFile(@"D:\Auth.txt");

            var azure = Azure
                .Configure()
                .WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
                .Authenticate(credentials)
                .WithDefaultSubscription();

            var sqlServer = await azure.SqlServers.GetByResourceGroupAsync("groupname", "brandotest");


            var dbList = sqlServer.Databases.List();

            foreach (ISqlDatabase db in dbList)
            {
                dbNameList.Add(db.Name);
            }
            return dbNameList;
        }
 public static List<String> getSqlDatabaseList()
        {
            //System.Diagnostics.Debug.WriteLine("Starting to get database list");
            List<string> dbNameList = new List<string>();

            string tenantId = "yourtenantid";
            string clientId = "yourclientId";
            string clientSecret = "clientSecret";
            string subscriptionid = "subscriptionid";
            string resourcegroup = "resourcegroupname";

            string sqlservername = "brandotest";
            string version = "2014-04-01";

            string authContextURL = "https://login.windows.net/" + tenantId;
            var authenticationContext = new AuthenticationContext(authContextURL);
            var credential = new ClientCredential(clientId, clientSecret);
            var result = authenticationContext.AcquireToken(resource: "https://management.azure.com/", clientCredential: credential);

            if (result == null)
            {
                throw new InvalidOperationException("Failed to obtain the JWT token");
            }

            string token = result.AccessToken;

            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(string.Format("https://management.azure.com/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.Sql/servers/{2}/databases?api-version={3}", subscriptionid, resourcegroup, sqlservername, version));

            request.Method = "GET";
            request.Headers["Authorization"] = "Bearer " + token;


            request.ContentType = "application/json";


            var httpResponse = (HttpWebResponse)request.GetResponse();
            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                string jsonResponse = streamReader.ReadToEnd();

                dynamic json = JsonConvert.DeserializeObject(jsonResponse);

                dynamic resultList = json.value.Children();


                foreach (var item in resultList)
                {

                    dbNameList.Add(((Newtonsoft.Json.Linq.JValue)item.name).Value.ToString());
                }
            }


            return dbNameList;
        }
 public static async Task<List<String>> getSqlDatabaseList()
        {
            //System.Diagnostics.Debug.WriteLine("Starting to get database list");
            List<string> dbNameList = new List<string>();

            var credentials = SdkContext.AzureCredentialsFactory.FromFile(@"D:\Auth.txt");

            var azure = Azure
                .Configure()
                .WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
                .Authenticate(credentials)
                .WithDefaultSubscription();

            var sqlServer = await azure.SqlServers.GetByResourceGroupAsync("brandosecondtest", "brandotest");

            IReadOnlyList<ISqlDatabase> dbList = null;

            Thread thread = new Thread(() => { dbList = sqlServer.Databases.List(); });
            thread.Start();
            //wait the thread
            thread.Join();

            foreach (ISqlDatabase db in dbList)
            {
                dbNameList.Add(db.Name);
            }
            return dbNameList;
        }