Is It Possible To Call HTTPClient From SSIS Script Task?

Paduck, Daniel 0 Reputation points
2024-06-06T17:37:15.7233333+00:00

Hi,

I have the following code in my SSIS script task:

User's image

However, if I put a break point on the client.SendAsysnc, the code goes right by it. Then if I take out the break point and do a break point after execution, the SSIS project just stops running.

Has anyone been successful at using HTTPClient from SSIS? I am using VS 2019 C# coding.

Thanks,

Daniel

SQL Server Integration Services
SQL Server Integration Services
A Microsoft platform for building enterprise-level data integration and data transformations solutions.
2,592 questions
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. Michael Taylor 54,816 Reputation points
    2024-06-06T17:48:01.7566667+00:00

    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.

    0 comments No comments

  2. Olaf Helper 45,106 Reputation points
    2024-06-06T17:53:21.2733333+00:00

    I have the following code in my SSIS script task:

    That's not code as it, it's a screenshot. Please post the code as code.

    client.SendAsysnc

    You can not really use Async methods i a SSIS script task.

    0 comments No comments

  3. ZoeHui-MSFT 37,671 Reputation points
    2024-06-07T06:24:10.11+00:00

    Hi @Paduck, Daniel,

    You may take a reference to below thread.

    SSIS package C# script task to make https call using httpClient

    Regards,

    Zoe Hui


    If the answer is helpful, please click "Accept Answer" and upvote it.

    0 comments No comments

  4. Alpesh Dhori 0 Reputation points
    2024-09-09T05:25:39.49+00:00

    Code for Script task using C# 2019

    public void Main()

    {

    GetToken();

    }

    public static async void GetToken()

    {

            try
    
            {
    
                ServicePointManager.SecurityProtocol = (SecurityProtocolType)768 | (SecurityProtocolType)3072;
    
                var client = new HttpClient();
    
                var request = new HttpRequestMessage(HttpMethod.Post, "https://login.microsoftonline.com/qa.onmicrosoft.com/oauth2/v2.0/token");
    
                var collection = new List<KeyValuePair<string, string>>();
    
                collection.Add(new KeyValuePair<string, string>("grant_type", "client_credentials"));
    
                collection.Add(new KeyValuePair<string, string>("client_id", "xxx));
    
                collection.Add(new KeyValuePair<string, string>("scope", "https://graph.microsoft.com/.default"));
    
                collection.Add(new KeyValuePair<string, string>("client_secret", "xxx"));
    
                var content = new FormUrlEncodedContent(collection);
    
                request.Content = content;
    
                var response = await client.SendAsync(request);
    
                response.EnsureSuccessStatusCode();
    
                //Console.WriteLine(await response.Content.ReadAsStringAsync());
    
                MessageBox.Show(await response.Content.ReadAsStringAsync());
    
            }
    
            catch (Exception ex) {
    
                MessageBox.Show(ex.Message);
    
                MessageBox.Show(ex.InnerException.Message);
    
            }
    
        }
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.