Dynamics 365 – Use External DLLs in SSIS

By | February 13, 2018

Introduction:

No doubt SSIS is widely used tool for Migration and Integration in all areas, Dynamics 365 CRM is no exception. We have been using SSIS in our number of Dynamics 365 Migration and Integration project.

Recently, we received a business requirement where we have to use SSIS script component and need to connect to CRM and perform CRUD operation. So to connect to Dynamics CRM and perform the operations we need to add reference of below DLL in script component code.

Microsoft.Xrm.Sdk.dll

Microsoft.Crm.Sdk.Proxy.dll

Microsoft.Xrm.Sdk.Deployment.dll

Microsoft.IdentityModel.Clients.ActiveDirectory.dll

Microsoft.Xrm.Tooling.Connector.dll

But adding reference was not enough, further, we need to register all the above DLLs in GAC. But the client doesn’t want to register DLL in GAC because of some other issues. When we remove the DLL from GAC and execute the SSIS package then it started throwing following Error.ssis1

After some research and play around we found the solution to resolve this issue.

For this, first, we created one folder and copy all DLLs in that folder or give reference from project bin folder where we need to keep all DLLs and then add following code in script component.

static ScriptMain()
    {
        AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
    }

    ///// <summary>
    ///// This function is used to load the DLL 
    ///// </summary>
    ///// <param name="sender"></param>
    ///// <param name="args"></param>
    ///// <returns></returns>
    static System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
    {

        try
        {
            if (args.Name.Contains("Microsoft.Crm.Sdk.Proxy"))
            {
                string path = @"C:\DLL";
                return System.Reflection.Assembly.LoadFile(System.IO.Path.Combine(path, "Microsoft.Crm.Sdk.Proxy.dll"));
            }

            else if (args.Name.Contains("Microsoft.Xrm.Sdk"))
            {
                string path = @"C:\DLL";
                return System.Reflection.Assembly.LoadFile(System.IO.Path.Combine(path, "Microsoft.Xrm.Sdk.dll"));
            }

            else if (args.Name.Contains("Microsoft.Xrm.Sdk.Deployment"))
            {
                string path = @"C:\DLL";
                return System.Reflection.Assembly.LoadFile(System.IO.Path.Combine(path, "Microsoft.Xrm.Sdk.Deployment.dll"));
            }
            else if (args.Name.Contains("Microsoft.Xrm.Tooling.Connector"))
            {
                string path = @"C:\DLL";
                return System.Reflection.Assembly.LoadFile(System.IO.Path.Combine(path, "Microsoft.Xrm.Tooling.Connector.dll"));
            }
            else if (args.Name.Contains("Microsoft.IdentityModel.Clients.ActiveDirectory"))
            {
                string path = @"C:\DLL";
                return System.Reflection.Assembly.LoadFile(System.IO.Path.Combine
               (path, "Microsoft.IdentityModel.Clients.ActiveDirectory.dll"));
            }
            else if (args.Name.Contains("Microsoft.IdentityModel.Clients.ActiveDirectory.WindowsForms"))
            {
                string path = @"C:\DLL";
                return System.Reflection.Assembly.LoadFile(System.IO.Path.Combine
                (path, "Microsoft.IdentityModel.Clients.ActiveDirectory.WindowsForms.dll"));
            }
            
            else
            {
                return null;
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

If DLLs don’t exist on specified path and we execute package then it throws the error. So make sure you keep all DLLs at a specified path.

Also be sure you keep the sequence of load DLL as shown above. If the sequence of a line of code is different then it will throw the below error.

 ssis2

Conclusion:

The above method helps to use any External DLL in SSIS script component without registering it in GAC.

Dynamics CRM Maps