【问题标题】:Linux system throws an error on a .NET Core applicationLinux 系统在 .NET Core 应用程序上引发错误
【发布时间】:2020-02-12 10:35:30
【问题描述】:

我在 .NET Core 中为 linux 编写了一个控制台应用程序。当我尝试在我的 linux vps 上运行它时,它会抛出该错误:

Could not resolve type with token 01000010
  at Lumo.Program.Main (System.String[] args) [0x00039] in <dff92c70c42444648f6bf1be28aa709c>:0
[ERROR] FATAL UNHANDLED EXCEPTION: System.TypeLoadException: Could not resolve type with token 01000010
  at Lumo.Program.Main (System.String[] args) [0x00039] in <dff92c70c42444648f6bf1be28aa709c>:0

这是我的主要 C# 代码:

string city;
 Console.WriteLine("lolyweather");
  Thread.Sleep(1000);
   Console.Write("Enter your city: ");
   city = Console.ReadLine();
   string link = "https://api.openweathermap.org/data/2.5/weather?q=" + city + "&appid=" + API_KEY + "&units=metric&cnt=6";
 GetWeather(link, city);

这里是 GetWeather 函数:

 public static void GetWeather(string url, string city)
        {
            HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
            httpWebRequest.Method = "GET";
            try
            {
                using (WebResponse response = httpWebRequest.GetResponse())
                {
                    HttpWebResponse httpResponse = response as HttpWebResponse;
                    using (StreamReader reader = new StreamReader(httpResponse.GetResponseStream()))
                    {
                        var json = reader.ReadToEnd();
                        var obj = JsonConvert.DeserializeObject<WD.RootObject>(json);
                        WD.RootObject weather = obj;


                        Console.WriteLine("There are " + weather.weather[0].main.ToString() + " in {0}", city);
                        Console.WriteLine("There are {0} \u00B0C.", Math.Round(weather.main.temp));
                        Console.WriteLine("Max temperature: {0}, Minimum temperature: {1}", Math.Round(weather.main.temp_max), Math.Round(weather.main.temp_min));
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Program crashed: {ex.Message} (Check if you wrote your city's name correctly!)");
                Thread.Sleep(1_000);
            }
        }

有人知道怎么回事吗?

【问题讨论】:

  • 删除您的 bin 和 obj 文件夹,重建。这行得通吗?
  • 我刚试过。可惜没有
  • csproj 文件中的 是什么?
  • @James &lt;TargetFramework&gt;netcoreapp2.2&lt;/TargetFramework&gt; 就是这个
  • 什么样的Linux VPS?请更具体。还有你用来运行应用程序的命令是什么。

标签: c# .net linux windows .net-core


【解决方案1】:

从您上次的评论看来,您构建了一个依赖于框架的 .NET 核心应用程序版本。您不能只上传 dll 并在不安装 .NET 运行时或创建自包含部署的情况下执行它。我建议您在 VPS 上安装 .NET 运行时,其中包括复制 dotnet publish 命令的所有输出,或者创建一个独立的部署作为发布步骤的一部分并上传。

试试这个命令并将输出复制到发布文件夹中,让我们知道它是否有效。

dotnet publish -c Release -r linux-x64 --self-contained true

框架依赖和自包含之间的区别可以在这里找到:https://docs.microsoft.com/en-us/dotnet/core/deploying

【讨论】:

    猜你喜欢
    • 2020-05-28
    • 1970-01-01
    • 2021-11-06
    • 2020-01-19
    • 2019-10-04
    • 1970-01-01
    • 2017-06-23
    • 1970-01-01
    • 2019-01-28
    相关资源
    最近更新 更多