我的. static async Task Main(string[] args)_ 我正在嘗試從內部呼叫。public static async Task c8y_pushEvent(string Event_MSG)class Programpublic static async Task c8y_pushEvent(string Event_MSG)static async Task Main(string[] args)
class Program
{
int int_lastPushedId = 235;
int get_maxID_old = 250;
string c8y_Event_MSG = String.Empty;
static async Task Main(string[] args)
{
//Here's where I am getting the problem.
for(int event_dbID_lp = int_lastPushedId 1 ; event_dbID_lp <= get_maxID_old ; event_dbID_lp )
{
SqlCommand command4 = new SqlCommand(queryString4, connection);
command4.Parameters.Add("@ID", (int)event_dbID_lp);
SqlDataReader reader4 = command4.ExecuteReader();
while (reader4.Read())
{
c8y_Event_MSG = Convert.ToString(reader4[0]);
await c8y_pushEvent(c8y_Event_MSG);
}
reader4.Close();
}
}
public static async Task c8y_pushEvent(string Event_MSG)
{
var serviceCollection = new ServiceCollection();
ConfigureServices(serviceCollection);
var services = serviceCollection.BuildServiceProvider();
var httpClientFactory = services.GetRequiredService<IHttpClientFactory>();
List<KeyValue> keyvalue = JsonConvert.DeserializeObject<List<KeyValue>>(Event_MSG);
string controllerName = keyvalue[0].ToString();
JArray EventMSG = JArray.Parse(Event_MSG);
var item = EventMSG[0];
var httpClientgetControllerId = httpClientFactory.CreateClient("getControllerId");
var httpClientcreateGolfControllerEvent = httpClientFactory.CreateClient("createGolfControllerEvent");
var request1 = await httpClientgetControllerId.GetAsync($"abc/xyz/{controllerName}");
if (request1.IsSuccessStatusCode)
{
HttpResponseMessage request2 = await httpClientcreateGolfControllerEvent.PostAsync("event/events", stringContent);
//stringContent is fetched from some other lines which I've not
//included here
}
}
private static void ConfigureServices(ServiceCollection services)
{
services.AddHttpClient("getControllerId", options =>
{
options.BaseAddress = new Uri("https://myurl.com/");
options.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic","auth_value");
});
services.AddHttpClient("createGolfControllerEvent", options =>
{
options.BaseAddress = new Uri("https://myurl.com/");
options.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic","auth_value");
options.DefaultRequestHeaders.Add("Accept", "application/vnd.com.nsn.xyz.event json");
});
}
public class KeyValue
{
[JsonProperty("ControllerName")]
public string ControllerName { get; set; }
public override string ToString()
{
return String.Format(ControllerName.ToString());
}
}
}
我收到錯誤
非靜態欄位、方法或屬性“Program.c8y_Event_MSG”需要物件參考 [deserializeJSON]
在線的
await c8y_pushEvent(c8y_Event_MSG);
我該如何解決這個問題?
附加問題:
int_lastPushedId和之間的差異get_maxID_old可以大到 1000 ,因此可以有 1000 次迭代。我的 IHttpClientFactory 實作是否適合這個,確保沒有耗盡套接字/埠?
uj5u.com熱心網友回復:
主要錯誤
看起來c8y_Event_MSG應該是本地的:
while (reader4.Read())
{
var c8y_Event_MSG = Convert.ToString(reader4[0]);
await c8y_pushEvent(c8y_Event_MSG);
}
HttpClient 問題
所有的依賴注入代碼似乎都是不必要的。我想不出var serviceCollection = new ServiceCollection();在 for 回圈中是個好主意的情況。
這是一個更簡單的版本,應該可以完成這項作業:
class Program
{
static HttpClient httpClient;
static async Task Main(string[] args)
{
httpClient = new HttpClient();
//Setup auth and base address
for(...)
{
...
c8y_pushEvent(...)
}
}
public static async Task c8y_pushEvent(string Event_MSG, HttpClient)
{
List<KeyValue> keyvalue = JsonConvert.DeserializeObject<List<KeyValue>>(Event_MSG);
string controllerName = keyvalue[0].ToString();
JArray EventMSG = JArray.Parse(Event_MSG);
var item = EventMSG[0];
var request1 = await httpClient.GetAsync($"abc/xyz/{controllerName}");
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/487100.html
標籤:C# 。网 异步等待 dotnet-httpclient
上一篇:C#TamilRunes:如何獲得正確數量的泰米爾字母
下一篇:在Rider中添加.NET參考
