1. 我通过nuget引入了一个restSharp的依赖
2. 点击按钮的代码(其实就是调用restsharp相关代码):
private void button1_Click(object sender, EventArgs e)
{
// 设置 Basic Auth 的用户名和密码
string username = "your_username";
string password = "your_password";
// 设置请求的 URL
var options = new RestClientOptions("https://api.twitter.com/1.1")
{
Authenticator = new HttpBasicAuthenticator("username", "password")
};
var client = new RestClient(options);
var request = new RestRequest("statuses/home_timeline.json");
// The cancellation token comes from the caller. You can still make a call without it.
var response = client.ExecuteAsync(request).Result;
MessageBox.Show(response.Content);
}
3. 我程序dll叫做WindowsFormsApp1.dll
## 问题
点击按钮后,报错,System.Runtime.CompilerServices.Unsafe.dll这个文件已经在WindowsFormsApp1.dll同级目录上:
************** 异常文本 **************System.AggregateException: 发生一个或多个错误。 —> System.IO.FileNotFoundException: 未能加载文件或程序集“System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”或它的某一个依赖项。系统找不到指定的文件。在 System.Threading.Tasks.ValueTask.get_IsCompleted()在 RestSharp.RestClient.<ExecuteRequestAsync>d__3.MoveNext()— 引发异常的上一位置中堆栈跟踪的末尾 —在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)在 RestSharp.RestClient.<ExecuteAsync>d__0.MoveNext()— 内部异常堆栈跟踪的结尾 —在 System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)在 WindowsFormsApp1.Form1.button1_Click(Object sender, EventArgs e) 位置 D:\Project\C#\CAD\WindowsFormsApp1\Form1.cs:行号 45在 System.Windows.Forms.Control.OnClick(EventArgs e)在 System.Windows.Forms.Button.OnClick(EventArgs e)在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)在 System.Windows.Forms.Control.WndProc(Message& m)在 System.Windows.Forms.ButtonBase.WndProc(Message& m)在 System.Windows.Forms.Button.WndProc(Message& m)在 System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)—> (内部异常 #0) System.IO.FileNotFoundException: 未能加载文件或程序集“System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”或它的某一个依赖项。系统找不到指定的文件。文件名:“System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”在 System.Threading.Tasks.ValueTask.get_IsCompleted()在 RestSharp.RestClient.<ExecuteRequestAsync>d__3.MoveNext()— 引发异常的上一位置中堆栈跟踪的末尾 —在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)在 RestSharp.RestClient.<ExecuteAsync>d__0.MoveNext()
## 临时解决方案
将System.Runtime.CompilerServices.Unsafe.dll文件放到cad根目录就可以正确运行
请问有没有好的解决方案呢,十分感谢!