【问题标题】:Windows service getting stopped abruptlyWindows 服务突然停止
【发布时间】:2017-12-26 11:06:09
【问题描述】:

我有一个 Windows 服务应用程序,它将一些消息发送到另一个网络服务(实习生作为电子邮件发送)并从服务器下载响应。

最近,服务每隔几天就会停止一次。 (尽管包含了一些异常处理,但它仍然没有被捕获并且服务正在崩溃)。

在事件查看器中登录的异常如下:

框架版本:v4.0.30319 描述:进程被终止 由于未处理的异常。异常信息:System.Net.WebException 堆栈:在 System.Net.ServicePointManager.FindServicePoint(System.Uri, System.Net.IWebProxy,System.Net.ProxyChain ByRef, System.Net.HttpAbortDelegate ByRef, Int32 ByRef) 在 System.Net.HttpWebRequest.FindServicePoint(Boolean) 在 System.Net.HttpWebRequest.get_ServicePoint() 在 System.Net.AuthenticationState.PrepareState(System.Net.HttpWebRequest) 在 System.Net.AuthenticationState.ClearSession(System.Net.HttpWebRequest) 在 System.Net.HttpWebRequest.ClearAuthenticatedConnectionResources()
在 System.Net.HttpWebRequest.Abort(System.Exception, Int32) 在 System.Net.HttpWebRequest.AbortWrapper(System.Object) 在 System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem() 在 System.Threading.ThreadPoolWorkQueue.Dispatch() 在 System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()

我搜索了很多,但找不到正确的答案。 任何帮助,将不胜感激。使用的代码:

public class DataAPIWebClient : WebClient
{
    protected override WebRequest GetWebRequest(Uri uri)
    {
        WebRequest w = base.GetWebRequest(uri);
        w.Timeout = 60000;
        return w;
    }
}

public string Send(string path)
{
    try
    {
        using (DataAPIWebClient webClient = new DataAPIWebClient())
        {
            webClient.Credentials = new NetworkCredential("XXX", "YYY");
            string reply = webClient.DownloadString(path);
            return reply;
        }
    }
    catch (WebException)
    {
        throw;
    }
}

【问题讨论】:

    标签: c# webclient


    【解决方案1】:

    这是你的问题

    catch (WebException)
    {
        throw;
    }
    

    你只是重新抛出你的异常,它永远不会被捕获,你永远不知道错误是什么,所以服务别无选择,只能崩溃

    我可以建议

    catch (WebException ex)
    {
        Log.Error(ex);
        // Or write it to a file or something
    }
    

    编辑:如 cmets 所述,这不是解决方案, 但是您缺少该堆栈中的实际异常消息 由于未捕获的异常而导致的跟踪,但它是未捕获的 使您的服务崩溃的异常

    【讨论】:

    • 非常感谢您的快速回复。从不同的方法调用“Send”方法,所有其他方法代码都包裹在 Try、catch 块中,异常也被记录到 Textfile 和数据库中。仍然没有捕获到与 ServicePointManager.FindServicePoint 相关的异常。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-18
    相关资源
    最近更新 更多