【问题标题】:How To Tell If WCF Service Is Running At Host?如何判断 WCF 服务是否在主机上运行?
【发布时间】:2017-01-27 21:57:42
【问题描述】:

我有一个自托管 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);
        }

【问题讨论】:

    标签: c# winforms wcf


    【解决方案1】:

    也许,您需要在 wcf 服务中创建方法 Ping

    public bool Ping()
    {
        return true;
    }
    

    并在应用程序中调用Ping

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

    【讨论】:

      猜你喜欢
      • 2010-10-19
      • 2012-04-07
      • 2013-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多