3

This is a possible duplicate question of this post, however my error is different and far less readable.

Right now, I have an SSIS package that only contains a script task. Inside of that script task, I have Microsoft.Exchange.WebServices.dll referenced and I have it included in the namespace.

Whenever I don't take use of any of the methods and classes defined in WebServices, the script runs perfectly. However, whenever I do make use of them, the entire script fails and won't even make it to a break point on the first line... It also throws the following error:

at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments) at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams) at Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTATaskScriptingEngine.ExecuteScript()

Any ideas? When I run my same code outside of this SSIS script task (in its own project) it works fine.

UPDATE

I'm updaing this post to include more potentially useful information. The code in my Script Task looks like this:

public void Main()
{
    string _SharedBox = "user@domain.com";
    ExchangeService service = new ExchangeService();
    service.AutodiscoverUrl(_SharedBox);
    service.UseDefaultCredentials = true;
    //... goes on, but the rest is commented out for now.
}

I think that Microsoft.Exchange.WebServices might not be included properly and that's the issue, however I'm not sure what's wrong with it. I have the DLL sitting inside of the VS solution but outside of the SSIS project. I have then added it as a reference in the Script Task - which is the part I'm not sure about. I initially tried including WebServices via a NuGet package, however, I've come to realize that you can't use NuGet in a Script Task. Here's what my solution looks like in the Script Task:

enter image description here

Thanks for any help in advance!

Hadi
  • 36,233
  • 13
  • 65
  • 124
Darryl Huffman
  • 2,499
  • 3
  • 18
  • 40
  • `the entire script fails and won't even make it to a break point on the first line` means that the script fails during the validation phase (or PreExecute phase), you must provide the script code and the real error message since what you provided is the error StackTrace not the message. – Hadi Mar 12 '19 at 21:49
  • @Hadi the only other message I get when I run it is: Exception has been thrown by the target invocation. I'm not sure where else I can look for an error that this script is producing. I'll update my post and add the code in my script task. – Darryl Huffman Mar 13 '19 at 12:53
  • @DarrylHuffman just move `service.UseDefaultCredentials = true;` before `service.AutodiscoverUrl(_SharedBox);` as mentioned in the provided answer – Yahfoufi Mar 14 '19 at 15:11

1 Answers1

0

Update 1

Two things to try:

  • Run the package in 32-bit mode (since it maybe that only 32 bit assemblies are registered in GAC.)
  • Set the Microsoft.Exchange.WebServices.dll Copy Local property to True

Reading the main exception

Based on your comments the exception thrown is:

Exception has been thrown by the target invocation

Is a general error message that is shown when an exception is thrown by the script code. To read the main error message you can add a try catch clause into your code and use Dts.FireError() method to throw the real exception.

public void Main()
{
    try{

        string _SharedBox = "user@domain.com";
        ExchangeService service = new ExchangeService();
        service.AutodiscoverUrl(_SharedBox);
        service.UseDefaultCredentials = true;
        //... goes on, but the rest is commented out for now.
        Dts.TaskResult = (int)ScriptResult.Success;

    }catch(Exception ex){

        Dts.FireError(0,"An error occured", ex.Message,String.Empty, 0);
        Dts.TaskResult = (int)ScriptResult.Failure;

    }


}

Trying to figure out the issue

After searching for a while, i think that the exception is thrown on the following line:

service.AutodiscoverUrl(_SharedBox);

Try to put service.UseDefaultCredentials = true; before service.AutodiscoverUrl(_SharedBox); since the credentials must be defined before using AutoDiscoverUrl() method.

Also, you can check to following links it can gives you more information on reading email using ExchangeService:

Hadi
  • 36,233
  • 13
  • 65
  • 124
  • Thanks for your help. Unfortunately, I don't believe that's the problem... It doesn't seem to execute far enough to even make it into the exception. It makes me wonder if I'm able to add the reference to the DLL in an SSIS Script Task. The moment I use any methods from the Microsoft.Exchange.WebServices namespace, the task just throws the exception without any details. I tried adding `Dts.Events.FireError`, however, it doesn't seem to bubble anything up due to the script not really running. – Darryl Huffman Mar 14 '19 at 21:06
  • @DarrylHuffman Try running the package in 32bit mode maybe it is a 64bit issue dll is not registered – Hadi Mar 14 '19 at 21:35
  • @DarrylHuffman have you tried running package in 32-bit mode? – Hadi Mar 15 '19 at 20:52