簡體   English   中英

如何判斷WCF服務是否正在主機上運行?

[英]How To Tell If WCF Service Is Running At Host?

我有一個自我托管WCF服務的C#應用​​程序。 我想在應用程序中添加一個按鈕單擊事件,讓用戶知道該服務是否正在運行/正在托管。 有沒有一種方法可以檢測服務是否正在運行/托管?

如果有人希望看到它,這是我用來開始托管服務的代碼:

        private static void RunService()
    {
        System.ServiceModel.ServiceHost host = new System.ServiceModel.ServiceHost(typeof(AccountingOperationsService.AccountingOperationsService));
        System.ServiceModel.Description.ServiceDebugBehavior debug = host.Description.Behaviors.Find<System.ServiceModel.Description.ServiceDebugBehavior>();
        // if not found - add behavior with setting turned on 
        if (debug == null)
        {
            host.Description.Behaviors.Add(
                 new System.ServiceModel.Description.ServiceDebugBehavior() { IncludeExceptionDetailInFaults = true });
        }
        else
        {
            // make sure setting is turned ON
            if (!debug.IncludeExceptionDetailInFaults)
            {
                debug.IncludeExceptionDetailInFaults = true;
            }
        }
        try
        {
            host.Open();


        }
        catch (Exception ex)
        {

            string errorMessage = ex.Message + Environment.NewLine;
            errorMessage += ex.StackTrace + Environment.NewLine;

            DevExpress.XtraEditors.XtraMessageBox.Show(errorMessage, "Error Starting Service", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

也許,您需要在wcf服務中創建方法Ping

public bool Ping()
{
    return true;
}

並在應用程序中調用Ping

bool itsWork;
try
{
    itsWork = service.Ping();
}
catch(Exception ex){}

暫無
暫無

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

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