gpt4 book ai didi

c# - Linux 系统在 .NET Core 应用程序上引发错误

转载 作者:太空宇宙 更新时间:2023-11-04 05:08:24 24 4
gpt4 key购买 nike

我在 .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);
}
}

有人知道怎么回事吗?

最佳答案

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

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

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

依赖框架和独立框架之间的差异可以在这里找到:https://learn.microsoft.com/en-us/dotnet/core/deploying

关于c# - Linux 系统在 .NET Core 应用程序上引发错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58399867/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com