簡體   English   中英

使用 Azure Function 中的 Kusto.Data 訪問 Azure 數據資源管理器 -- Kusto 發送請求失敗 -- 本地調試工作

[英]Access Azure Data Explorer with Kusto.Data in Azure Function -- Kusto failed to send request -- local debugging works

我遇到了以下問題,並且在線進行了廣泛的搜索並沒有提供任何好的結果。 When trying to access my Azure Data Explorer Database and querying using the Kusto.Data SDK in an Azure Function, it yields the following error:

Kusto 客戶端未能向服務發送請求:“在 getsockopt 或 setsockopt 調用中指定了未知、無效或不受支持的選項或級別。”

但是,在我的本地機器上運行 Function,一切正常。

編輯:function using (var reader = await queryProvider.ExecuteQueryAsync(Database, query, clientRequestProperties))

EDIT2 - 解決方案:

您可以將 NuGet Kusto.Data Package 降級到版本 9.4.1,這解決了問題並且不再拋出任何錯誤。 如果還是遇到困難,可以嘗試通過 http 請求直接訪問 ADX 數據庫:

const string tenantId = "<tenantId>";
const string client_id = "<clientId>";
const string client_secret = "<client_secret>";
const string Cluster = "<cluster_adress";
const string Database = "<database_name>";

var authUrl = "https://login.microsoftonline.com/<tenantId>/oauth2/token";
var param = new Dictionary<string, string>
            {
                {"client_id",client_id},
                {"grant_type","client_credentials"},
                {"client_secret",client_secret},
                {"resource","https://help.kusto.windows.net"}
            };
var data = new FormUrlEncodedContent(param);
using var authClient = new HttpClient();
var response = await authClient.PostAsync(authUrl, data);
string result = response.Content.ReadAsStringAsync().Result;

//parse result
var resultJson = System.Text.Json.JsonDocument.Parse(result);
//retrieve access token
var accessToken = resultJson.RootElement.GetProperty("access_token");
//-----------------------------------------------------------------------------------------------

var dataXUrl = Cluster + "/v1/rest/query";
var database = Database;

var dataXQuery = "sample_table| where Time > ago(2min)";
var body = new Dictionary<string, string>
{
    {"db",database},
    {"csl",dataXQuery}
};

using var dataXClient = new HttpClient();
dataXClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken.ToString());
dataXClient.DefaultRequestHeaders.Add("Accept", "application/json");

HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, dataXUrl);
request.Content = new StringContent(JsonConvert.SerializeObject(body), Encoding.UTF8, "application/json");

var table = await dataXClient.SendAsync(request);

//pretty print
var obj = JsonConvert.DeserializeObject(table.Content.ReadAsStringAsync());
var tableJSON = JsonConvert.SerializeObject(obj, Formatting.Indented);

log.LogInformation("\n\n" + tableJSON);

我在 Azure 應用服務上的連續網絡作業中遇到了同樣的問題。 我使用的 Kusto nuget 版本是 10.1.0

降級到 nuget 9.4.1 立即解決了問題。

僅供參考 - 這似乎只影響 10.1.0。 較早的 10.xx 版本應該可以工作。

ADX 團隊相信他們將在下一個 nuget 版本中修復此問題。

10.0.3 是最新的工作版本

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM