Yes you can call HttpClient inside a script task. That is the only way to make REST calls.
There are a couple of problems here. Firstly your ObtainToken
method is marked async but it returns void. That means there is no way for the asyn call to actually wait. Hence your SendAsync
call is being triggered but nobody is waiting for it to complete. Async must (almost always) be combined with a return type of Task
. Without that return type the first async method you call immediately returns from the function, the caller isn't waiting and therefore the caller continues executing and your method is effectively ignored. Fix it by changing the return type.
public async Task ObtainToken()
{
//Rest of code
}
The second problem we can only guess at but SSIS script tasks are not async. I assume you're calling this method inside your script's Main method. You cannot use async/await in Main for the same reasons you cannot use it here, the function returns void and async void
doesn't work. So when you call an async method from non-async code you have to explicitly wait for the results. This is generally not recommended but in the rare cases (such as here) it is needed you must do so.
//Assuming the caller is Main which is void Main ()
//Wait for the work to complete
ObtainToken().GetAwaiter().GetResult();
The above code will block Main until the async work is completed inside the called method.