4

I installed the dll to the GAC even updated the dll to install that into the GAC but I get this 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.":"Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed"}

App.config

<configuration>
   <runtime>
      <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
         <dependentAssembly>
            <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-12.0.0.0" newVersion="12.0.0.0" />
         </dependentAssembly>
      </assemblyBinding>
   </runtime>
</configuration>

GAC location:

C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Newtonsoft.Json\v4.0_12.0.0.0__30ad4fe6b2a6aeed\Newtonsoft.Json.dll

I also copied the dll to the C:\Windows\System32 but when I try to add it as a reference from this location visual studios doesn't see it.

stuartd
  • 70,509
  • 14
  • 132
  • 163
user3266638
  • 429
  • 8
  • 25

3 Answers3

9

Try using the ResolveEventHandler delegate to load the .dll when an error is raised after it not being found. This can go directly above the Main() method.

static ScriptMain()
{
 AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
}
static System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
    if (args.Name.ToUpper().Contains("NEWTONSOFT"))
    {
   string path = @"C:\DLL File Path\";
   return System.Reflection.Assembly.LoadFile(System.IO.Path.Combine(path, "Newtonsoft.Json.dll"));
    }
    return null;
}
public void Main()
{
    ...
userfl89
  • 4,610
  • 1
  • 9
  • 17
  • I was able to resolve my issue locally but when I test this on the server I'm sure this may come in hand. – user3266638 Mar 08 '19 at 17:34
  • So, when I set up my SSIS package on the server our DBAs told me that we don't allow installing of Dlls on our SQL server. I deployed my dlls to another server and I referenced them using this and a network path and it worked. – user3266638 Mar 09 '19 at 05:31
  • I do have one question. What would be the correct way to use this code to load 2 different dlls? I got it to work, but I registered 2 different methods to there ResolveEventHandler delegate. It works but is that the right way? – user3266638 Mar 09 '19 at 05:32
  • Glad to hear the issue was resolved. As far as the proper way to handle this, just make sure the errors that addressed are for the exact .dll that they correspond to, which can be confirmed by testing for the exceptions that are raised when more than one occurs, and also the correct file paths are supplied. You’re probably already aware of this, but ToUpper was used to avoid any upper/lower case discrepancies. – userfl89 Mar 09 '19 at 19:15
1

To fix this issue I added a new script task and opened nugget from inside it.

I installed the Microsoft.AspNet.WebApi.Client package and then from this package added the System.Net.Http and Newtonsoft.Json dlls to my GAC.

After that I referenced those new dlls from the location it added them into and this fixed the issue.

This will only work if you are able to install the dll into the GAC on the server/computer running your SSIS package.

user3266638
  • 429
  • 8
  • 25
0

I understand the solution has been found but I thought I'd share another solution that works for me. Here is how I solve it using Visual Studio 2017 and Netwonsoft version 13.0.1

Pre-requisite: Make sure you have installed the Newtonsoft package in your SSIS, Script Task.


I hope this helps anyone as much as it helps me. Anyway, I started learning C# and SSIS 3 weeks ago, I know it works but I can't explain much. Would appreciate your kind feedback and knowledge sharing.


Verify the issue

From what I understand, every component or tasks references a dll file to operate. The file can be found in "GAC_MSIL"

So if you do not see "Newtonsoft.Json" folder after installing NewtonSoft, it means the problem lies here. You will need to install using gacutils to get this working or via the solution above.

Fixing the issue

1. Open Powershell as Administrator
2. Go to the nuget package folder (See Below)
3. Key in the following:
    a. e.g "C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.7 Tools\gacutil.exe" /i Newtonsoft.Json.dll

Install Assembly into GAC


Where to find GAC_MSIL folder

• .NET 1.0 - NET 3.5: c:\windows\assembly (%systemroot%\assembly)
• .NET 4.x: %windir%\Microsoft.NET\assembly

From How to view the Folder and Files in GAC?

Where to find gacutil.exe

   e.g C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin

From where is gacutil.exe?

Where to find the nuget package?

  e.g C:\Users\username\\.nuget\packages\newtonsoft.json\13.0.1\lib\net45

Look for a DLL file

Muzamir
  • 88
  • 1
  • 8