9

I have added third party reference (Json newtonsoft) dll in my script component (using edit script option), but when i run the package, I am getting an error

Could not load file or assembly 'Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The system cannot find the file specified.

Any suggestions?

I will not be able to add the dll in GAC.

I am using SQL Server 2008.

HashCoder
  • 946
  • 1
  • 13
  • 32
  • 1
    That is probably your problem then. I'm currently developing SSIS Control Flow Tasks and I found the same issue with my Task UI project. In the end I had to register it as I didn't find another solution. Is there a reason why you can't add it to the GAC? – zeencat Jul 23 '12 at 11:10
  • I can't do this in 2017 version, I posted a question [here](https://stackoverflow.com/q/56037106/1257607) – DanielV May 08 '19 at 09:07

2 Answers2

12

By "Running," I assume running from agent/command-line is failing? It should work from within BIDS/SSDT. The short answer is the DLL must be registered with the GAC or you can download the source code and add that project into the script task and then reference said project.

Looking at the project, it should be a strongly signed DLL (based on presences of Dynamic.snk) and thus capable of being added to the GAC. Oh, but you state you will not be able to add it into the GAC, implying it's a permission not a capability issue.

If that's the case, either compile the project in with the source or surround it with a web service wrapper and then reference the service.

I also saw this answer, seems you can try loading the references dynamically.

Community
  • 1
  • 1
billinkc
  • 59,250
  • 9
  • 102
  • 159
  • 2
    I have removed the reference of newtonsoft dll and used .net's JavaScriptSerializer class to serialize. – HashCoder Jul 24 '12 at 12:28
9

You can using Reflection to load dll at runtime from file system without needing to install in GAC . This is helpful if permission to install in GAC is not availaible .

//Add a Static Constructor which is guaranteed to be called exactly once
// “before the first instance is created or any static members are referenced.”, 
// so therefore before the dependent assemblies are loaded.

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

 //Provide path to dll stored in folder on file system
 static System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
 {

     string path = @"D:\DLL\";
     return System.Reflection.Assembly.LoadFile(System.IO.Path.Combine(path, "Newtonsoft.dll"));

  }

Ofcourse you need to also Add Reference to dll in script task .

Mudassir Hasan
  • 28,083
  • 20
  • 99
  • 133
  • Will try to do this too. – Tholitz_Reloaded Jul 08 '19 at 12:25
  • This is the common resolution for this problem. I have used this successfully. But I have an additional dimension to this problem. I want the DLLs to be loaded from different folder for DEV/TEST/UAT/PROD environment without having to change the code – Louis van Alphen May 13 '20 at 12:35
  • @user437239 is this still the only solution? this is so sad – fabsenet Jun 12 '20 at 07:41
  • it is also worth mentioning in case it hasn't been that any call to something inside a dll must be placed outside the main, as the resolving doesn't fire inside the main – Ab Bennett Mar 03 '21 at 22:35