简体   繁体   中英

How to use FireError for an SSIS task script?

The following SSIS task script is supposed to insert data into three tables, however the script is only working locally but not on the server. I want to use FireError() / FireInformation in order to debug and find out what the reason for that. My problem is that I don't know how to write an FireError for this script. Can you please provide me with a small exmaple/hint or whatever you want to call it, so that I can apply it to my code.

#region Namespaces
using System;
using System.Data;
using System.Text;
using System.Net.Http;
using Microsoft.SqlServer.Dts.Pipeline.Wrapper;
using Microsoft.SqlServer.Dts.Runtime.Wrapper;
using System.Security.Cryptography;
using System.IO;
using System.IO.Compression;
using System.Web.Script.Serialization;
using SC_2e3723d7849249a59fd8f421bff5cab1;
using System.Collections.Generic;

using System.Linq;
using System.Threading.Tasks;
using System.Net;


[Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute]
public class ScriptMain : UserComponent
{
    public override void PreExecute()
    {
        base.PreExecute();
        /*
         * Add your code here
         */
    }

    /// <summary>
    /// This method is called after all the rows have passed through this component.
    ///
    /// You can delete this method if you don't need to do anything here.
    /// </summary>
    public override void PostExecute()
    {
        base.PostExecute();
        /*
         * Add your code here
         */
    }

    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".
        */

        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
        string path = "/jobs-sdk/jobs/job";
        string props = "customerAddrCity%2CcustomerAddrCountry%2CcustomerAddrLine1%2CcustomerAddrLine2%2CcustomerAddrPostalCode%2CcustomerAddrState%2CcustomerContact%2CcustomerName%2CcustomerPhoneNumber%2CdeviceId%2Cduplex%2CframeSizeX%2CframeSizeY%2ChpTrackingId%2Cimpressions%2Cimpressions1Color%2Cimpressions2Colors%2CimpressionsType%2Cinks%2CinkUnits%2CjdfJobId%2CjdfJobPartId%2CjobCompleteTime%2CjobCondition%2CjobCopies%2CjobElapseTime%2CjobId%2CjobLastEventTime%2CjobName%2CjobPriority%2CjobPriorityEnum%2CjobProgress%2CjobSubmitTime%2CjobSubstrate%2CjobType%2CjobWorkTimeEstimate%2ClastPrintedTime%2Clocation%2ClocationType%2Cmarker%2CparentDevId%2CparentJobId%2CqueueOrderIndex%2Cresolution%2Csubstrates%2CsubstrateUnits%2CticketTemplate";
        string baseurl = Convert.ToString(Variables.strHPBaseUrl);
        string startMarker = Convert.ToString(Variables.intMaxMarker);
        string secret = Convert.ToString(Variables.strHPSecret);
        string key = Convert.ToString(Variables.strHPKey);
        string limit = Convert.ToString(Variables.intLimit);
        int maxLoopIterations = Variables.intMaxLoopIterations;
        Boolean fireAgain = true;

        int counter;
        int LoopIteration = 0;
        string fullurl;
        string CurrentMaxMarker = startMarker;
        Boolean IsEndReached = false;


