c#控制台应用程序引用的DLL变量保持为空

时间:2017-10-28 05:41:27

标签: c# web-services dll

我已阅读过很多关于此类情况的帖子,我无法弄清楚解决方案或我做错了什么。

我有一个引用DLL的代码。它在Visual Studio中作为引用添加,并与目标exe文件一起在本地复制。它为我运行,但当我将它部署到另一台计算机时,它会在下面的代码行中导致NullReference异常(请参阅注释):

  

未处理的异常:System.NullReferenceException:对象引用   没有设置为对象的实例。

     

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

     

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

这是我的代码,请参阅堆栈跟踪中报告错误的注释:

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的任何建议?它可以在同一台计算机上的浏览器中运行。

更新 - 已解决

我通过将Exception堆栈跟踪输出到应用程序不工作的工作站上的控制台来跟踪错误,结果发现WSWebServices.dll引用了Microsoft.Web.Services2.dll(WSE的一部分)包装)并且出于某种原因,在行为不端的车站上安装了这个。

我添加了它作为参考,并确保将dll复制到应用程序文件夹,它现在按预期工作。这里为其他人提供了同样神秘的文档,为什么WorldServer Web服务不起作用。

1 个答案:

答案 0 :(得分:1)

在while循环中,首先使用Password1进行尝试,在创建WSContext时获得异常,然后在catch块中将testStr设置为{{1 }}。使用Password2,您再次获得异常,这次在catch块中将testStr设置为空字符串,并且在不创建Password2的情况下退出while循环。

我认为您在代码_UpdateWScontext的这一点上完全取消了异常

您应该在while循环中的catch块中记录异常详细信息,它可以提供有关错误的线索。