WCF托管与Windows服务,客户端无法看到端点

时间:2014-07-26 14:08:46

标签: c# wcf service

我正在尝试构建一个测试应用程序来测试wcf并了解更多内容:

我写了一个WCF服务并在Windows服务中托管了它。这是来自Windows服务的app.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>    
     <services>
        <service name="WCFLibrary.CalculatorService"
                 behaviorConfiguration="CalculatorServiceBehavior">
           <host>
              <baseAddresses>
                 <add baseAddress="http://localhost:8000/ServiceModelSamples/service"/>
              </baseAddresses>
           </host>
           <!-- this endpoint is exposed at the base address provided by host: http://localhost:8000/ServiceModelSamples/service  -->
           <endpoint 
               address=""
               binding="wsHttpBinding"
               contract="WCFLibrary.ICalculator" />
           <!-- the mex endpoint is exposed at http://localhost:8000/ServiceModelSamples/service/mex -->
           <endpoint 
               address="mex"
               binding="mexHttpBinding"
               contract="IMetadataExchange" />
         </service>
      </services>
      <behaviors>
         <serviceBehaviors>
            <behavior name="CalculatorServiceBehavior">
               <serviceMetadata httpGetEnabled="true"/>
               <serviceDebug includeExceptionDetailInFaults="False"/>
            </behavior>
         </serviceBehaviors>
      </behaviors>
   </system.serviceModel>
</configuration>

现在我想构建一个客户端应用程序来使用WCF服务。但是当我转到Add Service Reference时,它会给我一个错误,它无法到达终点。我可以看到服务从服务管理器运行。我通过从VS进行调试来进行测试。在调试期间,客户端能够看到端点。但是,如果我停止并且Debug返回服务管理器,则客户端无法看到端点。

可能有什么不对?有什么线索吗?

EDIT1

Windows服务Service1.cs

namespace WindowsService
{
   public partial class Service1 : ServiceBase
   {
        public ServiceHost serviceHost = null;

        public Service1()
        {
             InitializeComponent();
        }

        public void onDebug()
        {
            OnStart(null);
        }

        protected override void OnStart(string[] args)
        {
            if (serviceHost != null)
            {
                serviceHost.Close();
            }

            // Create a ServiceHost for the CalculatorService type and 
            // provide the base address.
            serviceHost = new ServiceHost(typeof(CalculatorService));

            // Open the ServiceHostBase to create listeners and start 
            // listening for messages.
            serviceHost.Open();
        }

        protected override void OnStop()
        {
            if (serviceHost != null)
            {
                 serviceHost.Close();
                 serviceHost = null;
            }
        }
    }
}

Program.cs

namespace WindowsService
{
    public  class CalculatorWindowsService :ServiceBase
    {
        public ServiceHost serviceHost = null;

        public CalculatorWindowsService()
        {
            // Name the Windows Service
            ServiceName = "WCFWindowsServiceSample";
        }

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        public static void Main()
        {
//#if DEBUG
//           Service1 myservice = new Service1();
//           myservice.onDebug();
//           System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
//#else
            //  CalculatorWindowsService calc = new CalculatorWindowsService();
            ServiceBase.Run(new CalculatorWindowsService());
//#endif
        }
    }
}

1 个答案:

答案 0 :(得分:0)

上面的代码没有错。我的第二台笔记本电脑使用相同的代码。所以在我的第一台笔记本电脑上与操作系统或IE有关。将检查出来。