【问题标题】:SSIS - PostAsJsonAsync is not workingSSIS - PostAsJsonAsync 不工作
【发布时间】:2018-03-19 05:42:52
【问题描述】:

我正在尝试使用 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;

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

我在这里做错了什么?

【问题讨论】:

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


    【解决方案1】:

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

    这里是sn-p的代码:

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

    【讨论】:

      猜你喜欢
      • 2015-06-29
      • 2020-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多