繁体   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