C# Verarbeiten Sie große Aufgabe von Upload -Skripts berichtet Erfolg, bevor HTTPClient beendet ist
Posted: 14 Sep 2025, 11:44
Versuchen Sie, eine SSIS -Datenflussaufgabe zu erhalten, um eine große Abfrage (und die resultierende große Antwort zu erfassen) in die Cloud hochladen. Der Code funktioniert in Ordnung, aber die Skriptaufgaben berichten aus (und der Steuerfluss bewegt sich zum nächsten Schritt), lange bevor das Upload und die Antwort abgeschlossen sind. Ich bin mir nicht sicher, ob ich erwarte, dass der Code etwas tut oder ob ich den Code falsch habe: < /p>
Das messageBox "Skript komplett" wird sofort angezeigt - und dann 15-20 Sekunden später wird der messageBox für den Antwortcode auftaucht. Alles wird korrekt abgeschlossen, einschließlich der Protokollierung der Antwortfehler ansonsten. Dies wird in Visual Studio 2022
jede sehr geschätzte Hilfe geschrieben! < /P>
Code: Select all
#region Namespaces
using System;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
#endregion
namespace ST_fceb5e6b88ee4a39b755793d4261bec8
{
[Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute]
public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{
public async void Main()
{
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
var handler = new HttpClientHandler();
handler.UseCookies = false;
string hostURL = @"https://myupload.com?id=12345";
string fileToLoad = Dts.Variables["User::SourceFileName"].Value.ToString();
string OutputFile = @"D:\BadStuffHappenedFolder\OutputErrors.txt";
try
{
//==================
using (var httpClient = new HttpClient(handler))
{
httpClient.BaseAddress = new Uri(hostURL);
System.Windows.Forms.MessageBox.Show("New Http Client");
using (var multipartFormContent = new MultipartFormDataContent())
{
//Load the file and set the file's Content-Type header
using (var fileStreamContent = new StreamContent(File.OpenRead(fileToLoad)))
{
fileStreamContent.Headers.ContentType = new MediaTypeHeaderValue("text/csv");
//Add the file
multipartFormContent.Add(fileStreamContent, name: "file", fileName: Path.GetFileName(fileToLoad));
System.Windows.Forms.MessageBox.Show("Ready to send!");
//Send it
HttpResponseMessage response = await httpClient.PostAsync(hostURL, multipartFormContent);
System.Windows.Forms.MessageBox.Show(response.ToString());
response.EnsureSuccessStatusCode();
//string responseBody = await response.Content.ReadAsStringAsync();
using (Stream contentStream = await response.Content.ReadAsStreamAsync())
{
// Check if the file exists before attempting to delete it
if (File.Exists(OutputFile))
{
try
{
File.Delete(OutputFile);
System.Windows.Forms.MessageBox.Show($"File '{OutputFile}' deleted successfully.");
}
catch (IOException ex)
{
System.Windows.Forms.MessageBox.Show($"Error deleting file: {ex.Message}");
}
}
// Create or open the file for writing
using (FileStream fileStream = File.Create(OutputFile))
{
// Copy the content stream to the file stream
await contentStream.CopyToAsync(fileStream);
}
}
System.Windows.Forms.MessageBox.Show("script Complete");
}
}
}
//==================
//create the archive filename and rename the file just used
var now = DateTime.Now.ToString("yyyyMMdd_HHmm");
var archiveFile = fileToLoad.Replace(".csv", "") + "_" + now + ".csv";
if (File.Exists(archiveFile))
{
File.Delete(archiveFile);
}
File.Move(fileToLoad, archiveFile);
//now delete the original file
if (File.Exists(archiveFile))
{
File.Delete(fileToLoad);
}
}
catch(Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.ToString());
}
Dts.TaskResult = (int)ScriptResults.Success;
}
#region ScriptResults declaration
enum ScriptResults
{
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
};
#endregion
}
}
jede sehr geschätzte Hilfe geschrieben! < /P>