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



我有一个c#应用程序。当我从visualstudio以正常方式运行这个应用程序时,或者双击.exe文件时,一切都是文件,工作正常。但是我必须从sql服务器执行.exe文件。我正在使用xp_cmdshell,并且出现以下错误:

未处理的异常:System.IO.FileNotFoundException:无法加载文件或程序集'Newtonsoft.Json,版本=7.0.0.0,区域性=中性,PublicKeyToken=30ad4fe6b2a6eed'或其依赖项之一。这个系统找不到指定的文件。

我的应用程序代码是这个

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

您需要服务器上可用的Newtonsoft.json.dll

简单:将bin文件夹中的所有文件与可执行文件一起保留,确保Newtonsoft.Json 的引用属性"Copy Local"设置为true

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

不知道如何将dll添加到GAC?查看本文,了解如何使用Powershell

首先尝试使用..重新安装库。。更新包Newtonsoft.Json-重新安装

你好,这里是对我有效的所有步骤:

首次卸载/重新安装程序包打开Package Manager控制台

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

获取DDl文件夹

包对象浏览器

获取路径:

PAth DDL

通过以管理员身份打开Power Shell注册DLL,并使用您的Path 运行此DLL

Set-location "C:WindowsMicrosoft.NetassemblyGAC_MSILNewtonsoft.Jsonv4.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:WindowsMicrosoft.NetassemblyGAC_MSILNewtonsoft.Jsonv4.0_13.0.0.0__30ad4fe6b2a6aeedNewtonsoft.Json.dll")   

为我工作

感谢

相关内容

  • 没有找到相关文章

最新更新