Click here to Skip to main content
15,880,967 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,
I'm consuming the WebAPI from SSIS Packages so i have written the following code in the ScriptTask and my intention is it should to wait for the response (i.e. it will just call the endpoint and continue the process)

var webClient = new System.Net.WebClient();
webClient.Headers.Add("abc", "xyz");
webClient.Headers.Add("abc1", "xyz1");

File.AppendAllLines("FilePath\\Log.txt", new string[] { string.Format("{0}  EP Calling Started: {1}", DateTime.Now, BatchID) });
System.Collections.Specialized.NameValueCollection formData = new System.Collections.Specialized.NameValueCollection();
formData["test"] = "test";
webClient.UploadValuesAsync(new Uri(webAddr), "Post", formData);
File.AppendAllLines("FilePath\\Log.txt",, new string[] { string.Format("{0} EP Calling ended: {1}", DateTime.Now, BatchID) });

Sometimes WebClient is not hitting the endpoint. to confirm that I have placed text file logging in Script Task and endpoint. endpoint calling is happening from packages but it is not hitting the endpoint in logs i found this
when WebClient is not hitting the endpoint. following info is logged into the file
From Script Task:
6/1/2017 1:10:03 PM EP Calling Started: 33962
6/1/2017 1:10:03 PM EP Calling ended: 33962

if it is success log will be like this
From Script Task:
6/1/2017 1:10:03 PM EP Calling Started: 33962
6/1/2017 1:10:03 PM EP Calling ended: 33962
From endPoint:
6/1/2017 1:10:04 PM EP Started: 33962
6/1/2017 1:10:58 PM EP ended: 33962


can you please let me know what is the root cause of the issue?

What I have tried:

var webClient = new System.Net.WebClient();
webClient.Headers.Add("abc", "xyz");
webClient.Headers.Add("abc1", "xyz1");

File.AppendAllLines("FilePath\\Log.txt", new string[] { string.Format("{0}  EP Calling Started: {1}", DateTime.Now, BatchID) });
System.Collections.Specialized.NameValueCollection formData = new System.Collections.Specialized.NameValueCollection();
formData["test"] = "test";
webClient.UploadValuesAsync(new Uri(webAddr), "Post", formData);
File.AppendAllLines("FilePath\\Log.txt",, new string[] { string.Format("{0} EP Calling ended: {1}", DateTime.Now, BatchID) });
Posted
Updated 27-Jun-17 0:57am

1 solution

UploadValuesAsync[^] does not wait for the request to complete. It starts a background operation to make the request, and returns immediately. When the request is complete, the UploadValuesCompleted[^] event will be raised.

Your method is most likely exiting before the request has been completed, because it's not waiting for the UploadValuesCompleted event.

If you want to make a synchronous request, use the UploadValues[^] method instead.

If you still want an asynchronous request, but you want to wait for the request to be completed before your method exits, then you'll need to use something like a ManualResetEvent to wait until the UploadValuesCompleted event fires.
C#
var mre = new ManualResetEventSlim();
webClient.UploadValuesCompleted += delegate { mre.Set(); };
webClient.UploadValuesAsync(new Uri(webAddr), "Post", formData);
...
mre.Wait();
 
Share this answer
 
Comments
KGBRS 27-Jun-17 7:01am    
Hi Richard Deeming,
Thank you for your solution, it was very helpful for me.
but for time being i have tried in an other way with httpClient class.

Hide   Copy Code
 
var hc = new HttpClient();
hc.DefaultRequestHeaders.Add("Key1", Value1);
hc.DefaultRequestHeaders.Add("Key2", Value2); 
var request = new HttpRequestMessage(HttpMethod.Post, webAddr);
var response = hc.SendAsync(request);


with this code also I'm getting same issue. May i know the root cause (it is similar you earlier response?) May I know More details about this issue? please help me.
Thanks in advance.
Richard Deeming 27-Jun-17 7:08am    
Yes, it's the same issue - the SendAsync method returns before the request has completed.

In this case, the method returns a Task, so the solution is slightly different.

If you can make the calling method async, then you need to await the returned task:
Asynchronous programming[^]

But since you're calling this from SSIS, you probably can't do that. Instead, you'll need to block the thread until the request has completed:
var response = hc.SendAsync(request).GetAwaiter().GetResult();

If you want to mirror the WebClient code, and throw an exception if the server responds with an error, you'll need to call:
response.EnsureSuccessStatusCode();
KGBRS 27-Jun-17 10:22am    
Thank you Richard Deeming.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900