簡體   English   中英

如何從自托管WCF服務引發FaultException?

[英]How to throw FaultException from self hosting wcf service?

我打算將服務托管在Windows服務中,但是我正在考慮標題中描述的問題。 有人有類似的問題嗎? 謝謝

更新資料

問題是,當您在WinForms / WPF / Win Service應用程序中引發異常時,程序崩潰,您將不得不重新啟動它。

異常並不總是會使您的服務器崩潰。 甚至意外的服務器端異常也將傳輸到客戶端。 但是,它被認為比預期的要嚴重得多,從而使通道出現故障。

基本思想是在接口合同中包括預期的異常(錯誤)。 有很多方法可以做到這一點,這是一篇介紹文章

當然,您需要在服務器上進行體面的異常處理。

您可以嘗試的方法是,通過掛鈎到主機應用程序的Main方法入口點的ThreadException事件來攔截所有異常,以檢查它是否是FaultException。

static class Program
{
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());

        // Hook to this event below
        Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
    }

    static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
    {
        if (e.Exception is FaultException)
            return; // Bypass FaultExceptions;
        else
            throw e.Exception; // Throw otherwise
    }
}

暫無
暫無

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

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