        do
        {
            counter = 0;
            LoopIteration++;
            #region InnerDo
            fullurl = baseurl + path + "?limit=" + limit + "&properties=" + props + "&startMarker=" + CurrentMaxMarker + "&direction=forward";
            ComponentMetaData.FireInformation(10, "CallwebApi", "Processing has started with Url: " + fullurl, "", 0, fireAgain);
            ComponentMetaData.FireInformation(10, "CallwebApi", "StartDoLoop with Iteration: " + LoopIteration, "", 0, fireAgain);

            using (var client = new HttpClient())
            {
                CreateHmacHeaders("GET", path, client, secret, key);

                try
                {
                    HttpResponseMessage response = client.GetAsync(fullurl).Result;
                    if (response.IsSuccessStatusCode)
                    {
                        Stream a = response.Content.ReadAsStreamAsync().Result;
                        //a = new GZipStream(a, CompressionMode.Decompress);
                        StreamReader Reader = new StreamReader(a, Encoding.Default);
                        string Html = Reader.ReadToEnd();
                        a.Close();
                        JavaScriptSerializer js = new JavaScriptSerializer();
                        List<JobContext> Jobs = new List<JobContext>();
                        Jobs = js.Deserialize<List<JobContext>>(Html);

                        //loops trough foreach (var j in Jobs) and set Attributes to ContextBuffer.AddRow();
                        foreach (var j in Jobs)
                        {
                            counter++;
                            #region Job
                            ContextBuffer.AddRow();
                            string JobId = "";
                            try
                            {
                                ContextBuffer.JobName = Convert.ToString(j.JobName);
                            }
                            catch (NullReferenceException)
                            {
                                ContextBuffer.JobName_IsNull = true;
                            }

                            try
                            {
                                ContextBuffer.DeviceId = Convert.ToString(j.DeviceId);
                            }
                            catch (NullReferenceException)
                            {
                                ContextBuffer.DeviceId_IsNull = true;
                            }


                            try
                            {
                                ContextBuffer.Duplex = Convert.ToString(j.Duplex);
                            }
                            catch (NullReferenceException)
                            {
                                ContextBuffer.Duplex_IsNull = true;
                            }


                            try
                            {
                                ContextBuffer.Impressions = Convert.ToInt32(j.Impressions);

                            }
                            catch (NullReferenceException)
                            {
                                ContextBuffer.Impressions_IsNull = true;
                            }


                            try
                            {
                                ContextBuffer.Impressions1Color = Convert.ToInt32(j.Impressions1Color);
                            }
                            catch (NullReferenceException)
                            {
                                ContextBuffer.Impressions1Color_IsNull = true;
                            }


                            try
                            {
                                ContextBuffer.Impressions2Colors = Convert.ToInt32(j.Impressions2Colors);
                            }
                            catch (NullReferenceException)
                            {
                                ContextBuffer.Impressions2Colors_IsNull = true;
                            }

                            try
                            {
                                ContextBuffer.ImpressionsType = Convert.ToString(j.ImpressionsType);
                            }
                            catch (NullReferenceException)
                            {
                                ContextBuffer.ImpressionsType_IsNull = true;
                            }


                            try
                            {
                                ContextBuffer.InkUnits = Convert.ToString(j.InkUnits);
                            }
                            catch (NullReferenceException)
                            {
                                ContextBuffer.InkUnits_IsNull = true;
                            }


                            try
                            {
                                ContextBuffer.JobCompleteTime = Convert.ToString(j.JobCompleteTime);
                            }
                            catch (NullReferenceException)
                            {
                                ContextBuffer.JobCompleteTime_IsNull = true;
                            }


                            try
                            {
                                ContextBuffer.JobCopies = Convert.ToInt32(j.JobCopies);
                            }
                            catch (NullReferenceException)
                            {
                                ContextBuffer.JobCopies_IsNull = true;
                            }


                            try
                            {
                                ContextBuffer.JobElapseTime = Convert.ToInt64(j.JobElapseTime);
                            }
                            catch (NullReferenceException)
                            {
                                ContextBuffer.JobElapseTime_IsNull = true;
                            }


                            try
                            {
                                ContextBuffer.JobId = Convert.ToString(j.JobId);
                                JobId = Convert.ToString(j.JobId);
                            }
                            catch (NullReferenceException)
                            {
                                ContextBuffer.JobId_IsNull = true;
                            }


                            try
                            {
                                ContextBuffer.JobLastEventTime = Convert.ToString(j.JobLastEventTime);
                            }
                            catch (NullReferenceException)
                            {
                                ContextBuffer.JobLastEventTime_IsNull = true;
                            }

                            try
                            {
                                ContextBuffer.JobProgress = Convert.ToString(j.JobProgress);
                            }
                            catch (NullReferenceException)
                            {
                                ContextBuffer.JobProgress_IsNull = true;
                            }


                            try
                            {
                                ContextBuffer.JobSubmitTime = Convert.ToString(j.JobSubmitTime);
                            }
                            catch (NullReferenceException)
                            {
                                ContextBuffer.JobSubmitTime_IsNull = true;
                            }



                            try
                            {
                                ContextBuffer.JobType = Convert.ToString(j.JobType);
                            }
                            catch (NullReferenceException)
                            {
                                ContextBuffer.JobType_IsNull = true;
                            }


                            try
                            {
                                ContextBuffer.Marker = Convert.ToInt64(j.Marker);
                                CurrentMaxMarker = Convert.ToString(j.Marker);
                            }
                            catch (NullReferenceException)
                            {
                                ContextBuffer.Marker_IsNull = true;
                            }


                            try
                            {
                                ContextBuffer.ParentJobId = Convert.ToString(j.ParentJobId);
                            }
                            catch (NullReferenceException)
                            {
                                ContextBuffer.ParentJobId_IsNull = true;
                            }



                            try
                            {
                                ContextBuffer.SubstrateUnits = Convert.ToString(j.SubstrateUnits);
                            }
                            catch (NullReferenceException)
                            {
                                ContextBuffer.SubstrateUnits_IsNull = true;
                            }

                            #endregion

                            #region Substrates
                            if (j.Substrates != null)
                            {
                                foreach (var i in j.Substrates.Counts)
                                {
                                    SubstratesBuffer.AddRow();
                                    try
                                    {
                                        SubstratesBuffer.Name = Convert.ToString(i.Name);
                                    }
                                    catch (NullReferenceException)
                                    {
                                        SubstratesBuffer.Name_IsNull = true;
                                    }

                                    try
                                    {
                                        SubstratesBuffer.AmountUsed = Convert.ToInt32(i.AmountUsed);
                                    }
                                    catch (NullReferenceException)
                                    {
                                        SubstratesBuffer.AmountUsed_IsNull = true;
                                    }


                                    try
                                    {
                                        SubstratesBuffer.JobId = JobId;
                                    }
                                    catch (NullReferenceException)
                                    {
                                        SubstratesBuffer.JobId_IsNull = true;
                                    }

                                }
                            }
                            #endregion

                            #region inks
                            if (j.Inks != null)
                            {
                                foreach (var i in j.Inks.Counts)
                                {
                                    InksBuffer.AddRow();
                                    try
                                    {
                                        InksBuffer.Name = Convert.ToString(i.Name);
                                    }
                                    catch (NullReferenceException)
                                    {
                                        InksBuffer.Name_IsNull = true;
                                    }

                                    try
                                    {
                                        InksBuffer.AmountUsed = Convert.ToInt32(i.AmountUsed);
                                    }
                                    catch (NullReferenceException)
                                    {
                                        InksBuffer.AmountUsed_IsNull = true;
                                    }


                                    try
                                    {
                                        InksBuffer.JobId = JobId;
                                    }
                                    catch (NullReferenceException)
                                    {
                                        InksBuffer.JobId_IsNull = true;
                                    }

                                }
                            }


                            #endregion

                        }

                    }
                    else //response.IsSuccessStatusCode
                    {
                        ErrorBuffer.AddRow();
                        ErrorBuffer.ErrorMessage = "Status code is unsuccessful";
                        ErrorBuffer.ErrorMessageStacktrace = SubstringStringByLength(response.ReasonPhrase, 4000);
                    }
                }
                catch (Exception e) // From make call to parse Objkect
                {
                    ErrorBuffer.AddRow();
                    ErrorBuffer.ErrorMessage = SubstringStringByLength(e.Message.ToString(), 950);
                    ErrorBuffer.ErrorMessageStacktrace = SubstringStringByLength(e.StackTrace.ToString(), 4000);
                }

            }
            #endregion

            if (LoopIteration >= maxLoopIterations)
            {
                IsEndReached = true;
            }

            if (counter <= 0)
            {
                IsEndReached = true;
            }
        } while (IsEndReached == false);

    }


    private static void CreateHmacHeaders(string method, string path, HttpClient client, string secret, string key)
    {
        string timeStamp = DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ssZ");
        string stringToSign = method + " " + path + timeStamp;
        HMACSHA256 hmac = new HMACSHA256(Encoding.UTF8.GetBytes(secret));
        byte[] bytes = hmac.ComputeHash(Encoding.UTF8.GetBytes(stringToSign));
        string signature = BitConverter.ToString(bytes).Replace("-", string.Empty).ToLower();
        string auth = key + ":" + signature;

        client.DefaultRequestHeaders.Add("x-hp-hmac-authentication", auth);
        client.DefaultRequestHeaders.Add("x-hp-hmac-date", timeStamp);
        client.DefaultRequestHeaders.Add("x-hp-hmac-algorithm", "SHA256");
    }

    
    private string SubstringStringByLength(string LongString, int maxLength)
    {
        int maxLengthInoutString = (LongString.Length > maxLength ? maxLength : LongString.Length);
        String OutputString = (LongString != null)
                 ? LongString.Substring(0, maxLengthInoutString)
                 : "";

        return OutputString;
    }




}

Books online provides the

Sample code from https://stackoverflow.com/a/28907522/181965 which demonstrates the differences between the Script Task vs Script Component (which you're using) method signatures

    bool fireAgain = false;
    this.Dts.Events.FireInformation(0, "my sub", "info", string.Empty, 0, ref fireAgain);
    // Note, no cancel available
    this.Dts.Events.FireError(0, "my sub", "error", string.Empty, 0);

As to where you'd plumb this into your existing code, I don't know what your design goals are but I'd assume it'd fit in where your ErrorBuffer calls are - although FireError will blow up processing so maybe not? Maybe you want to accumulate all the bad rows into a global variable and in the PostEvent section, enumerate through them so you can see all the bad rows. /shrug

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM