Restsharp: 没有名称的正文参数的 NullReferenceException

创建于 2019-04-05  ·  20评论  ·  资料来源: restsharp/RestSharp

预期行为

我不必添加名称,否则错误消息会更清楚地表明缺少参数名称。

实际行为

抛出 NullReferenceException。

重现问题的步骤

  1. 添加一个没有名字的请求体参数

这一定是 RestClient.ConfigureHttp 中的问题(假设名称在那里):

var parameterExists =
                    request.Parameters.Any(p =>
                        p.Name.Equals(defaultParameter.Name, StringComparison.InvariantCultureIgnoreCase)
                        && p.Type == defaultParameter.Type);

规格

  • 版本:106.6.9.0
  • 平台:.NET 框架
  • 子系统:

堆栈跟踪


System.NullReferenceException:未将对象引用设置为对象的实例。
在 RestSharp.RestClient.<>c__DisplayClass172_0。b__8(参数p)
在 System.Linq.Enumerable.Any TSource
在 RestSharp.RestClient.ConfigureHttp(IrestRequest 请求)
在 RestSharp.RestClient.ExecuteAsync(IRestRequest request, Action 2 callback, String httpMethod, Func 4 getWebRequest)
在 RestSharp.RestClient.ExecuteAsync(IRestRequest 请求,Action`2 回调,方法 httpMethod)

所有20条评论

@vbenedichuk你不想提交 PR 的任何理由?

在下一个版本中关闭。 突破性改变!

似乎我们错过了一些测试。 实际上,身体参数不需要名称。 我认为更好的修复方法只会检查名称是否为非 body 类型的参数的空。

我更新了拉取请求以反映您的想法。

我只是发现了这个问题,因为我试图更好地理解 RestSharp。 目前在 nuget 包中似乎不是问题。

是的,新版本还没有发布。 我进行了更多更改以准备转向更好的异步处理并解决一些异常处理问题

什么时候修复?

我可以看到有 106.7.0 版本,但我的 Visual Studio 只显示 106.6.10 为什么?

已在 106.9 中修复

这在最新版本的 Alexey 中仍然是一个问题

@ronaldfeb我合并了声称可以解决此问题的 PR,您能否发布您的代码?

@alexeyzimarev我只有一个简单的 post 函数,它接收一个 JSON 对象作为主体。 正文的名称参数为空。

        public async Task<Result> postAsync(object objectToUpdate, string apiEndPoint)
        {
            Result result = new Result();

            try
            {
                var client = new RestClient(apiEndPoint);

                var request = new RestRequest(apiEndPoint, Method.POST);
                request.OnBeforeDeserialization = resp => { resp.ContentType = "application/json"; };
                request.RequestFormat = DataFormat.Json;
                request.AddJsonBody(objectToUpdate);
                var response = await client.ExecuteTaskAsync(request);
                //var response = client.ExecuteDynamic(request);


                if (response.StatusCode != HttpStatusCode.Created)
                {

                    Console.WriteLine("Access Token cannot obtain, process terminate");
                    result.Success = false;
                    result.Message = response.ErrorMessage;
                    return result;

                }

                result.Success = true;
                result.Message = "OK";
                return result;

            }
            catch
            {
                throw;
            }

            // await Audit(_settings, new AuditEntry() { Action = EnumTypes.Actions.Add.ToString(), Area = "Testcase", AuditId = obj.key, DatetimeStamp = DateTime.Now, ClientId = userId, NewValue = obj, OldValue = null });
        }

json 主体:

{
  "smartloadId": "string",
  "clientReference": "string",
  "smsRecipientMsisdn": "string",
  "deviceId": "string",
  "productId": 0,
  "amount": 0,
  "pinless": true,
  "sendSms": true,
  "orderRefID": 0
}

您能否粘贴异常详细信息,包括堆栈跟踪?

@alexeyzimarev (在 106.9 中修复)
我的 RestSharp 版本 (106.10.1)

MemberSignUp 注册 = new MemberSignUp()
{
电子邮件 = "测试",
昵称 = "测试",
};
RestRequest req = new RestRequest("/my/resource", Method.POST);

        req.AlwaysMultipartFormData = true;
        req.AddHeader("content-Type", "multipart/form-data");
        req.AddJsonBody(signup);

但是 -> req.Parameters[1].Name = 是空的。

当我 req.AddJsonBody() 时如何设置参数名称

image

我有类似的错误
代码

string url = "http://????????????????????????????/reports/new/Report2_11_1?page=reports/new/Report2_11_1&rep_nd=04.03.2020&rep_nd_h=18&rep_nd_min=00&rep_kd=05.03.2020&rep_kd_h=18&rep_kd_min=00&&rep_nd_fact_time=&rep_nd_h_fact_time=00&rep_nd_min_fact_time=00&rep_kd_fact_time=&rep_kd_h_fact_time=00&rep_kd_min_fact_time=00&train_registration=1&kind_rep_type=1,2&four_report_save=4,5&usw=1007&ush=1108";
CookieContainer container = new CookieContainer();
RestClient client = new RestClient();
client.CookieContainer = container;

RestRequest request = new RestRequest(url, Method.GET);

错误

Unhandled exception. System.ArgumentNullException: Value cannot be null. (Parameter 'name')
   at RestSharp.RestRequest..ctor(String resource, Method method, DataFormat dataFormat)
   at RestSharp.RestRequest..ctor(String resource, Method method)

我尝试了不同的版本:106.10.1、106.10.0б、106.9.0
似乎是 url 工作良好

"http://????????????????????????????/reports/new/Report2_1_1?page=reports/new/Report2_1_1&rep_nd=01.01.2019&rep_nd_h=0
0&rep_nd_min=00&rep_kd=05.03.2019&rep_kd_h=23&rep_kd_min=59&rep_status=0,1,2,4,5,6,7,10&state_among_road=close&rep_stat
us_among_road=0,1,2,4,6,7,10&rep_flg_add_inv=0&rep_flg_add_inv_among=0&rep_conseq=null&rep_character=null&kind_rep_type
=3&rep_service=null&rep_subdivision=null&rep_nod0=null&rep_place0=null&rep_place0_id=null&rep_stan0_1_id=null&rep_stan0
_2_id=null&rep_place0_value=null&rep_object=null&nod_place=null&rep_asu=0,1,2,5,7,11,12,14,15,20,21&rep_asu_dop=1:1&flg
_alien_service=2&usw=1578&ush=705"

@veliev这是一个不同的问题,请另开一个

好的,请求体参数需要名字的问题是_fixed_。 如果参数名称为空还有其他问题,请单独开一个 issue。

您是如何在不更改主要版本的情况下添加重大更改(删除 Parameter 类的无参数构造函数)的?
我尝试从 106.2.1 更新到最新版本,假设它具有相同的主要 (106) 而不破坏更改但不是!

@bsalmeida那样。 理想情况下,参数类应该是内部的。 我们不能接受未正确配置的参数,因为它会引起人们在此处发布的更多问题。

此页面是否有帮助?
0 / 5 - 0 等级