में Async कैसे करें मेरे पास एक async विधि है जो किसी सर्वर से एपीआई डेटा प्राप्त करती है। जब मैं अपने स्थानीय मशीन पर एक कंसोल ऐप में यह कोड चलाता हूं, तो यह एसिंक फ़ंक्शन प्रति मिनट में कुछ सौ http कॉल के माध्यम से दबाकर उच्च गति पर प्रदर्शन करता है। जब मैं एक ही कोड को एक Azure WebJob कतार संदेश से ट्रिगर करने के लिए डालता हूं, हालांकि, यह सिंक्रनाइज़ रूप से काम करता प्रतीत होता है और मेरी संख्या क्रॉल होती है - मुझे यकीन है कि मुझे अपने दृष्टिकोण में कुछ आसान याद आ रही है - किसी भी सहायता की सराहना की।Azure WebJob फ़ंक्शन
(1) .. WebJob समारोह है कि कतार के संदेश के लिए सुनता है और संदेश पर प्रक्रिया हो प्राप्त एपीआई की शुरूआत:
public class Functions
{
// This function will get triggered/executed when a new message is written
// on an Azure Queue called queue.
public static async Task ProcessQueueMessage ([QueueTrigger("myqueue")] string message, TextWriter log)
{
var getAPIData = new GetData();
getAPIData.DoIt(message).Wait();
log.WriteLine("*** done: " + message);
}
}
(2) वर्ग कि गति से async मोड में बाहर काम करता है नीला ...
class GetData
{
// wrapper that is called by the message function trigger
public async Task DoIt(string MessageFile)
{
await CallAPI(MessageFile);
}
public async Task<string> CallAPI(string MessageFile)
{
/// create a list of sample APIs to call...
var apiCallList = new List<string>();
apiCallList.Add("localhost/?q=1");
apiCallList.Add("localhost/?q=2");
apiCallList.Add("localhost/?q=3");
apiCallList.Add("localhost/?q=4");
apiCallList.Add("localhost/?q=5");
// setup httpclient
HttpClient client =
new HttpClient() { MaxResponseContentBufferSize = 10000000 };
var timeout = new TimeSpan(0, 5, 0); // 5 min timeout
client.Timeout = timeout;
// create a list of http api get Task...
IEnumerable<Task<string>> allResults = apiCallList.Select(str => ProcessURLPageAsync(str, client));
// wait for them all to complete, then move on...
await Task.WhenAll(allResults);
return allResults.ToString();
}
async Task<string> ProcessURLPageAsync(string APIAddressString, HttpClient client)
{
string page = "";
HttpResponseMessage resX;
try
{
// set the address to call
Uri URL = new Uri(APIAddressString);
// execute the call
resX = await client.GetAsync(URL);
page = await resX.Content.ReadAsStringAsync();
string rslt = page;
// do something with the api response data
}
catch (Exception ex)
{
// log error
}
return page;
}
}
क्या आपने "getAPIData.DoIt (संदेश) को बदलने का प्रयास किया है। प्रतीक्षा करें();" "पाने के लिए getAPIData.DoIt (संदेश);"? –
धन्यवाद जेसन - आपकी और अन्य टिप्पणियों ने मुझे यह काम करने में मदद की। बहुत सराहना की। – qtime67