Could not load file or assembly Newtonsoft.json. The system cannot find the file specified

I have an c# app. When i am running this app normal way from visual studio or using double click to the .exe file everything is file and working correct. But i have to execute the .exe file file from sql server. I am using xp_cmdshell and i have this error:

Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'Newtonsoft.Json, Version=7.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The system cannot find the file specified.

My app code is this

using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace AndroidParse
{
    class Program
    {

        static void Main(string[] args)
        {
            //SqlDataReader reader;
            SqlConnection conn = new SqlConnection();

            string queryString = "select device_id from Requests where ID_Requests = 'xxx'";
            string connectionString = "Data Source=xxx\\SQLEXPRESS;" +
            "Initial Catalog=xxx;" +
            "User id=xxx;" +
            "Password=xxx;";
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                SqlCommand command = new SqlCommand(queryString, connection);
                connection.Open();
                SqlDataReader reader = command.ExecuteReader();
                try
                {
                    while (reader.Read())
                    {
                        Console.WriteLine(reader[0]);

                        bool isPushMessageSend = false;

                        string postString = "";
                        string urlpath = "https://api.parse.com/1/push";
                        var httpWebRequest = (HttpWebRequest)WebRequest.Create(urlpath);

                        postString = "{\"data\": { \"alert\": \"Finally is working\" },\"where\": { \"device_id\": \"" + reader[0] + "\" }}";
                        httpWebRequest.ContentType = "application/json";
                        httpWebRequest.ContentLength = postString.Length;
                        httpWebRequest.Headers.Add("X-Parse-Application-Id", "xxx");
                        httpWebRequest.Headers.Add("X-Parse-REST-API-KEY", "xxx");
                        httpWebRequest.Method = "POST";

                        StreamWriter requestWriter = new StreamWriter(httpWebRequest.GetRequestStream());
                        requestWriter.Write(postString);
                        requestWriter.Close();
                        var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();


                        using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                        {
                            var responseText = streamReader.ReadToEnd();
                            JObject jObjRes = JObject.Parse(responseText);
                            if (Convert.ToString(jObjRes).IndexOf("true") != -1)
                            {
                                isPushMessageSend = true;
                            }
                        }

                    }
                }
                finally
                {
                    reader.Close();
                }
            }


        }

        private static void println()
        {
            throw new NotImplementedException();
        }
    }
}

Upvotes: 1

Views: 3762

Answers (3)

CaribouHunter
CaribouHunter

Reputation: 1

Hello here is all the steps that worked for me :

First uninstall / reinstall Package Open Package Manager Console

Uninstall-Package Newtonsoft.Json -Force
Install-Package Newtonsoft.Json

Get the DDl Folder

Package object Browser

Get the Path :

PAth DDL

Register the DLL by Opening Power Shell as admin and run this with your Path

Set-location "C:\Windows\Microsoft.Net\assembly\GAC_MSIL\Newtonsoft.Json\v4.0_13.0.0.0__30ad4fe6b2a6aeed"            
[System.Reflection.Assembly]::Load("System.EnterpriseServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")            
$publish = New-Object System.EnterpriseServices.Internal.Publish            
$publish.GacInstall("C:\Windows\Microsoft.Net\assembly\GAC_MSIL\Newtonsoft.Json\v4.0_13.0.0.0__30ad4fe6b2a6aeed\Newtonsoft.Json.dll")   

Worked for me

thanks

Upvotes: 0

aghost
aghost

Reputation: 192

You need the Newtonsoft.json.dll available on the server

Simple: Keep all the files from the bin-folder along with the executable, make sure the reference property "Copy Local" is set to true for Newtonsoft.Json

Advanced: Alternatively, add the Newtonsoft.Json.dll to the Global Assembly Cache (GAC)

Don't know how to add a dll to the GAC? Check this article out to learn how to do it using Powershell

Upvotes: 1

Nitin
Nitin

Reputation: 107

First try to reinstall library using.. Update-Package Newtonsoft.Json -Reinstall

Upvotes: 0

Related Questions