我为不同的域/主机接收了6个唯一的URL,解析了它们返回的XML,并插入到表中。我有这个工作正常,除了它同步每个服务器(一次一个)。我需要它同时联系所有六台服务器。
我更改了我的WebClient调用以使用Async方法然后添加了一个while语句来检查是否所有WebClient调用都已返回,但是在while语句完成后SSIS包似乎被取消,无论我是否调用了SetEndOfRowSet或不。 SSIS日志记录中没有显示错误(即使选择所有日志选项)。它说'取消'在输出窗口中,有一个简短的控制台窗口,当它发生时弹出但我无法捕捉它。
如果我没有在SSIS包中使用while语句,那么包将继续通过我的脚本转换,并且不会从中输出任何行。在SynchronousInputID上将脚本转换设置为None。
这是SSIS C#脚本转换代码:
/* Microsoft SQL Server Integration Services Script Component
* Write scripts using Microsoft Visual C# 2008.
* ScriptMain is the entry point class of the script.*/
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Pipeline.Wrapper;
using Microsoft.SqlServer.Dts.Runtime.Wrapper;
using System.Net;
using System.Diagnostics;
using System.Xml;
[Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute]
public class ScriptMain : UserComponent
{
private int CountComplete = 0;
public override void PreExecute()
{
base.PreExecute();
}
public override void PostExecute()
{
base.PostExecute();
}
public override void Input0_ProcessInput(Input0Buffer Buffer)
{
while(Buffer.NextRow())
{
Input0_ProcessInputRow(Buffer);
}
if (Buffer.EndOfRowset())
{
while (CountComplete < Variables.intRows)
{
System.Threading.Thread.Sleep(500);
}
Output0Buffer.SetEndOfRowset();
}
}
public override void Input0_ProcessInputRow(Input0Buffer Row)
{
string DNS_NA = Row.DNSNA;
long SERVER = Convert.ToInt64(Row.SERSYSNR);
// Ignore server/certificate mismatch...
ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;
// Declare variable that will hold the xml document received by the API
string xmlDoc = String.Empty;
// Get the URI from the variable
string url = @"https://" + DNS_NA + Variables.strURLSuffix;
// Time the request...
Stopwatch timer = Stopwatch.StartNew();
//Create a Web Client
WebClient client = new WebClient();
client.DownloadStringCompleted += (sender, e) =>
{
timer.Stop();
processRow(e.Result, SERVER, Convert.ToInt32(timer.ElapsedMilliseconds), Convert.ToInt32(Row.UPTIMQY), Output0Buffer);
CountComplete += 1;
};
Uri uri = new Uri(url);
client.DownloadStringAsync(uri);
}
private void processRow(string xmlDoc, long SERVER, int FRAPI_TIME, int PRE_UPTIME, Output0Buffer buffer)
{
try
{
XmlDocument xDoc = new XmlDocument();
xDoc.LoadXml(xmlDoc);
bool NORESPONSE = false; // If we made it this far, we got a response, right?
short CPU = Convert.ToInt16(xDoc.SelectSingleNode("//server").ChildNodes[1].InnerText);
int TOT_MEM = Convert.ToInt32(xDoc.SelectSingleNode("//server").ChildNodes[2].InnerText) / (1024 * 1024);
int USE_MEM = Convert.ToInt32(xDoc.SelectSingleNode("//server").ChildNodes[3].InnerText) / (1024 * 1024);
int UPTIME = Convert.ToInt32(xDoc.SelectSingleNode("//server").ChildNodes[4].InnerText) / 1000 / 60;
short REQUESTS = Convert.ToInt16(xDoc.SelectSingleNode("//server").ChildNodes[5].InnerText);
bool RESET = (UPTIME < PRE_UPTIME) ? true : false;
foreach (XmlNode xNode in xDoc.SelectNodes("//server/detail/request"))
{
buffer.AddRow();
buffer.SERSYSNR = SERVER;
buffer.CPUUSEQY = CPU;
buffer.TOTMEMQY = TOT_MEM;
buffer.USEMEMQY = USE_MEM;
buffer.UPTIMQY = UPTIME;
buffer.REQCNTQY = REQUESTS;
buffer.REQMSQY = FRAPI_TIME;
buffer.SERNONRSPIR = NORESPONSE;
buffer.SERRSTIR = RESET;
buffer.REQSYSNR = Convert.ToInt32(xNode.ChildNodes[0].InnerText);
buffer.REQIPTE = xNode.ChildNodes[1].InnerText;
buffer.REQURLTE = xNode.ChildNodes[2].InnerText;
buffer.REQRUNTMQY = Convert.ToInt32(xNode.ChildNodes[3].InnerText);
}
}
catch
{
// Hopefully we didn't throw an exception inside the foreach where a row has already been added...
buffer.AddRow();
buffer.SERSYSNR = SERVER;
buffer.SERNONRSPIR = true;
}
}
// public override void CreateNewOutputRows()
// {
/*
Add rows by calling the AddRow method on the member variable named "<Output Name>Buffer".
For example, call MyOutputBuffer.AddRow() if your output was named "MyOutput".
*/
//}
}
我可以在DNS_NA上使用条件分割并生成x个脚本组件,然后使用UNION ALL它们,但每次将新服务器添加到列表中时我都必须编辑SSIS包。不理想。
答案 0 :(得分:0)
分成6个脚本任务是唯一有效的解决方案。我在使用DownloadStringSync时通过切换到线程设置并仅调用DownloadString来修复SSIS崩溃。我必须确保在处理结果时锁定了Output0Buffer,这让我有点困扰。我最后使用一个列表来存储线程并循环遍历它们并在我在ProcessInputBuffer中检查Buffer.EndOfRowSet()之后调用Thread.Join()。
然而,该解决方案仍然不会转发行,直到所有请求都已完成,这意味着如果一台服务器运行了很长一段时间,结果就不会被放入数据库直到所有请求都返回。