SSIS - PostAsJsonAsync不起作用

时间:2018-03-19 05:42:53

标签: c# ssis asp.net-web-api2 script-task

我正在尝试在SSIS包中使用一个WEB API。我想在一个服务器文件夹中发布一个pdf文件。我为此创建了一个脚本任务。

但是当我执行该包时,它给了我一些随机的运行时错误。

byte[] content = System.IO.File.ReadAllBytes("Testfile.pdf");
string serviceUrl = "http://NNN.NN.NNN.NN:NNNN/";
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(serviceUrl);
// Add an Accept header for JSON format.  
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));

decimal amount = 1200.50m;
long tranactionID = 1001;

string APIUrl = "api/UploadDocCon/PDA/Test.pdf/FlexField/ABC/ABCD?FolderName=FoldA";
HttpResponseMessage response = client.PostAsJsonAsync(APIUrl, content).Result;


if (response.IsSuccessStatusCode)
{
  var serviceResponse = response.Content.ReadAsAsync<DocumentServiceResponse>().Result as DocServiceResponse;
return serviceResponse.STREAMID;
}
else
{
string error = response.Content.ReadAsStringAsync().Result;
}

它给了我这个错误(它甚至没有打到调试器)

Exception has been thrown by the target of an invocation

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

当我替换此行

HttpResponseMessage response = client.PostAsJsonAsync(APIUrl, content).Result;

与      var response = client.GetAsync(APIUrl).Result;

它不会给出任何错误。但我想在这里发布一些字节数组。

我在这里做错了什么?

1 个答案:

答案 0 :(得分:2)

您可以将HttpClient转换为byte[],使用System.Net.Http.HttpContent传输字节数组。

以下是代码段:

ByteArrayContent byteContent = new ByteArrayContent(content); HttpResponseMessage reponse = await client.PostAsJsonAsync(uri, byteContent);