C# C不无故断开回路

C# C不无故断开回路,c#,vhosts,C#,Vhosts,我正在编写一些解析Apache配置文件的代码 正在分析的文件: NameVirtualHost *:80 # # VirtualHost example: # Almost any Apache directive may go into a VirtualHost container. # The first VirtualHost section is used for all requests that do not # match a ServerName or ServerAlias

我正在编写一些解析Apache配置文件的代码

正在分析的文件:

NameVirtualHost *:80

#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for all requests that do not
# match a ServerName or ServerAlias in any <VirtualHost> block.
#
<VirtualHost *:80>
    ServerAdmin webmaster@dummy-host.example.com
    DocumentRoot "c:/Apache2/docs/dummy-host.example.com"
    ServerName dummy-host.example.com
    ServerAlias www.dummy-host.example.com
    ErrorLog "logs/dummy-host.example.com-error.log"
    CustomLog "logs/dummy-host.example.com-access.log" common
</VirtualHost>

<VirtualHost *:80>
    ServerAdmin webmaster@dummy-host2.example.com
    DocumentRoot "c:/Apache2/docs/dummy-host2.example.com"
    ServerName dummy-host2.example.com
    ErrorLog "logs/dummy-host2.example.com-error.log"
    CustomLog "logs/dummy-host2.example.com-access.log" common
</VirtualHost>
vhostRules对象

类vhostRules{public string key;public string value;}

vhostRules基本上是一个保存键和值的类

现在问题是hosts[c].addRuleparam[0],param[1];您可以看到,上面的读取文件在vhost类中再次调用addRule,即使我清空了addRule,所以里面没有代码,但仍然不起作用

这是我唯一能说明问题的方法——没有比这更小的方法可以复制,因为错误似乎与编译的最终结果有关

我找到的唯一方法是注释或删除主机[c].addRuleparam[0],param[1];从我的代码,然后它的工作,但如上所述,当我清空它,它不工作,这是理论上不正确的

尽管代码按原样执行,但不会发生错误,并且只运行一次循环

无错误发生这包括无死机、无异常和无冻结/崩溃

更新 更新阅读器

try
{
    hosts[c].addRule(param[0], param[1]);
}catch (Exception e) { Console.WriteLine(e.Message); }
提供输出

1 of 2
Index was outside the bounds of the array
Index was outside the bounds of the array
Index was outside the bounds of the array
Index was outside the bounds of the array
Index was outside the bounds of the array
Index was outside the bounds of the array
Index was outside the bounds of the array
Index was outside the bounds of the array
2 of 2
Index was outside the bounds of the array
Index was outside the bounds of the array
Index was outside the bounds of the array
Index was outside the bounds of the array
Index was outside the bounds of the array
Index was outside the bounds of the array
Index was outside the bounds of the array
Index was outside the bounds of the array
断点信息

param = {string[2]} 
    [0] = "ServerAdmin" 
    [1] = "webmaste

r@dummy-host.example.com

仅提供有关此问题的实际答案:

我最初的猜测——这只是一个猜测,因为我们没有我们需要的所有信息——是在循环中的某个地方抛出了一个异常

我能看到的最有可能出现例外情况的候选行是:

hosts[c].addRule(param[0], param[1]);
rules[counter] = new vhostRules();
string[] param = line.Trim().Split(' ');
vhost类中的addRule方法做了一些非常有趣的事情,但是我们没有所有的信息,所以我不能确定

我们需要什么

我们至少需要知道发生了什么。是否正在引发异常?它在哪一条线上?它会默默地死去吗?你有没有通过密码

请帮助我们帮助你

更新 因此,问题在于:

string[] param = line.Trim().Split(' ');                
hosts[c].addRule(param[0], param[1]);            
或:

现在,您在规则[]中用于键值对的键是什么

你可以试试 更改行:

hosts[c].addRule(param[0], param[1]);
rules[counter] = new vhostRules();
string[] param = line.Trim().Split(' ');
致:

这将允许您在某些行的末尾保留公用项。。。这可能不是你的主要问题,但至少可能是个问题

更新2 在字符串[]param=line.Trim.Splitnew char[]{''},2;,上面的行之后设置断点;,并查看param实际设置为什么。添加手表,查看参数[0]和参数[1]的确切含义。我有一种感觉,他们没事,实际上是在addRule方法的某个地方抛出了异常


因此,再次在addRule方法中的代码周围添加错误处理,并查看输出的内容。查看堆栈跟踪,检查行号,并向我们准确显示其中发生的情况。

我想我找到了。您可以为每个虚拟主机定义中的每一行执行以下代码:

/* get rid of whitespace and split on the space (' ')*/
string[] param = line.Trim().Split(' ');
/* provde the current vHost number and the key and value in the config file */
hosts[c].addRule(param[0], param[1]);
这假定每行至少包含两个空格分隔的值。除了最后一个例外,所有的都是这样


我希望您能够获得ArrayIndexOutOfBoundsException或任何在.NET中被称为ArrayIndexOutOfBoundsException的东西,但似乎有什么东西吞没了它。

这不是问题的解决方案,但至少是一些清理:更改此行:

public vhostRules[] rules = new vhostRules[1];
进入

进入


代码没有问题,只是没有运行。天啊,我想这就是大多数不运行的代码的问题!这是一个公平的评论:我的车没有问题,引擎就是无法启动。此外,我强烈建议发布代码的相关部分。。。我不会等45秒才从MegaUpload下载一些东西,然后才能开始帮助您使用没有问题的代码。因此,请向我们展示相关部分,这不可能是所有不起作用的事情。试着用一个尽可能小的例子来重现它,然后把它贴出来。@Barkermn01:如果你的应用程序那么大,那么没有人会看透它。试着提出一个能证明你的问题的答案。在这样做的过程中,您可能会发现问题所在。任何未处理的异常都会被抛出到调试器中,因为您可以看到没有错误处理,所以所有的异常都未处理。首先,您仍然没有回答“我们需要什么”部分中的任何问题。其次,不一定:检查VS中的调试菜单、选项和设置。确保启用异常助手和所有其他相关选项均已启用。mate go read我说虽然代码有问题,但我已采取了步骤我告诉过你它没有错误,并告诉过你没有引发异常是的,我的VS已正确设置以处理异常,所以这就是你在我面前的观点,这就是我确认我为什么下载代码是为了让你能看到这一点当然,这是你说的,我不反对。因此,从外观上看,addRule方法存在问题。这导致我们需要查看vhostRules的构造器,正如新vhostRules所称;。您提到从addRule方法中删除所有代码会留下相同的结果,这导致我想到了beli
eve规则未正确添加到集合中。你用什么作为收藏的钥匙?这是在什么地方复制的吗?类vhostRules{public string key;public string value;}只是没有任何构造或任何东西看到了相同的东西:我刚刚要求他将整个内容包装在一个try/catch块中,看看我们是否可以得到一些异常输出。除非它被进一步吞没,否则我们真的应该做点什么。干得好,伙计们,我得到的索引超出了数组的界限。为什么在没有处理到位的情况下处理它是一个燃烧的奇迹,我要+1这个,只是因为我很想在查看代码时说很多类似的事情来整理代码@Barkermn01-请注意,重构尽可能多的代码,你和我将有一个更轻松的调试时间。伙计,你会喜欢这个,但这为我修复了最后的错误,谢谢
public vhostRules[] rules = new vhostRules[1];
private List<vhostRules> rules = new List<vhostRules>();
public vhostRules[] getRules()
{
    return rules;
}
using System.Linq;
public vhostRules[] getRules()
{
    return rules.ToArray();
}