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 ์š”์ฒญ, ์•ก์…˜ 2 callback, String httpMethod, Func 4 getWebRequest)
RestSharp.RestClient.ExecuteAsync(IRestRequest ์š”์ฒญ, Action`2 ์ฝœ๋ฐฑ, ๋ฉ”์„œ๋“œ httpMethod)

๋ชจ๋“  20 ๋Œ“๊ธ€

@vbenedichuk PR์„ ์ œ์ถœํ•˜๊ณ  ์‹ถ์ง€ ์•Š์€ ์ด์œ ๊ฐ€ ์žˆ์Šต๋‹ˆ๊นŒ?

๋‹ค์Œ ๋ฆด๋ฆฌ์Šค์—์„œ ๋‹ซํž™๋‹ˆ๋‹ค. ํš๊ธฐ์ ์ธ ๋ณ€ํ™”!

๋ฌธ์ œ๋ฅผ ๋””๋ฒ„๊ทธ/์ˆ˜์ •ํ•˜๋ ค๊ณ  ์‹œ๋„ํ–ˆ์ง€๋งŒ ๋ณ€๊ฒฝ ์‚ฌํ•ญ์œผ๋กœ ์ธํ•ด ๋ฒ„๊ทธ๊ฐ€ ๋ฐœ์ƒํ•œ ๊ฒƒ์œผ๋กœ ๋‚˜ํƒ€๋‚ฌ์Šต๋‹ˆ๋‹ค. https://github.com/restsharp/RestSharp/blob/a8a9f3473be30317fc449c1c47e3f3cd74e8bab4/RestSharp/RestRequest.cs#L335 ๋ผ์ธ

๋ผ์ธ์ด ๊ฐ์ฒด๋ฅผ ์ƒˆ๋กœ ์ƒ์„ฑํ•˜์ง€๋งŒ ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ์— ํ•„์š”ํ•œ ์ด๋ฆ„์„ ์ œ๊ณตํ•˜์ง€ ์•Š๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

https://github.com/restsharp/RestSharp/blob/a8a9f3473be30317fc449c1c47e3f3cd74e8bab4/RestSharp/Parameter.cs#L28 -L35

https://github.com/restsharp/RestSharp/blob/a8a9f3473be30317fc449c1c47e3f3cd74e8bab4/RestSharp/Validation/Ensure.cs#L13 -L17

์ œ์ถœ๋œ ํ’€ ๋ฆฌํ€˜์ŠคํŠธ:
https://github.com/restsharp/RestSharp/pull/1337

๋ช‡ ๊ฐ€์ง€ ํ…Œ์ŠคํŠธ๊ฐ€ ๋ˆ„๋ฝ๋œ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์‹ค์ œ๋กœ ๋ณธ๋ฌธ ๋งค๊ฐœ๋ณ€์ˆ˜์—๋Š” ์ด๋ฆ„์ด ํ•„์š”ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋” ๋‚˜์€ ์ˆ˜์ • ์‚ฌํ•ญ์€ ๋ณธ๋ฌธ ์œ ํ˜•์ด ์•„๋‹Œ ๋งค๊ฐœ ๋ณ€์ˆ˜์— ๋Œ€ํ•ด ์ด๋ฆ„์ด ๋น„์–ด ์žˆ์ง€ ์•Š์€์ง€ ํ™•์ธํ•˜๋Š” ๊ฒƒ๋ฟ์ด๋ผ๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค.

๊ท€ํ•˜์˜ ์•„์ด๋””์–ด๋ฅผ ๋ฐ˜์˜ํ•˜๊ธฐ ์œ„ํ•ด ํ’€ ๋ฆฌํ€˜์ŠคํŠธ๋ฅผ ์—…๋ฐ์ดํŠธํ–ˆ์Šต๋‹ˆ๋‹ค.

RestSharp๋ฅผ ๋” ์ž˜ ์ดํ•ดํ•˜๋ ค๊ณ  ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ์ด ๋ฌธ์ œ๋ฅผ ๋ฐœ๊ฒฌํ–ˆ์Šต๋‹ˆ๋‹ค. ํ˜„์žฌ nuget ํŒจํ‚ค์ง€์˜ ๋ฌธ์ œ๋Š” ์•„๋‹Œ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

์˜ˆ, ์ƒˆ ๋ฒ„์ „์€ ์•„์ง ๊ฒŒ์‹œ๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค. ๋” ๋‚˜์€ ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ๋กœ ์ด๋™ํ•˜๊ณ  ์˜ˆ์™ธ ์ฒ˜๋ฆฌ์™€ ๊ด€๋ จ๋œ ์ผ๋ถ€ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ๋ช‡ ๊ฐ€์ง€ ์ถ”๊ฐ€ ๋ณ€๊ฒฝ ์‚ฌํ•ญ์„ ์ ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค.

์–ธ์ œ ๊ณ ์ณ์งˆ๊นŒ?

๋ฒ„์ „ 106.7.0์ด ์žˆ๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์ง€๋งŒ ๋‚ด Visual Studio๋Š” 106.6.10๋งŒ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค. ์ด์œ ๋Š” ๋ฌด์—‡์ž…๋‹ˆ๊นŒ?

106.9์—์„œ ์ˆ˜์ •๋จ

์ด๊ฒƒ์€ ์ตœ์‹  ๋นŒ๋“œ Alexey์—์„œ ๋ฌธ์ œ๋กœ ๋‚จ์•„ ์žˆ์Šต๋‹ˆ๋‹ค.

@ronaldfeb ์ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•œ๋‹ค๊ณ  ์ฃผ์žฅํ•˜๋Š” PR์„ ๋ณ‘ํ•ฉํ–ˆ์Šต๋‹ˆ๋‹ค. ์ฝ”๋“œ๋ฅผ ๊ฒŒ์‹œํ•ด ์ฃผ์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ?

@alexeyzimarev ์ €๋Š” JSON ๊ฐ์ฒด๋ฅผ ๋ณธ๋ฌธ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฐ„๋‹จํ•œ ํฌ์ŠคํŠธ ํ•จ์ˆ˜๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋ณธ๋ฌธ์˜ ์ด๋ฆ„ ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ null์ž…๋‹ˆ๋‹ค.

        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 ๊ฐ€์ž… = ์ƒˆ๋กœ์šด MemberSignUp()
{
์ด๋ฉ”์ผ = "ํ…Œ์ŠคํŠธ",
๋ณ„๋ช… = "ํ…Œ์ŠคํŠธ",
};
RestRequest ์š”์ฒญ = ์ƒˆ๋กœ์šด RestRequest("/my/resource", Method.POST);

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

๊ทธ๋Ÿฌ๋‚˜ -> req.Parameters[1].Name = ๋น„์–ด ์žˆ์Šต๋‹ˆ๋‹ค.

q.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_์ž…๋‹ˆ๋‹ค. ๋งค๊ฐœ๋ณ€์ˆ˜ ์ด๋ฆ„์ด null์ธ ๋‹ค๋ฅธ ๋ฌธ์ œ๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ ๋ณ„๋„์˜ ๋ฌธ์ œ๋ฅผ ์—ด์–ด์ฃผ์„ธ์š”.

์ฃผ์š” ๋ฒ„์ „์„ ๋ณ€๊ฒฝํ•˜์ง€ ์•Š๊ณ  ์ฃผ์š” ๋ณ€๊ฒฝ ์‚ฌํ•ญ(Parameter ํด๋ž˜์Šค์˜ ๋งค๊ฐœ ๋ณ€์ˆ˜ ์—†๋Š” ์ƒ์„ฑ์ž ์ œ๊ฑฐ)์„ ์–ด๋–ป๊ฒŒ ์ถ”๊ฐ€ํ–ˆ์Šต๋‹ˆ๊นŒ?
๋‚˜๋Š” 106.2.1์—์„œ ์ตœ์‹  ๋ฒ„์ „์œผ๋กœ ์—…๋ฐ์ดํŠธ๋ฅผ ์‹œ๋„ํ–ˆ๋Š”๋ฐ, ๋ณ€๊ฒฝ ์‚ฌํ•ญ์„ ๊นจ๋œจ๋ฆฌ์ง€ ์•Š๊ณ  ๋™์ผํ•œ ๋ฉ”์ด์ €(106)๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์ง€๋งŒ ๊ทธ๋ ‡์ง€๋Š” ์•Š์Šต๋‹ˆ๋‹ค!

@bsalmeida ๊ทธ๋ ‡๊ตฐ์š” . ์ด์ƒ์ ์œผ๋กœ๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜ ํด๋ž˜์Šค๊ฐ€ ๋‚ด๋ถ€์— ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์‚ฌ๋žŒ๋“ค์ด ์—ฌ๊ธฐ์— ๊ฒŒ์‹œํ•˜๋Š” ๋” ๋งŽ์€ ๋ฌธ์ œ๋ฅผ ์ƒ์„ฑํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ๊ตฌ์„ฑ๋˜์ง€ ์•Š์€ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ˆ˜๋ฝํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

์ด ํŽ˜์ด์ง€๊ฐ€ ๋„์›€์ด ๋˜์—ˆ๋‚˜์š”?
0 / 5 - 0 ๋“ฑ๊ธ‰