无法加载文件或程序集Newtonsoft.json。系统找不到指定的文件

时间:2015-12-28 12:50:35

标签: c# .net json json.net

我有一个c#app。当我从visual studio正常运行这个应用程序或使用双击到.exe文件时,一切都是文件和工作正确。但我必须从sql server执行.exe文件。我使用xp_cmdshell,我有这个错误:

  

未处理的异常:System.IO.FileNotFoundException:无法加载   文件或程序集'Newtonsoft.Json,Version = 7.0.0.0,Culture = neutral,   PublicKeyToken = 30ad4fe6b2a6aeed'或其中一个依赖项。该   系统找不到指定的文件。

我的应用代码就是这个

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();
        }
    }
}

2 个答案:

答案 0 :(得分:1)

您需要服务器上提供的Newtonsoft.json.dll

简单:保留bin文件夹中的所有文件以及可执行文件,确保引用属性" Copy Local"对于Newtonsoft.Json

,设置为true

高级:或者,将Newtonsoft.Json.dll添加到全局程序集缓存(GAC)

不知道如何向GAC添加dll? Check this article out了解如何使用Powershell

进行操作

答案 1 :(得分:0)

首先尝试使用.. Update-Package Newtonsoft.Json -Reinstall

重新安装库