You are viewing limited content. For full access, please sign in.

Question

Question

Exception has been thrown by the target of invocation - Testing Rule

asked on February 16, 2022 Show version history

I am testing a LF Cloud Rule that runs a C# script via the Agent. I get this message and I am not sure what this means

This is my test script

namespace ClassLibrary1
{
    public class Class1
    {       //This is a method that takes inputs and returns outputs
            //The names of the input and output parameters configured in the Rule are used as input and output Dictionary keys
        public static Task<IDictionary<string, object>> MyScript(IDictionary<string, object> inputsFromRule)
        {



            var outputsFromScript = new Dictionary<string, object>();

            outputsFromScript["Status"] = "Finished";

            
            try
            {


OdbcCommand command = new OdbcCommand(inputsFromRule["Query"].ToString());
                using (OdbcConnection connection = new OdbcConnection("Driver={ODBC Driver 11 for SQL Server};Server=(local);Database=WorkflowData;UID=X;PWD=X;"))
                {
                    command.Connection = connection;
                    connection.Open();
                    command.ExecuteNonQuery();

                    // The connection is automatically closed at
                    // the end of the Using block.
                }
            }
            catch(InvalidCastException e) { outputsFromScript["Status"] = e.Message; }

           

            return Task.FromResult<IDictionary<string, object>>(outputsFromScript);

        }
    }
}

Update:

Trying to use a different library and getting another error I am not sure what to make of. The odd thing is that I should never get any errors back from the agent. I am using a try catch to ensure the error is sent back in the status in case there is anything wrong with my code:

 

0 0

Replies

replied on February 21, 2022

Your catch block is only catching the specific InvalidCastException, which is not assignable from the thrown type. I think you meant to catch all Exceptions?

0 0
replied on February 22, 2022

I think so, probably copied the try catch from a blog entry. Just tried changing InvalidCastException to just Exception but getting the same exact error. Still not sure why it can not run the script successfully, even if my ODBC connection or query is failing. I can't even reference OdbcCommand at all in the script without getting the error.

0 0
replied on February 23, 2022

Do you have all your references in place? Based on the code, it looks like you might be referencing System.Data.ODBC. Is that present on the remote agent machine? Is the same version you referenced in this project?

0 0
replied on February 23, 2022

Rather than referencing a DLL for System.Data.ODBC, I installed the NuGet package in Visual Studio. This allows me to include System.Data.ODBC just as I would include any other built in namespace without referencing any additional files. It does not output a seperate DLL file in the bin folder when I build. I still copy everything in the bin folder on build though.

In my second project (where I get the constructor error), since I am using a third party DLL, I referenced it and it appears in the bin folder when I build the project. I also copy everything including the referenced DLL in the bin folder.

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.