C# 调用Azure Translator API的SSIS脚本组件抛出一个已断开连接的缓冲区
我正在使用SSIS脚本组件,使用Azure认知服务转换API将表行从德语转换为英语 API将正确返回翻译。但是自动创建的文件BufferWrapper.cs抛出Dts.Pipeline.BufferDisconnectedException 用于调用API的Microsoft文档将主要API调用方法指定为:C# 调用Azure Translator API的SSIS脚本组件抛出一个已断开连接的缓冲区,c#,sql-server,ssis,azure-cognitive-services,C#,Sql Server,Ssis,Azure Cognitive Services,我正在使用SSIS脚本组件,使用Azure认知服务转换API将表行从德语转换为英语 API将正确返回翻译。但是自动创建的文件BufferWrapper.cs抛出Dts.Pipeline.BufferDisconnectedException 用于调用API的Microsoft文档将主要API调用方法指定为: static async Task Main(字符串[]args) 同时,用于处理表中每一行的SSIS脚本组件自动创建方法定义为: public override void Input0\u
static async Task Main(字符串[]args)
同时,用于处理表中每一行的SSIS脚本组件自动创建方法定义为:
public override void Input0\u ProcessInputRow(Input0Buffer行)
我想我的问题是如何协调/整合这两种方法
public class ScriptMain : UserComponent
{
private static readonly string subscriptionKey = "###########";
private static readonly string endpoint = "https://api.cognitive.microsofttranslator.com";
private static readonly string location = "global";
public override async void Input0_ProcessInputRow(Input0Buffer Row)
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
string route = "/translate?api-version=3.0&from=de&to=en";
// get text and convert to Json
string textToTranslate = Row.TextToTranslate;
object[] body = new object[] { new { Text = textToTranslate } };
var requestBody = JsonConvert.SerializeObject(body);
using (var client = new HttpClient())
using (var request = new HttpRequestMessage())
{
// Build the request.
request.Method = HttpMethod.Post;
request.RequestUri = new Uri(endpoint + route);
request.Content = new StringContent(requestBody, Encoding.UTF8, "application/json");
request.Headers.Add("Ocp-Apim-Subscription-Key", subscriptionKey);
request.Headers.Add("Ocp-Apim-Subscription-Region", location);
// Send the request and get response.
HttpResponseMessage response = await client.SendAsync(request).ConfigureAwait(false);
// Read response as a string.
string result = await response.Content.ReadAsStringAsync();
Row.TranslationFromAPI = result;
}
}
}
不太可能是核心问题,但除非Newtonsoft在GAC中,否则脚本将失败,因为无法从nugetI重新提取SSIS脚本任务/组件。如果看到其中的异步调用,您不需要等待结果吗?闻起来这可能是导致断开连接的缓冲区出现异常的原因我已修复以等待结果,但它仍会出现异常。我正在使用Microsoft文档介绍如何调用API。我试图用SSIS自动创建的方法来协调这一点。我更新了我的问题来解释。谢谢你给我的建议。