0

I have create a method in the service class that makes a call to another class to invoke the instance of webclient object. I am trying to perform Post operation using UploadString. However, when the method is called, I'm getting null reference exception. The webclient does not seems to be executing. Here's my code structure:

Service.cs

public static class Service
{
    private static readonly WebClientService _webClientService;
    private static async Task<string> ChangePassword(string ip, string pass)
    {
        _webClientService.UpdatePassword(serverIp, pass);
    }
}

WebClientService.cs

public class WebClientService
{
    private static readonly WebClient _client = new();
    private static string _getNewPwdUrl = 
        "https://execute-api.com/default/pwd?={0}/user/{1}";
    private static string _postNewPwdUrl = 
        "http://{0}/bin/link?cmd=chngpwd&oldpwd={1}";

    static WebClientService()
    {
        _client.UseDefaultCredentials = true;
    }
    
    public string UpdatePassword(string serverIp, string pass)
    {
            string result = string.Empty;
            string urlPwd = string.Format(_getNewPwdUrl, "admin", pass);
            string xmlPayload = DownloadXml(urlPwd);
            string updateUrl = string.Format(_postNewPwdUrl, serverIp, pass);
            try
            {
                result = _client.UploadString(updateUrl, xmlPayload);
                   
            }
            catch (Exception ex)
            {
                Ilogger.Error($"UpdatePassword > Error: {ex.Message}, 
                    stackTrace: {ex.StackTrace}");
            }
            return result;
        }

    public string DownloadXml(string address)
    {
        string result = string.Empty;
        try
        {
            result = _client.DownloadString(address);
        }
        catch (Exception ex)
        {
            Ilogger.Error($"DownloadXml > Error: {ex.Message}, 
                stackTrace: {ex.StackTrace}");    
        }
        return result;
        }

    }

The xml payload will just contain some xmlstring with values that the api will read it by itself.

jamesnet214
  • 1,044
  • 13
  • 21
Walter
  • 79
  • 6
  • Which line are you getting an error on, `result = _client.UploadString(updateUrl, xmlPayload);`? – DekuDesu May 20 '21 at 23:34
  • No, in the service.cs itself when I am invoking UpdatePassword. By putting a breakpoint I realized it stopped there. That;s where I'm getting ObjectReference not set to an instance of an object. – Walter May 20 '21 at 23:37
  • I think my format of creating webclient instance is incorrect. – Walter May 20 '21 at 23:39

2 Answers2

0

In which method does the null reference exception occur in?

From the looks of things I am going to assume it's when you call ChangePassword sins from what you posted _webClientService, in Service.cs, is never assigned to and is always null so you might want to change it to _webClientService = new WebClientService();

If it's not there then I am going to guess it's in static WebClientService(), to avoid a race, just in case, between the assignment of _client & the setting of _client.UseDefaultCredentials, I would recommend changing it to:

static WebClientService()
{
    _client = new WebClient()
    {
        UseDefaultCredentials = true
    };
}
Space
  • 175
  • 9
0

You may have forgotten to instantiate _webClientService.

You created a field for it here:

private static readonly WebClientService _webClientService;

But it appears you never actually give it a value. But then procede to use the null field here:

private static async Task<string> ChangePassword(string ip, string pass)
{
     _webClientService.UpdatePassword(serverIp, pass);
}

Make sure to instantiate it so it can be used such as:

private static readonly WebClientService _webClientService = new WebClientService();
DekuDesu
  • 2,224
  • 1
  • 5
  • 19