在C#visual studio中的API自动化测试中,请求主体返回null

在C#visual studio中的API自动化测试中,请求主体返回null,c#,visual-studio,api,automation,C#,Visual Studio,Api,Automation,这是我的密码 public static RestRequest CreateReportRequest() { restRequest = new RestRequest(Method.POST); restRequest.AddHeader("Content-Type", "application/json"); restRe

这是我的密码

        public static RestRequest CreateReportRequest()
        {            
            restRequest = new RestRequest(Method.POST);
            restRequest.AddHeader("Content-Type", "application/json");
            restRequest.AddHeader("Authorization", "Basic T1NUAxNFVxeTIwag==");
            using (StreamReader file = 
File.OpenText(@"C:\Automation\APIAutomationSuite\APIAutomationSuite\TestData\CreateReport.json"))
            using (JsonTextReader reader = new JsonTextReader(file))
            {
                JObject Body = (JObject)JToken.ReadFrom(reader);                    
            }            
            restRequest.AddJsonBody(Body);
            var response = client.Execute(restRequest);
            return restRequest;
        }
当我调试代码时,我在“JObject body”中看到json请求正文值,但当按F10并移动到下一条语句(restRequest.AddJsonBody(body);)时,正文值变为null。这会抛出错误的请求错误。
请帮我解决这个问题。

您的
主体
变量是在
的内部范围内使用
语句声明的。因此,当您试图使用在
之外引用它时,它是未声明的

您可以使用
范围将方法的结尾完全移动到
,我想这将消除您的问题。例如:

...
using (JsonTextReader reader = new JsonTextReader(file))
{
    JObject Body = (JObject)JToken.ReadFrom(reader);     
    restRequest.AddJsonBody(Body);
    var response = client.Execute(restRequest);
    return restRequest;               
}            

您的
主体
变量是在
的内部范围内使用
语句声明的。因此,当您试图使用
之外引用它时,它是未声明的

您可以使用
范围将方法的结尾完全移动到
,我想这将消除您的问题。例如:

...
using (JsonTextReader reader = new JsonTextReader(file))
{
    JObject Body = (JObject)JToken.ReadFrom(reader);     
    restRequest.AddJsonBody(Body);
    var response = client.Execute(restRequest);
    return restRequest;               
}            

在using语句之外声明“JObject Body”。它可能在using语句之外丢失了scopedeclare“JObject Body”。它可能会失去作用范围