windows 服务 c# 中的 UnhandledException

UnhandledException in windows service c#

我遇到了一些真正的麻烦。我真的希望有人能够帮助我。

我有一个 Windows 服务,它应该 运行 有一定的频率。 我的服务与我们供应商的一些网络服务进行通信。

我的问题是使我的服务稳健,以防这些服务因故障等原因不可用。

我尝试用 try catch 来控制它。

/***************************************************************************************************/
/* Code snippet: Unable to compile. (it says: The name 'xyz' does not exist in the current context)*/
try  
{
  var xyz = new xyz.ExternalService().GetRelevantData();
}

catch(Exception e)
{
  ...handle the exception
}
/* Code snippet: Unable to compile. (it says: The name 'xyz' does not exist in the current context)*/
/***************************************************************************************************/

但是编译器不会接受这个(它说:名称 'xyz' 在当前上下文中不存在),所以我需要另一个解决方案。 我遇到了 currentDomain.UnhandledException,我想使用这个功能。 不幸的是我无法用它来处理异常。

谁能帮我解决问题?

关于下面除以零的代码,我打算将除法替换为:

var xyz = new xyz.ExternalService().GetRelevantData();

我相信这会解决我的问题。

/*********************************************************************/
/* Code snippet: currentDomain.UnhandledException : Division by zero.*/

    public void divisionbyzero()
    {
        AppDomain currentDomain = AppDomain.CurrentDomain;
        currentDomain.UnhandledException += new UnhandledExceptionEventHandler(MyHandler);

        int i = 0;
        int m = 12;
        double x = 0;
        try
        {
            x = m * i;
        }
        catch (Exception e)
        {
            writeToLog(e.ToString());                
        }
        x = m / i;
    }
/*********************************************************************/
/* Code snippet: currentDomain.UnhandledException : Division by zero.*/

我想在除法完成后用 'UnhandledException' 捕获异常。 但不幸的是,这并没有发生。

下面是代码上下文的新部分: 我希望这是有道理的:

/*****************************************************************/        
/****Code snippet: called from Windows service OnTimedEvent *****/        
    static void MyHandler(object sender, UnhandledExceptionEventArgs args)
    {
        Exception e = (Exception)args.ExceptionObject;
        File.AppendAllText("C:\lab\logs\log.txt", "" + e.Message + ""); 
    }

    private void writeToLog(Exception e)
    {
        File.AppendAllText("C:\lab\logs\log.txt", "" + e.Message + "");
    }

    public void divisionbyzero() // this is called in Windows service in OnTimedEvent.
    {

        AppDomain currentDomain = AppDomain.CurrentDomain;
        currentDomain.UnhandledException += new UnhandledExceptionEventHandler(MyHandler);


        int ii = 0;
        int mm = 12;
        double xx = 0;
        try
        {
            xx = mm * ii;
        }
        catch (Exception excep)
        {
        File.AppendAllText("C:\lab\logs\log.txt", "" + excep.ToString() + ""); 
        }

        xx = mm / ii; // This is the exception I would like (currentDomain.UnhandledException) to handle
    }
/****Code snippet: called from Windows service OnTimedEvent ************/        
/***********************************************************************/ 

我在网上查了一下:这个人也有同样的问题,你可能想看看这个:http://go4answers.webhost4life.com/Example/getting-appdomainunhandledexception-74589.aspx

您需要以下代码来创建实例:

var xyz = new xyz().ExternalService().GetRelevantData();

现在 xyz 将被视为函数!我猜你的代码中有这个功能!

然后把它写成下面:

try  
{
  var xyz = new xyz().ExternalService().GetRelevantData();
}

catch(Exception e)
{
  ...handle the exception
}

从您的评论看来,这是您的问题:

try  
{
  var xyz = new xyz.ExternalService().GetRelevantData();
}

catch(Exception e)
{
  //but I want to use xyz here
  ...handle the exception
}

因此,在更高的范围内定义 xyz,因此它在 catch;

中可见
var xyz = new Xyz();
try
{
    var someValue = xyz.DoSomething();
}
catch(Exception e)
{
    //xyz is still visible here.
}

问题已解决:

var xyz = new xyz().ExternalService();
try  
{
    xyz.GetRelevantData();
}

catch(Exception e)
{
    ...handle the exception
}

感谢您的所有帖子:-)。