简体   繁体   中英

c# Console Application referenced DLL variable remains null

I have read through many posts on this type of situation and I can't figure out the solution or what I am doing wrong.

I have a code that references a DLL. It is added as a reference in Visual Studio and it is copied locally along with the target exe file. It runs for me, but when I deploy it to another computer it causes a NullReference exception at the below line of code (see comment):

Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.

at CheckUpdate.Program.checkUpdate() in Program.cs:line 28

at CheckUpdate.Program.Main(String[] args) in Program.cs:line 22

Here is my code, see comments where the error is reported on the stack trace:

using System;
using System.Collections.Generic;
using System.Text;
using Com.Idiominc.Webservices.Client;
using System.Diagnostics;
using System.IO;
using System.Net;


namespace CheckUpdate
{
    class Program 
    {
        private static WSContext _UpdateWScontext { get; set; }
        private static string wsUrl = "https://www.myWorldServer.com/ws";
        private static string[] myArgs;

        static int Main(string[] args)
        {
            myArgs=args;
            if (myArgs.GetUpperBound(0) != 1) return 9;
            return checkUpdate();
        }

        public static int checkUpdate()
        {
                string testStr = "password1";

                while (_UpdateWScontext == null && testStr != "")
                {
                    try
                    {
                        _UpdateWScontext = new WSContext("user", testStr, wsUrl + "/services");
                    }
                    catch
                    {
                        if (_UpdateWScontext == null)
                        {
                            if (testStr == "password1") testStr = "password2";
                            else if (testStr == "password2") testStr = "password3";
                            else testStr = "";
                        }
                    }
                }
                if (_UpdateWScontext.token == string.Empty) return 0;

                string currentversion = myArgs[0];

                string latestver = getLatestVersion();

            if (latestver == null)
            {
               return 0;
            }
            else if (!currentversion.Equals(latestver))
            {
                if (getLatestDownload()) return 1;
                else return 0;
            }
            else
            {
                _UpdateWScontext.logout(_UpdateWScontext.token);
                return 2;
            }
        }       
    }
}

The DLL (WSWebServices.dll) is copied locally along with the executable. It runs on my own station, but when I port it to another station, it causes the exception.

Any advice or clue would be appreciated, thanks.

Edit:

Based on comments the NullReference exception was on the line

if (_UpdateWScontext.token == string.Empty) return 0;

I have now modified this to

if (_UpdateWScontext == null || _UpdateWScontext.token == string.Empty) return 0;

With this no more NullReference Exception, however the exit code now is always 0 , meaning the variable is still null .

Any suggestion as to why the connection is not working to the URL? It does work from the browser on the same computer.

Update - solved

I traced the error by outputting the Exception stack trace to the console on the station where the app was not working, and it turned out that the WSWebServices.dll is referencing the Microsoft.Web.Services2.dll (part of the WSE package) and for some reason this was missing from the installation on the misbehaving station.

I added it as a reference and made sure that the dll is copied to the application folder and it now works as intended. Documented here for others in the same mystery why the WorldServer web services would not work.

Within while loop, firstly you try with Password1 , you get exception on creating WSContext , then in catch block you set testStr to Password2 . With Password2 you again get an exception , and this time in catch block you set testStr to an empty string, and you leave out of while loop without creation of _UpdateWScontext .

I think you null ref exception exactly at this point of code if (_UpdateWScontext.token == string.Empty) return 0; .

You should log exception details in the catch block within the while loop, it can give clue about the error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM