Search code examples
ssisdotnet-httpclientscript-task

Issue with HttpClient.SendAsync("...") in SSIS Script Task


I want to access the MS Graph API from inside my script task. I didn't get the SDK to work (in SSIS at least) so I thought I would write my own class using the System.Net.Http HttpClient. Outside of SSIS I did not have any problem getting that to work. When I'm trying to run HttpClient.SendAsync() in SSIS however I'm getting an Error.

public void Main()
        {
            var HttpClient = new HttpClient();
            var req_msg = new HttpRequestMessage(HttpMethod.Get, "http://ip.jsontest.com/");

            HttpResponseMessage t = System.Threading.Tasks.Task.Run(async () =>
                await HttpClient.SendAsync(req_msg)).Result;

            MessageBox.Show(t.StatusCode.ToString()); // -> OK

            var req_msg2 = new HttpRequestMessage(HttpMethod.Get, $"https://graph.microsoft.com/v1.0/users/{test_name}");

            HttpResponseMessage t2 = System.Threading.Tasks.Task.Run(async () =>
                await HttpClient.SendAsync(req_msg2)).Result; // -> Error

            MessageBox.Show(t2.StatusCode.ToString());

            Dts.TaskResult = (int)ScriptResults.Success;
        }

This is the Error:

   bei System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
   bei System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
   bei System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   bei System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)
   bei Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTATaskScriptingEngine.ExecuteScript()

It might have something to do with Https, that's my theory at least. Then again in regular c# projects that was not a problem.

Also it's the same way with getAsync(), postAsync(), ...


Solution

  • It turns out the HttpClient wasn't using a (correct) security protocol. I'm obviously not at all an expert on that topic but changing the security protocol at the beginning of my script to TLS 1.2 using this code:

    System.Net.ServicePointManager.SecurityProtocol = (System.Net.SecurityProtocolType)3072;
    

    fixed my problem completely. Hope this helps someone