You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET 7 C#应用偶现RestSharp.RestClient构造方法未找到异常求助

解决RestSharp 110.2.0下System.MissingMethodException非必现问题

问题现象

应用抛出非必现异常:

System.MissingMethodException: Method not found: 'Void RestSharp.RestClient..ctor(System.String)'.
at .....service.http.HttpCommunication.HttpAsync(Method httpVerb, String hostUrl, Dictionary2 headers, Object requestObject, Func2 logFunction)

当前使用RestSharp版本110.2.0,异常触发时API通信及HttpRequest构造终止。

根因分析

RestSharp从107.x版本开始,已移除接收string类型参数的RestClient构造函数,仅保留基于Uri或RestClientOptions的构造重载。非必现异常通常由以下场景导致:

  • 项目依赖冲突:解决方案内不同项目引用了不同版本的RestSharp,部署时旧版本DLL覆盖了110.2.0版本
  • 部署环境残留:服务器上存在旧版本RestSharp DLL,应用加载时优先读取了旧文件
  • 动态加载问题:插件或动态加载的程序集使用了低版本RestSharp,导致跨版本调用不兼容

解决方案

  1. 替换构造函数调用
    将代码中旧的构造方式:

    var client = new RestClient(hostUrl);
    

    替换为以下任一兼容写法:

    // 使用Uri参数
    var client = new RestClient(new Uri(hostUrl));
    // 使用RestClientOptions(推荐,支持更多配置)
    var options = new RestClientOptions { BaseUrl = new Uri(hostUrl) };
    var client = new RestClient(options);
    
  2. 统一依赖版本
    在所有引用RestSharp的项目文件中明确指定版本,并设置私有资产避免版本泄漏:

    <PackageReference Include="RestSharp" Version="110.2.0" PrivateAssets="all" />
    

    执行以下命令清理NuGet缓存并重新还原:

    dotnet nuget locals all --clear
    dotnet restore
    
  3. 校验部署环境
    检查服务器应用目录下的RestSharp.dll版本,确保是110.2.0。删除目录中所有旧版本的RestSharp相关文件后重新部署。

  4. 处理动态加载场景
    若存在插件或动态加载程序集,确保这些模块也引用110.2.0版本的RestSharp。对于.NET 7项目,可在项目文件中启用自动绑定重定向:

    <PropertyGroup>
      <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
      <GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
    </PropertyGroup>
    

内容的提问来源于stack exchange,提问作者Caio César Lorensetti

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 03:34:55