簡體   English   中英

c#控制台應用程序引用的DLL變量保持為空

[英]c# Console Application referenced DLL variable remains null

我已經閱讀了許多有關這種情況的文章,但無法弄清楚解決方案或我做錯了什么。

我有一個引用DLL的代碼。 它被添加為Visual Studio中的參考,並且與目標exe文件一起在本地復制。 它為我運行,但是當我將其部署到另一台計算機時,它將在下面的代碼行中引起NullReference異常(請參閱注釋):

未處理的異常:System.NullReferenceException:對象引用未設置為對象的實例。

在Program.cs:第28行的CheckUpdate.Program.checkUpdate()中

在Program.cs:22行的CheckUpdate.Program.Main(String [] args)中

這是我的代碼,請參閱在堆棧跟蹤中報告錯誤的注釋:

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;
            }
        }       
    }
}

DLL(WSWebServices.dll)與可執行文件一起在本地復制。 它在我自己的工作站上運行,但是當我將其移植到另一個工作站時,它將導致異常。

任何建議或線索,將不勝感激,謝謝。

編輯:

根據評論,NullReference異常在線上

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

我現在將其修改為

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

不再有NullReference異常,但是退出代碼現在始終為0 ,這意味着變量仍為null

關於為何無法對URL進行連接的任何建議? 它確實可以從同一台計算機上的瀏覽器運行。

更新-解決

我通過將異常堆棧跟蹤輸出到應用程序無法運行的站點上的控制台來跟蹤錯誤,結果證明WSWebServices.dll引用了Microsoft.Web.Services2.dll (WSE軟件包的一部分),並且由於某種原因,該行為平台上的安裝中缺少此功能。

我將其添加為參考,並確保將dll復制到應用程序文件夾中,並且現在可以正常運行了。 出於同樣的秘密,在這里為其他人記錄了為什么WorldServer Web服務無法正常工作。

在while循環中,首先嘗試使用Password1 ,在創建WSContext遇到異常 ,然后在catch塊中將testStr設置為Password2 使用Password2您再次遇到異常 ,這次在catch塊中,將testStr設置為空字符串,並且退出了while循環,而沒有創建_UpdateWScontext

我認為if (_UpdateWScontext.token == string.Empty) return 0;恰好在此代碼點為null ref 異常 if (_UpdateWScontext.token == string.Empty) return 0;

您應該在while循環內的catch塊中記錄異常詳細信息,它可以提供有關錯誤的線索。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM