我的解决方案里有个WCF服务库的项目,每次调试WCF服务主机都自动启动了,我想让它不要启动,而通过我自己的程序代码控制其启动,怎么处理?

解决方案 »

  1.   


    ServiceHost host = new ServiceHost(typeof(服务名称));
      if (host.State != CommunicationState.Opening)
      {
         host.Open();//开启服务
      }
     if (host != null)
      {
         if (host.State == CommunicationState.Opened)
          {
            host.Close();//关闭服务
          }
      }
      

  2.   

    自建一个Console或WinForm或WinService程序
    再把楼上的的代码添加到合适的位置,再设置VS启动项目,就OK啦
    比如Console:
    class Program
    {
        static void Main(string[] args)
        {
            ServiceHost host = new ServiceHost(typeof(服务名称));
            if (host.State != CommunicationState.Opening)
            {
               host.Open();//开启服务
             }
            System.Console.WriteLine("按任意键关闭服务.");
            System.Console.ReadLine();
            if (host != null)
            {
               if (host.State == CommunicationState.Opened)
               {
                  host.Close();//关闭服务
                 }
            }
        }
    }