通过脚本任务调用SSIS中的安全Web服务



我正在处理一个SSIS包,在该包中,我们必须通过脚本任务调用或使用SSIS中的web服务。我已经浏览了很多链接,但我找不到确切的解决方案。我收到了很多推荐信,尽管我无法破解。

我的要求是,我需要通过具有客户端证书的脚本任务来调用web服务URL。在调用这个URL之后,我们将从web服务获得一个WSDL文件。我们需要使用该WSDL文件,并且需要标识该WSDL中的方法,并且需要将该WSDL中可用的数据写入数据库表。我不知道如何通过脚本tas调用web服务URL(带证书),如何读取WSDL文件,以及如何将数据加载到DB表中。

在ServiceReferences文件夹下添加Service引用,在reference文件夹下添加System.ServiceModel(这是为了在脚本中使用EndPointAddress类)

在Main方法中,使用以下脚本(高级)开始。。。

 var endPointAddress = new EndpointAddress('http://Server/ServiceName.svc');
 //Put your end point address 
 var basicBinding = new BasicHttpBinding();
 basicBinding.Name = "BasicHttpBinding_IService";
 //this is the port name, you can find it in the WSDL
 ClassServiceClient pay = new ClassServiceClient (basicBinding, endPointAddress);
 //this is the class in which the method exists you want to make a service call 
 IService = pay.YourMethodName();
 XMLDocument xmlOut = new XmlDocument();
 //This is to store return value from your method 
 xmlOut.LoadXml(IService);
 //Load the xmlOut with the return value
 XmlNode xmlNode = xmlOut.SelectSingleNode("ParentElement/ChildElement");
 //Search for your element name where you want to get the value
 string strValue = xmlNode.InnerText;
 //this gives the element value

接下来,使用DataTable类,通过创建新行来加载strValue

DataTable dt = new DataTable();
DataRow dr = dt.NewRow();
dr["ValueToInsertIntoDb"] = strValue;
dr.Rows.Add(dr);

之后,将dt分配给对象变量。

Dts.Variables["User::Values"].Value = dt;

接下来,使用另一个数据流任务,在其中使用脚本组件并在ReadOnlyVariables中选择变量。在脚本组件内部,您必须遍历DataTable数据集。这是应该看起来像的代码

 DataTable dt = (DataTable)Variables.Values
   foreach (DataRow dr in dt.Rows)
   {
     ScriptComponentOutputBuffer.AddRow()
     ScriptComponentOutputBuffer.Column1 = dr["ValueToInsertIntoDb"].ToString();
   }
   //ScriptComponentOutputBuffer.Column1 --You need to manually add this column on output columns of your scriptcomponent

接下来,将脚本组件连接到OLEDB命令或OLE DB目标,并将值插入数据库。

在解决方案资源管理器中添加服务引用。这将允许您在代码中引用web服务,并通过对象浏览器进行浏览。我通常首先通过浏览器访问WSDL,以探索属性和方法。

如果脚本任务不是绝对要求,您可以尝试web服务任务:https://www.mssqltips.com/sqlservertip/3272/example-using-web-services-with-sql-server-integration-services/

最新更新