简体   繁体   中英

SSIS Script Task with Microsoft.Exchange.WebServices

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:

在此输入图像描述

Thanks for any help in advance!

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:

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